thomas ferris nicolaisen - objectware. sprint testcontext framework thomas ferris nicolaisen...

20
Thomas Ferris Nicolaisen - Objectware

Upload: howard-wood

Post on 24-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Thomas Ferris Nicolaisen - Objectware. Sprint TestContext Framework Thomas Ferris Nicolaisen Objectware JavAcademy desember 2007

Thomas Ferris Nicolaisen - Objectware

Page 2: Thomas Ferris Nicolaisen - Objectware. Sprint TestContext Framework Thomas Ferris Nicolaisen Objectware JavAcademy desember 2007

Thomas Ferris Nicolaisen - Objectware

Sprint TestContext Framework

Thomas Ferris Nicolaisen

Objectware JavAcademy desember 2007

Page 3: Thomas Ferris Nicolaisen - Objectware. Sprint TestContext Framework Thomas Ferris Nicolaisen Objectware JavAcademy desember 2007

Thomas Ferris Nicolaisen - Objectware

Page 4: Thomas Ferris Nicolaisen - Objectware. Sprint TestContext Framework Thomas Ferris Nicolaisen Objectware JavAcademy desember 2007

Thomas Ferris Nicolaisen - Objectware

”A completely revised integration test framework, with first-class

support for JUnit 4 and TestNG ”

Page 5: Thomas Ferris Nicolaisen - Objectware. Sprint TestContext Framework Thomas Ferris Nicolaisen Objectware JavAcademy desember 2007

Thomas Ferris Nicolaisen - Objectware

Spring

Spring TestSpring TestContext Framework

Page 6: Thomas Ferris Nicolaisen - Objectware. Sprint TestContext Framework Thomas Ferris Nicolaisen Objectware JavAcademy desember 2007

Thomas Ferris Nicolaisen - Objectware

mv spring-mock spring-test

Page 7: Thomas Ferris Nicolaisen - Objectware. Sprint TestContext Framework Thomas Ferris Nicolaisen Objectware JavAcademy desember 2007

Thomas Ferris Nicolaisen - Objectware

Page 8: Thomas Ferris Nicolaisen - Objectware. Sprint TestContext Framework Thomas Ferris Nicolaisen Objectware JavAcademy desember 2007

Thomas Ferris Nicolaisen - Objectware

Page 9: Thomas Ferris Nicolaisen - Objectware. Sprint TestContext Framework Thomas Ferris Nicolaisen Objectware JavAcademy desember 2007

Thomas Ferris Nicolaisen - Objectware

Page 10: Thomas Ferris Nicolaisen - Objectware. Sprint TestContext Framework Thomas Ferris Nicolaisen Objectware JavAcademy desember 2007

Thomas Ferris Nicolaisen - Objectware

So how do we do it?pom.xml:

(junit 4.4) spring-core spring-beans

spring-test

spring-tx

265 + 451 + 216 + 174 = 1,1 MB

Page 11: Thomas Ferris Nicolaisen - Objectware. Sprint TestContext Framework Thomas Ferris Nicolaisen Objectware JavAcademy desember 2007

Thomas Ferris Nicolaisen - Objectware

How?

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations = { "testSpring.xml" })

