spring 2 - goto blogjaoo.dk/.../slides/sambrannen_testingbyexamplewithspring.pdf · 2008-03-19 ·...

48
Spring 2.5 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Testing by Example with Spring 2.5 Sam Brannen Senior Software Engineer [email protected] http://www.springsource.com

Upload: others

Post on 20-Jun-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

Spring 2.5

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

Testing by Example with Spring 2.5

Sam Brannen

Senior Software Engineer

[email protected]

http://www.springsource.com

Page 2: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

Goals of the Presentation

• Gain an overview of the new Spring TestContext Framework

• Learn how to get the most out of Spring’s testing support

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 2

Spring’s testing support

Page 3: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

Before we start…

… a show of hands …

• Unit and integration testing

• Integration testing with Spring

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 3

• Integration testing with Spring

• Spring 2.5

• Spring TestContext Framework

• JUnit 3.8, JUnit 4.x, TestNG

Page 4: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

Agenda

• Background Information

• The TestContext Framework

• Examples and Custom Solutions

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 4

Page 5: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

Background InformationA bit of history and the impetus for change

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

Page 6: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

History of Testing in Java

• JUnit– Developed by Kent Beck (XP) and Erich Gamma (GoF)

– Debuted on the Java scene � 2000

– Wide adoption through JUnit 3.8.x

• TestNG

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 6

• TestNG– Modern unit and integration testing framework developed by Cédric Beust and Alexandru Popescu

– Debuted in April 2004

• Java SE 5 � September 2004

• JUnit 4 � 2006 (JUnit 4.4 � July ’07)

Page 7: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

Spring and Unit Testing

• POJO-based programming model– Program to interfaces

– IoC / Dependency Injection

– Out-of-container testability

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 7

– Out-of-container testability

• Testing mocks/stubs for various APIs:– Servlet

– Portlet

– JNDI

Page 8: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

Spring and Integration Testing

• Context management & caching

• Dependency injection of test instances

• Transactional test management

• Abstract JUnit 3.8 based support classes

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 8

• Abstract JUnit 3.8 based support classes– Abstract*SpringContextTests

Page 9: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

Growing Pains of an Inheritance Hierarchy

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 9

2 from JUnit + 9 from Spring = 11 classes!

Page 10: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

Impetus for Change

Out with the old and in with the new!

• Break free of the inheritance hierarchy

• Provide an easy migration path

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 10

• Provide an easy migration path

• Maintain the existing feature set

• Allow for extensibility

• Provide consistent support across testing frameworks

Page 11: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

The TestContext FrameworkFrom AbstractDependencyInjection-

SpringContextTests to @ContextConfiguration

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

SpringContextTests to @ContextConfiguration

Page 12: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

Spring TestContext Framework:In a Nutshell

• Fully revised, annotation-based test framework

• Supports JUnit 4.4 and TestNG as well as JUnit 3.8

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 12

as JUnit 3.8

• Supersedes older JUnit 3.8 base classes– AbstractDependencyInjection-SpringContextTests & friends

– They're still there for JDK 1.4

Page 13: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

• Convention over configuration– Use only annotations

– Reasonable defaults that can be overridden

• Consistent support for Spring's core

Spring TestContext Framework:Goals (1)

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 13

• Consistent support for Spring's core annotations

• Spring-specific integration testing functionality:– Context management & caching

– Dependency injection of tests

– Transactional test management

Page 14: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

• Agnostic of the testing framework in use

• Support unit and integration testing

• Extensible and highly configurable

• Support POJO test classes

Spring TestContext Framework:Goals (2)

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 14

• Support POJO test classes

• Provide base support classes where necessary and/or as a convenience

Page 15: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

TestContext Key Abstractions (1)

• TestContext– Context for executing test

• TestContextManager– Signals events for the managed context to listeners

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 15

listeners

• TestExecutionListener– Reacts to test execution events

– Spring provides these:• DependencyInjectionTestExecutionListener

• DirtiesContextTestExecutionListener

• TransactionalTestExecutionListener

Page 16: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

TestContext Key Abstractions (2)

TestContext

TestContextManager

AbstractTestExecutionListener

