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 ”
Thomas Ferris Nicolaisen - Objectware Spring Spring Test Spring TestContext Framework
Thomas Ferris Nicolaisen - Objectware mv spring-mock spring-test
Thomas Ferris Nicolaisen - Objectware
So how do we do it? pom.xml: (junit 4.4)spring-corespring-beans spring-test spring-tx = 1,1 MB
Thomas Ferris Nicolaisen - = { "testSpring.xml" }) public class TestJUnit4WithSpringTestContextFramework {...
Thomas Ferris Nicolaisen - 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); }
Thomas Ferris Nicolaisen - Objectware :17:17,737 DEBUG [no.objectware.jafs.TestJUnit4WithSpringTestContextFramework] :17:18,237 TRACE [no.objectware.jafs.TestJUnit4WithSpringTestContextFramework] :17:18,253 TRACE [no.objectware.jafs.TestJUnit4WithSpringTestContextFramework] :17:18,268 TRACE [no.objectware.jafs.TestJUnit4WithSpringTestContextFramework] :17:18,284 TRACE [no.objectware.jafs.TestJUnit4WithSpringTestContextFramework] :17:18,300 TRACE [no.objectware.jafs.TestJUnit4WithSpringTestContextFramework] :17:18,331 TRACE [no.objectware.jafs.TestJUnit4WithSpringTestContextFramework] :17:18,362 TRACE [no.objectware.jafs.TestJUnit4WithSpringTestContextFramework] -
Thomas Ferris Nicolaisen - Objectware Timeout /** * This test will fail if it takes more than 50 ms to public void doSomethingBeforeTimeout() throws Exception{ int tryChangingMeTo50 = 20; Thread.sleep(tryChangingMeTo50); }
Thomas Ferris Nicolaisen - Objectware Caching public void doSomethingThatDirtiesTheSpringContext() { // can't really come up with anything that screws the context here.. }
Thomas Ferris Nicolaisen - Objectware Springs public void doSomethingThatThrowsAnException() { throw new RuntimeException(); }
Thomas Ferris Nicolaisen - Objectware JUnit4 exception testing /** * This test expects exceptions the JUnit4 way. * It is preferred over the way above. = RuntimeException.class) public void doSomethingElseThatThrowsAnException() { throw new RuntimeException(); }
Thomas Ferris Nicolaisen - Objectware = "mock", value = "true") public void thisWillOnlyBeRunIfTheSystemVariableMockIsTrue() { assertEquals("true", values={"unit-tests", "integration-tests"}) public void testProcessWhichRunsForUnitOrIntegrationTestGroups() { // some logic that should run only for unit and integration test groups }
Thomas Ferris Nicolaisen - Objectware Then the real fun begins.. Use Spring’s data factories Transaction testing Kan bygges videre på med Uendelige muligheter!
Thomas Ferris Nicolaisen - Objectware Q&A?