Presentation is loading. Please wait.

Presentation is loading. Please wait.

JUnit testing and Spring How to benefit from Spring support for jUnit during development.

Similar presentations


Presentation on theme: "JUnit testing and Spring How to benefit from Spring support for jUnit during development."— Presentation transcript:

1 jUnit testing and Spring How to benefit from Spring support for jUnit during development

2 Basic concepts jUnit framework setUp(), tearDown() testXXX() Spring framework context.xml Inversion of control AbstractDependencyInjectionSpringContextTests onSetUp(), onTearDown()

3 Faster development Limited need to build or deploy Run jUnit test from Eclipse 10s between 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 What we will achieve? Code change Testing it

4 test-context.xml beans In-memory database Hibernate session <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean" init-method="createDatabaseSchema"> net.sf.hibernate.dialect.HSQLDialect false

5 Real example Requirement DomainDaoTest.getDomainByNamePattern public Domain getDomainByNamePattern(String namePattern); Development cycle 1. Create new failing test 2. Make it compile 3. Achieve passing test – implement function 4. Commit

6 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. Fixing bug – XP approach

7 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

8 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

9 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

10 Thank you... And enjoy your automated testing


Download ppt "JUnit testing and Spring How to benefit from Spring support for jUnit during development."

Similar presentations


Ads by Google