«interface»TestExecutionListener

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 16

DependencyInjectionTestExecutionListener

DirtiesContextTestExecutionListener

TransactionalTestExecutionListener

Page 17: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

TestExecutionListener Ordering and Nesting

@TestExecutionListeners({DependencyInjectionTestExecutionListener .class,DirtiesContextTestExecutionListener .class,TransactionalTestExecutionListener .class})

{1} prepareTestInstance(){2} prepareTestInstance(){3} prepareTestInstance()

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 17

{1} beforeTestMethod(){2} beforeTestMethod(){3} beforeTestMethod()

{3} afterTestMethod(){2} afterTestMethod(){1} afterTestMethod()

{*} test method

Page 18: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

Annotated Test Class Example

@RunWith( SpringJUnit4ClassRunner.class )@ContextConfiguration// defaults to MyTests-context.xml in same packagepublic class MyTests {

@Autowiredprivate MyService myService;

@BeforeTransactionpublic void verifyInitialDatabaseState() { … }

“The Spring TestContext Framework is an excellent example of good annotation usage as it allows composition

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 18

public void verifyInitialDatabaseState() { }

@Beforepublic void setUpTestDataWithinTransaction() { … }

@Testpublic void myTest() { … }

@Test@Transactionalpublic void myOtherTest() { … }

}

usage as it allows composition rather than inheritance.”

- Costin Leau, SpringSource

Page 19: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

TestContext Annotations

• TestExecutionListeners– @TestExecutionListeners

• Application Contexts– @ContextConfiguration and

@DirtiesContext• Dependency Injection

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 19

• Dependency Injection– @Autowired , @Qualifier , @Resource,

@Required , etc.• Transactions

– @Transactional , @NotTransactional , @TransactionConfiguration , @Rollback , @BeforeTransaction , and @AfterTransaction

Page 20: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

TestContext Annotations - JUnit

• Testing Profiles– @IfProfileValue and

@ProfileValueSourceConfiguration• JUnit extensions

– @ExpectedException , @Timed, @Repeat

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 20

Page 21: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

Using the TestContext Framework

• Use the SpringJUnit4ClassRunner for

JUnit 4.4 or instrument test class with TestContextManager for TestNG

• Or extend one of the new base classes

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 21

• Or extend one of the new base classes– Abstract(Transactional)

[JUnit38|Junit4|TestNG]SpringContextTests

Page 22: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

Test Helper classes

• SimpleJdbcTestUtils– Count table rows, empty tables, run SQL script

• ReflectionTestUtils

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 22

• ReflectionTestUtils– Set non-public fields, invoke non-public setters

• Independent of TestContext framework

Page 23: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

Examples and Custom Solutions

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

Page 24: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

PetClinic: JUnit 3.8 - Legacy

public class JUnit38LegacyClinicTests extendsAbstractTransactionalDataSourceSpringContextTests {

private Clinic clinic;

public void setClinic(Clinic clinic) {this.clinic = clinic;

}

protected String getConfigPath() {

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 24

protected String getConfigPath() {return "/clinic-context.xml" ;

}

public void testGetVets() {Collection<Vet> vets = this.clinic.getVets();assertEquals("JDBC query must match",

super.countRowsInTable("VETS"), vets.size());}

}

Page 25: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

PetClinic: JUnit 3.8 - TestContext

@ContextConfiguration(locations="/clinic-context.xm l")public class JUnit38ClinicTests extends

AbstractTransactionalJUnit38SpringContextTests {

@Autowiredprotected Clinic clinic;

// Test methods must be named test*()

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 25

// Test methods must be named test*()public void testGetVets() {

Collection<Vet> vets = this.clinic.getVets();assertEquals("JDBC query must match",

super.countRowsInTable("VETS"), vets.size());}

}

Page 26: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

PetClinic: JUnit 4.4 - TestContext

@ContextConfiguration(locations="/clinic-context.xm l")public class JUnit4ClinicTests extends

