jUnit testing and Spring

Download Report

Transcript 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

default-autowire="byName"

>  Inversion of control  AbstractDependencyInjectionSpringContextTests  onSetUp(), onTearDown()

What we will achieve?

 Faster development  Limited need to build or deploy  Run jUnit test from Eclipse  10s between Code change  High confidence in code Testing it  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  Hibernate session net.sf.hibernate.dialect.HSQLDialect

false

Real example

  Requirement  DomainDaoTest.getDomainByNamePattern

public Domain getDomainByNamePattern(String namePattern); Development cycle 1.

Create new failing test 2.

3.

4.

Make it compile Achieve passing test – implement function Commit

Fixing bug – XP approach

1.

2.

3.

Create failing test reproducing bug Fix bug (makes test pass) 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