public class TestJUnit4WithSpringTestContextFramework {

...

...

Page 12: Thomas Ferris Nicolaisen - Objectware. Sprint TestContext Framework Thomas Ferris Nicolaisen Objectware JavAcademy desember 2007

Thomas Ferris Nicolaisen - Objectware

Repeat@Test

@Repeat(20)

public void repeatManyTimes() throws Exception {

counter++;

int numberOfTimesToRepeat = this.

getClass().

getMethod("repeatManyTimes").

getAnnotation(Repeat.class).value();

log.trace("Running for the "+counter+"th time");

if (counter > numberOfTimesToRepeat) {

fail(

"Should not repeat more than 100 times. counter is "+counter);

}

}

Page 13: Thomas Ferris Nicolaisen - Objectware. Sprint TestContext Framework Thomas Ferris Nicolaisen Objectware JavAcademy desember 2007

Thomas Ferris Nicolaisen - Objectware

2007-12-06 00:17:17,737 DEBUG [no.objectware.jafs.TestJUnit4WithSpringTestContextFramework] - <Logging works.>2007-12-06 00:17:18,237 TRACE [no.objectware.jafs.TestJUnit4WithSpringTestContextFramework] - <Running for the 1th time>2007-12-06 00:17:18,253 TRACE [no.objectware.jafs.TestJUnit4WithSpringTestContextFramework] - <Running for the 2th time>2007-12-06 00:17:18,253 TRACE [no.objectware.jafs.TestJUnit4WithSpringTestContextFramework] - <Running for the 3th time>2007-12-06 00:17:18,253 TRACE [no.objectware.jafs.TestJUnit4WithSpringTestContextFramework] - <Running for the 4th time>2007-12-06 00:17:18,253 TRACE [no.objectware.jafs.TestJUnit4WithSpringTestContextFramework] - <Running for the 5th time>2007-12-06 00:17:18,268 TRACE [no.objectware.jafs.TestJUnit4WithSpringTestContextFramework] - <Running for the 6th time>2007-12-06 00:17:18,268 TRACE [no.objectware.jafs.TestJUnit4WithSpringTestContextFramework] - <Running for the 7th time>2007-12-06 00:17:18,268 TRACE [no.objectware.jafs.TestJUnit4WithSpringTestContextFramework] - <Running for the 8th time>2007-12-06 00:17:18,268 TRACE [no.objectware.jafs.TestJUnit4WithSpringTestContextFramework] - <Running for the 9th time>2007-12-06 00:17:18,268 TRACE [no.objectware.jafs.TestJUnit4WithSpringTestContextFramework] - <Running for the 10th time>2007-12-06 00:17:18,284 TRACE [no.objectware.jafs.TestJUnit4WithSpringTestContextFramework] - <Running for the 11th time>2007-12-06 00:17:18,284 TRACE [no.objectware.jafs.TestJUnit4WithSpringTestContextFramework] - <Running for the 12th time>2007-12-06 00:17:18,284 TRACE [no.objectware.jafs.TestJUnit4WithSpringTestContextFramework] - <Running for the 13th time>2007-12-06 00:17:18,284 TRACE [no.objectware.jafs.TestJUnit4WithSpringTestContextFramework] - <Running for the 14th time>2007-12-06 00:17:18,300 TRACE [no.objectware.jafs.TestJUnit4WithSpringTestContextFramework] - <Running for the 15th time>2007-12-06 00:17:18,300 TRACE [no.objectware.jafs.TestJUnit4WithSpringTestContextFramework] - <Running for the 16th time>2007-12-06 00:17:18,300 TRACE [no.objectware.jafs.TestJUnit4WithSpringTestContextFramework] - <Running for the 17th time>2007-12-06 00:17:18,300 TRACE [no.objectware.jafs.TestJUnit4WithSpringTestContextFramework] - <Running for the 18th time>2007-12-06 00:17:18,331 TRACE [no.objectware.jafs.TestJUnit4WithSpringTestContextFramework] - <Running for the 19th time>2007-12-06 00:17:18,362 TRACE [no.objectware.jafs.TestJUnit4WithSpringTestContextFramework] - <Running for the 20th time>

Page 14: Thomas Ferris Nicolaisen - Objectware. Sprint TestContext Framework Thomas Ferris Nicolaisen Objectware JavAcademy desember 2007

Thomas Ferris Nicolaisen - Objectware

Timeout

/**

* This test will fail if it takes more than 50 ms to execute.

*/

@Test

@Timed(millis=50)

public void doSomethingBeforeTimeout() throws Exception{

int tryChangingMeTo50 = 20;

Thread.sleep(tryChangingMeTo50);

}

Page 15: Thomas Ferris Nicolaisen - Objectware. Sprint TestContext Framework Thomas Ferris Nicolaisen Objectware JavAcademy desember 2007

Thomas Ferris Nicolaisen - Objectware

Caching av contexts

@Test

@DirtiesContext

public void doSomethingThatDirtiesTheSpringContext() {

// can't really come up with anything that screws the context here..

}

Page 16: Thomas Ferris Nicolaisen - Objectware. Sprint TestContext Framework Thomas Ferris Nicolaisen Objectware JavAcademy desember 2007

Thomas Ferris Nicolaisen - Objectware

Springs exception testing

@Test

@ExpectedException(RuntimeException.class)

public void doSomethingThatThrowsAnException() {

throw new RuntimeException();

}

Page 17: Thomas Ferris Nicolaisen - Objectware. Sprint TestContext Framework Thomas Ferris Nicolaisen Objectware JavAcademy desember 2007

Thomas Ferris Nicolaisen - Objectware

JUnit4 exception testing

/**

* This test expects exceptions the JUnit4 way.

* It is preferred over the way above.

*/

@Test(expected = RuntimeException.class)

public void doSomethingElseThatThrowsAnException() {

throw new RuntimeException();

}

Page 18: Thomas Ferris Nicolaisen - Objectware. Sprint TestContext Framework Thomas Ferris Nicolaisen Objectware JavAcademy desember 2007

Thomas Ferris Nicolaisen - Objectware

Test Profiles@Test

@IfProfileValue(name = "mock", value = "true")

public void thisWillOnlyBeRunIfTheSystemVariableMockIsTrue() {

assertEquals("true", System.getProperty("mock"));

}

@ProfileValueSourceConfiguration(MyProfileValueSource.class)

@IfProfileValue(name="test-groups", values={"unit-tests", "integration-tests"})

public void testProcessWhichRunsForUnitOrIntegrationTestGroups() {

// some logic that should run only for unit and integration test groups

}

Page 19: Thomas Ferris Nicolaisen - Objectware. Sprint TestContext Framework Thomas Ferris Nicolaisen Objectware JavAcademy desember 2007

Thomas Ferris Nicolaisen - Objectware

Then the real fun begins..

• Use Spring’s data factories – Inject with @Autowired

• @ContextConfiguration

• Transaction testing

• Kan bygges videre på med egne @TestExecutionListeners

• Uendelige muligheter!

Page 20: Thomas Ferris Nicolaisen - Objectware. Sprint TestContext Framework Thomas Ferris Nicolaisen Objectware JavAcademy desember 2007

Thomas Ferris Nicolaisen - Objectware

Q&A?