AbstractTransactionalJUnit4SpringContextTests {

@Autowiredprotected Clinic clinic;

@org.junit.Test

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 26

@org.junit.Testpublic void getVets() {

Collection<Vet> vets = this.clinic.getVets();assertEquals("JDBC query must match",

super.countRowsInTable("VETS"), vets.size());}

}

Page 27: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

PetClinic: TestNG - TestContext

@ContextConfiguration(locations="/clinic-context.xm l")public class TestNGClinicTests extends

AbstractTransactionalTestNGSpringContextTests {

@Autowiredprotected Clinic clinic;

@org.testng.annotations.Test

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 27

@org.testng.annotations.Testpublic void getVets() {

Collection<Vet> vets = this.clinic.getVets();assertEquals("JDBC query must match",

super.countRowsInTable("VETS"), vets.size());}

}

Page 28: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

PetClinic: JUnit 4.4 – TestContextPOJO

@RunWith( SpringJUnit4ClassRunner.class )@ContextConfiguration(locations="/clinic-context.xm l")@Transactionalpublic class PojoJUnit4ClinicTests {

@Autowiredprotected Clinic clinic;

protected SimpleJdbcTemplate simpleJdbcTemplate;

@Autowired

Annotated POJO

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 28

@Autowiredpublic void setDataSource(DataSource dataSource) {

this.simpleJdbcTemplate = new SimpleJdbcTemplate(da taSource);}

@org.junit.Testpublic void getVets() {

Collection<Vet> vets = this.clinic.getVets();assertEquals("JDBC query must match",

SimpleJdbcTestUtils.countRowsInTable (this.simpleJdbcTemplate, "VETS"), vets.size());

}}

Page 29: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

Parameterized DI Test?

Q: Is it possible to use JUnit 4’s Parameterizedrunner in conjunction with the dependency injection support in the Spring TestContext Framework?

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 29

A: Yes! As with the TestNG support, you can manually instrument your test with a TestContextManager , but… there are some

limitations with this approach for JUnit 4.

Page 30: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

Parameterized DI Test – Code (1)

@RunWith ( Parameterized .class )@ContextConfiguration@TestExecutionListeners( {

DependencyInjectionTestExecutionListener .class })public class ParameterizedDependencyInjectionTests {

private final TestContextManager testContextManager ;

Note: we cannot use both Parameterized and SpringJUnit4ClassRunnersimultaneously!

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 30

@Autowiredprivate ApplicationContext applicationContext;

private final String employeeBeanName;private final String employeeName;

Page 31: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

Parameterized DI Test – Code (2)

public ParameterizedDependencyInjectionTests(String employeeBeanName, String employeeName )throws Exception {

this.testContextManager =new TestContextManager(getClass());

this.employeeBeanName = employeeBeanName;this.employeeName = employeeName;

}

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 31

}

@Parameterspublic static Collection<String[]> employeeData() {

return Arrays. asList(new String[][] {{"employee1", "John Smith" },{"employee2", "Jane Smith"} });

}

Page 32: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

Parameterized DI Test – Code (3)

@Beforepublic void injectDependencies() throws Exception {

this.testContextManager.prepareTestInstance(this);

}

@Testpublic void testAgainstInjectedDependencies()

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 32

public void testAgainstInjectedDependencies(){

// test against injected dependencies// with the 'parameterized‘ values

}

Page 33: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

Demo: Parameterized DI Test

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

Page 34: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

Custom TestExecutionListener?

Q: Is it possible to create a custom TestExecutionListener to provide project-specific testing support?

A: Yes! Either implement

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 34

A: Yes! Either implement TestExecutionListener or extend AbstractTestExecutionListener and override the methods which suit your use case.

Page 35: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

LogLevelTests (simple)

@RunWith(SpringJUnit4ClassRunner.class)@TestExecutionListeners(

LoggingTestExecutionListener .class)public class LogLevelTests {

@Testpublic void alwaysPasses() { /* no - op */ }

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 35

public void alwaysPasses() { /* no - op */ }

@Testpublic void alwaysFails() {fail("always fails");

}}

Page 36: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

LoggingTestExecutionListener (simple)

