close

Вход

Забыли?

вход по аккаунту

?

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
Документ
Категория
Презентации
Просмотров
6
Размер файла
439 Кб
Теги
1/--страниц
Пожаловаться на содержимое документа