Skip to content

Category Archives: Spring

Mock Unit Testing Services and Daos with Mockito and Spring 3

If you haven’t been mocking yet, check this out. There are other tools in Groovy using DSLs but if you want to stick to Java, Mockito is by far the best BDD-style mock unit testing tool. Traditional examples provide a List mocking example or foo/bar example which I find it mostly annoying since it doesn’t [...]

Understanding Spring Transaction Management

This is a little wiki file I had for a long time while understanding how spring transactions worked. Time to put it in a blog. (Note: data is unprocessed) – Two types of transactions: Global and Local Transactions – PlatformTransactionManager is main interface in Spring – PROPAGATION_REQUIRED, PROPAGATION_SUPPORTS, PROPAGATION_MANDATORY, PROPAGATION_REQUIRES_NEW, PROPAGATION_NOT_SUPPORTED, PROPAGATION_NEVER, PROPAGATION_NESTED – ISOLATION_DEFAULT, [...]

Spring Security 3 Unit Testing Example with DbUnit

This tutorial extends on the baseline laid by Spring Security Tutorial maven archetype. I’ve modified the spring security config file and will be unit testing a simple ProfileService (CRUD) on a Profile entity (User object). The tests include login/logout and spring services security unit testing. application-security.xml <?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd [...]

Custom Logout Filter handler in Spring 3

Spring Security provides comprehensive support for adding custom handlers for logout/login. The default logout simply logs the user out and goes to the login page. What if the application needs to determine the user’s role and redirect to a separate screen? For example, for an admin, a custom logout screen. What if the user is [...]

Generic Entity Converter for JSF / Spring / JPA

If you’ve come across Seam’s <s:convertEntity> and wondered why isn’t one there for JSF, here you go: Before using this, you should’ve designed your domain model to extend a Base Class (do it, it’s a good practice). In my case, I have BaseIdentityEntity which has an @Id, @Version properties import java.io.Serializable; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; [...]

Spring Integration Presentation with Mark Fisher

I had a chance to attend Mark Fisher‘s presentations a couple of times before at NEJUG. This time around, it was a local Java Meetup group in Cambridge. He’s a very humble and knowledgeable guy about a lot of things, from Spring to Hibernate to Seam, to Scala & MDA! With a brief introduction on [...]

Automated Unit Testing with DBUnit, Hsqldb in Spring & Hibernate projects

If your project has a lot of unit testing in Java and you use continuous builds (CruiseControl) to automate, it is often difficult to automate tests which connect to Database (Dao and integration tests). For most companies, having a dedicated Oracle schema for testing is expensive. Also, sometimes, developers need their own instances of schemas [...]

Generating Excel Formulas with Spring MVC and POI

I figured I’d lay this out there. It’s not awesome code, just quick and dirty. But it works. @Override protected void buildExcelDocument(Map model, HSSFWorkbook workbook, HttpServletRequest req, HttpServletResponse resp) throws Exception { //CREATE THE SHEET String periodDate = (String)model.get(PAYROLL_PERIOD_KEY); HSSFSheet sheet = workbook.createSheet(periodDate); sheet.setDefaultColumnWidth((short) 12); //GETCELL: getCell(SHEET, ROW, COLUMN); short currentRow = 0; //WRITE ROW [...]

Limitations of AbstractTransactionalDataSourceSpringContextTests

This has apparently gotten a fair amount of attention over the last year or so, but I never encountered it until today- call me lucky. Here’s the situation, you’re using Spring MVC and you have a controller that extends another controller (A->B). Your unit test extends AbstractTransactionalDataSourceSpringContextTests, and makes use of autowiring. You write your [...]

HSQLDB Integration Into Spring Security

Adding a database back end to Spring Security seems deceptively simple. And to be fair, there are several ways to do it. The way I had in mind looked like a shortcut. In my application there is basically one table for all users. It contains the values I need for username, password, role, and the [...]