public class LoggingTestExecutionListener extendsAbstractTestExecutionListener {

public void prepareTestInstance (TestContext context)throws Exception {

logInfo("Preparing test: " + context.getTestClass());}

public void beforeTestMethod (TestContext context) {logInfo("Before method: " + context.getTestMethod());

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 36

logInfo("Before method: " + context.getTestMethod());}

public void afterTestMethod (TestContext context) {String msg = "After method: " + context.getTestMethod();if (context.getTestException() != null)

logError(msg);else

logInfo(msg);}

}

Page 37: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

Demo: Simple Logging TestExecutionListener

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

Page 38: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

Custom Test Annotations?

Q: Is it possible to provide support for custom annotations in tests?

A: Yes! Implement a custom

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 38

A: Yes! Implement a custom TestExecutionListener which

processes your custom annotations.

Page 39: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

@LogLevel Custom Annotation

@Target({ElementType. TYPE,ElementType. METHOD})@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface LogLevel {

public static enum Level {NONE, ERROR, WARN, INFO, DEBUG, TRACE;

}

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 39

NONE, ERROR, WARN, INFO, DEBUG, TRACE;}

Level value() default Level.ERROR ;

}

Page 40: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

LogLevelTests (annotation)

@RunWith(SpringJUnit4ClassRunner.class)@TestExecutionListeners(

LoggingTestExecutionListener.class )@LogLevel(Level.NONE)public class LogLevelTests {

@Testpublic void alwaysPasses() { /* no - op */ }

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 40

@Testpublic void alwaysPasses() { /* no - op */ }

@Test@LogLevel(Level.INFO)public void alwaysFails() {fail("always fails");

}}

Page 41: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

LoggingTestExecutionListener (annotation) (1)

public class LoggingTestExecutionListener extendsAbstractTestExecutionListener {

// modified prepare, before, and after methods

private void logInfo(TestContext context, String msg) {if ( getLogLevel(context).ordinal() >= INFO.ordinal() ) {

System.out.println("INFO: " + msg);}

}

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 41

}

private void logError(TestContext context, String msg) {if (getLogLevel(context).ordinal() >= ERROR.ordinal()) {

System.err.println("ERROR: " + msg);}

}

// …

Page 42: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

LoggingTestExecutionListener (annotation) (2)

private Level getLogLevel (TestContext context) {Level level = null;final Class<LogLevel> clazz = LogLevel.class;final Method testMethod = context. getTestMethod ();

if (testMethod != null)level = testMethod.isAnnotationPresent(clazz) ?

testMethod.getAnnotation(clazz).value() : null;

if (level == null) {

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 42

if (level == null) {final Class<?> testClass = context. getTestClass ();level = testClass.isAnnotationPresent(clazz) ?

testClass.getAnnotation(clazz).value() : null;}

if (level == null)level = (Level) AnnotationUtils.getDefaultValue (clazz);

return level;}

Page 43: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

Demo: @LogLevel-based Logging TestExecutionListener

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

Page 44: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

PetClinic Sample Application

• Completely revised for Spring 2.5

• Annotation-driven configuration

• Annotation-driven @MVC controllers– focus on simple form handling

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 44

– focus on simple form handling

• Annotation-driven tests using the TestContext framework

Page 45: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

Demo: PetClinic

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

Page 46: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

Summary

The Spring TestContext Framework

• Provides generic unit and integration testing support

• Is agnostic of the actual testing framework– JUnit 3.8, JUnit 4.4, TestNG 5.5

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 46

– JUnit 3.8, JUnit 4.4, TestNG 5.5

• Embraces Java 5 annotations for configuration

• Provides a consistent yet enhanced feature set with an easy migration path for legacy code

• Is extensible, flexible, and easier to use

Page 47: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

Further Resources

• Updated PetClinic sample application

• Spring test suite

• Spring Reference Manual

• Spring Forums

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 47

• Spring Forums

• blog.springsource.com

• www.springframework.org

Page 48: Spring 2 - GOTO Blogjaoo.dk/.../slides/SamBrannen_TestingbyExamplewithSpring.pdf · 2008-03-19 · Summary The Spring TestContext Framework • Provides generic unit and integration

Q&A

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

Sam Brannen

Senior Software Engineer

[email protected]