jUnit testing and Spring

jUnit testing and Spring
How to benefit from Spring support for
jUnit during development
Basic concepts
 jUnit framework


setUp(), tearDown()
testXXX()
 Spring framework

context.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="byName">
</beans>

Inversion of control

AbstractDependencyInjectionSpringContextTests

onSetUp(), onTearDown()
What we will achieve?
 Faster development



Limited need to build or deploy
Run jUnit test from Eclipse
Testing it
10s between Code change
 High confidence in code


Repository of existing tests decreases chance
of introducing new bug
More satisfaction and peace of mind for
developer who checks-in code
test-context.xml beans
 In-memory database
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
<property name="url"
value="jdbc:hsqldb:mem:test"/>
<property name="username"
value="sa"/>
</bean>
 Hibernate session
<bean id="sessionFactory"
class="org.springframework.orm.hibernate.LocalSessionFactoryBean"
init-method="createDatabaseSchema">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">net.sf.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
</props>
</property>
<property name="mappingLocations" value="classpath*:com/norkom/**/*.hbm.xml" />
</bean>
Real example
 Requirement

DomainDaoTest.getDomainByNamePattern
public Domain getDomainByNamePattern(String namePattern);
 Development cycle
1.
2.
3.
4.
Create new failing test
Make it compile
Achieve passing test – implement function
Commit
Fixing bug – XP approach
1. Create failing test reproducing bug
2. Fix bug (makes test pass)
3. Commit
Thanks to existing test it is unlikely that bug will be
re-introduced in future.
Best practices
 „Test first“ approach
 Learn from (or extend) existing tests
 Import base-test-context.xml
 Use stubs where appropriate
 Use UnsupportedOperationException

For eclipse generated method body
Existing support in CoreDev
 BaseTestCase


Creates admin user
Good for facades testing
 TestUtils

dbUnitSetup


populates DB based on dbUnit XML file
writeDatabaseDtd

generates DTD for dbUnit XML file
 base-test-context.xml
Common testing issues
 hibernate.MappingException : Unknown entity class:
com.norkom.base.resources.model.Domain



Reason: Missing mapping file in classpath
Build mapping files
Add them to your project sources
 hibernate.MappingException: duplicate import: Domain


Reason: Mapping file twice in classpath
Check for linked *.jar in Eclipse, they can contain your
*.hbm.xml file, together with one in source folders
 UncategorizedSQLException: Could not execute JDBC batch
update; uncategorized SQLException for SQL []; SQL state [null];
error code [0]; failed batch;


Reason: Constraint violated in HSQLDB
Swap HSQL to Oracle to see which constraint
Thank you...
 And enjoy your automated testing