1 / 20

Sprint TestContext Framework

Sprint TestContext Framework. Thomas Ferris Nicolaisen Objectware JavAcademy desember 2007. ”A completely revised integration test framework, with first-class support for JUnit 4 and TestNG ”. Spring. Spring Test. Spring TestContext Framework. mv spring-mock spring-test.

verdi
Télécharger la présentation

Sprint TestContext Framework

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Thomas Ferris Nicolaisen - Objectware

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

  3. Thomas Ferris Nicolaisen - Objectware

  4. ”A completely revised integration test framework, with first-class support for JUnit 4 and TestNG ” Thomas Ferris Nicolaisen - Objectware

  5. Spring Spring Test Spring TestContext Framework Thomas Ferris Nicolaisen - Objectware

  6. mv spring-mock spring-test Thomas Ferris Nicolaisen - Objectware

  7. Thomas Ferris Nicolaisen - Objectware

  8. Thomas Ferris Nicolaisen - Objectware

  9. Thomas Ferris Nicolaisen - Objectware

  10. So how do we do it? pom.xml: (junit 4.4) spring-core spring-beans spring-tx spring-test 265 + 451 + 216 + 174 = 1,1 MB Thomas Ferris Nicolaisen - Objectware

  11. How? @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "testSpring.xml" }) publicclass TestJUnit4WithSpringTestContextFramework { ... ... Thomas Ferris Nicolaisen - Objectware

  12. Repeat @Test @Repeat(20) publicvoid 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

  13. 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> Thomas Ferris Nicolaisen - Objectware

  14. Timeout /** *Thistestwillfailifittakesmorethan50mstoexecute. */ @Test @Timed(millis=50) publicvoid doSomethingBeforeTimeout() throws Exception{ int tryChangingMeTo50 = 20; Thread.sleep(tryChangingMeTo50); } Thomas Ferris Nicolaisen - Objectware

  15. Caching av contexts @Test @DirtiesContext publicvoid doSomethingThatDirtiesTheSpringContext() { // can't really come up with anything that screws the context here.. } Thomas Ferris Nicolaisen - Objectware

  16. Springs exception testing @Test @ExpectedException(RuntimeException.class) publicvoid doSomethingThatThrowsAnException() { thrownew RuntimeException(); } Thomas Ferris Nicolaisen - Objectware

  17. JUnit4 exception testing /** *ThistestexpectsexceptionstheJUnit4way. *It is preferred over the wayabove. */ @Test(expected = RuntimeException.class) publicvoid doSomethingElseThatThrowsAnException() { thrownew RuntimeException(); } Thomas Ferris Nicolaisen - Objectware

  18. Test Profiles @Test @IfProfileValue(name = "mock", value = "true") publicvoid thisWillOnlyBeRunIfTheSystemVariableMockIsTrue() { assertEquals("true", System.getProperty("mock")); } @ProfileValueSourceConfiguration(MyProfileValueSource.class) @IfProfileValue(name="test-groups", values={"unit-tests", "integration-tests"}) publicvoid testProcessWhichRunsForUnitOrIntegrationTestGroups() { // some logic that should run only for unit and integration test groups } Thomas Ferris Nicolaisen - Objectware

  19. 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! Thomas Ferris Nicolaisen - Objectware

  20. Q&A? Thomas Ferris Nicolaisen - Objectware

More Related