Introducing Spring Framework

Introducing Spring Framework Introducing Spring Framework

25.02.2015 Views

Chapter 7 ■ Testing Your Spring Application import org.springframework.test.annotation.Repeat; import org.springframework.test.annotation.Timed; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.apress.isf.java.model.Document; import com.apress.isf.java.model.Type; import com.apress.isf.java.service.SearchEngine; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:META-INF/spring/mydocuments-context.xml") public class MyDocumentsMoreAnnotationsTest { private static final Logger log = LoggerFactory.getLogger(MyDocumentsMoreAnnotationsTest.class); @Autowired private SearchEngine engine; @Autowired private Type webType; @Timed(millis=2000) @Test public void testUsingSpringTimedAnnotationTest() throws InterruptedException { log.debug("Using Spring Test fixtures:"); List documents = engine.findByType(webType); assertNotNull(documents); assertTrue(documents.size() == 1); assertEquals(webType.getName(),documents.get(0).getType().getName()); assertEquals(webType.getDesc(),documents.get(0).getType().getDesc()); assertEquals(webType.getExtension(),documents.get(0).getType().getExtension()); Thread.sleep(500); } documents = engine.listAll(); assertNotNull(documents); assertTrue(documents.size() == 4); } @Repeat(10) @Test public void testUsingSpringRepeatedAnnotationTest() { log.debug("This message should be printed 10 times.."); } If you run the test (see Listing 7-8) with gradle :ch07:test 84

Chapter 7 ■ Testing Your Spring Application you should get the following output: 2014-02-16 20:08:53,797 DEBUG [main] SearchEngineService created: com.apress.isf.spring.service. SearchEngineService@3553d71d 2014-02-16 20:08:53,843 DEBUG [main] Document DAO set: com.apress.isf.spring.data. DocumentRepository@31da28a 2014-02-16 20:08:53,860 DEBUG [main] Using Spring Test fixtures: 2014-02-16 20:08:53,861 DEBUG [main] Start Params: Type Definition: Name: WEB Description: Web Link Extension: .url 2014-02-16 20:08:53,861 DEBUG [main] Start Params: 2014-02-16 20:08:53,861 DEBUG [main] Start Params: 2014-02-16 20:08:53,861 DEBUG [main] End Result:[Lcom.apress.isf.java.model. Document;@42704d54 2014-02-16 20:08:53,861 DEBUG [main] End Result: [com.apress.isf.java.model. Document@5ae4e7df, com.apress.isf.java.model.Document@6a331017, com.apress.isf.java.model. Document@3e658c79, com.apress.isf.java.model.Document@7ce97bef] 2014-02-16 20:08:53,861 DEBUG [main] End Result: [com.apress.isf.java.model. Document@7ce97bef] 2014-02-16 20:08:54,361 DEBUG [main] Start Params: 2014-02-16 20:08:54,362 DEBUG [main] Start Params: 2014-02-16 20:08:54,362 DEBUG [main] End Result:[Lcom.apress.isf.java.model. Document;@44c7c7fa 2014-02-16 20:08:54,362 DEBUG [main] End Result: [com.apress.isf.java.model. Document@5ae4e7df, com.apress.isf.java.model.Document@6a331017, com.apress.isf.java.model. Document@3e658c79, com.apress.isf.java.model.Document@7ce97bef] 2014-02-16 20:08:54,366 DEBUG [main] This message should be printed 10 times.. 2014-02-16 20:08:54,367 DEBUG [main] This message should be printed 10 times.. 2014-02-16 20:08:54,367 DEBUG [main] This message should be printed 10 times.. 2014-02-16 20:08:54,368 DEBUG [main] This message should be printed 10 times.. 2014-02-16 20:08:54,368 DEBUG [main] This message should be printed 10 times.. 2014-02-16 20:08:54,369 DEBUG [main] This message should be printed 10 times.. 2014-02-16 20:08:54,369 DEBUG [main] This message should be printed 10 times.. 2014-02-16 20:08:54,370 DEBUG [main] This message should be printed 10 times.. 2014-02-16 20:08:54,370 DEBUG [main] This message should be printed 10 times.. 2014-02-16 20:08:54,370 DEBUG [main] This message should be printed 10 times.. So the @Timed annotation will fail if the test takes more than the milliseconds parameter provided. Maybe you want to test some process time that should not take too long or maybe you need to test the response of the external call to another system that should not exceed the time you specified. And the @Repeat annotation will repeat the test as many times as the number provided. You can use the @Repeat in a test when you need to measure the average of process executing by repeating the same test several times. Summary In this chapter, I covered the Spring Framework Testing extension. You discovered how easy it is to use it. In previous chapters, you instantiated the ApplicationContext with the ClassPathXMLApplicationContext class and every time you needed a bean you had to call the getBean method. But thanks to the annotations provided by the Spring Framework Test extension, you can have better readable code and faster development because you can focus on the test cases. If you download the companion source code, you can find other versions of the classes, which convert to the Spring Test extension, even the Groovy classes. 85

Chapter 7 ■ Testing Your <strong>Spring</strong> Application<br />

you should get the following output:<br />

2014-02-16 20:08:53,797 DEBUG [main] SearchEngineService created: com.apress.isf.spring.service.<br />

SearchEngineService@3553d71d<br />

2014-02-16 20:08:53,843 DEBUG [main] Document DAO set: com.apress.isf.spring.data.<br />

DocumentRepository@31da28a<br />

2014-02-16 20:08:53,860 DEBUG [main] Using <strong>Spring</strong> Test fixtures:<br />

2014-02-16 20:08:53,861 DEBUG [main] Start Params: Type Definition:<br />

Name: WEB<br />

Description: Web Link<br />

Extension: .url<br />

2014-02-16 20:08:53,861 DEBUG [main] Start Params:<br />

2014-02-16 20:08:53,861 DEBUG [main] Start Params:<br />

2014-02-16 20:08:53,861 DEBUG [main] End Result:[Lcom.apress.isf.java.model.<br />

Document;@42704d54<br />

2014-02-16 20:08:53,861 DEBUG [main] End Result: [com.apress.isf.java.model.<br />

Document@5ae4e7df, com.apress.isf.java.model.Document@6a331017, com.apress.isf.java.model.<br />

Document@3e658c79, com.apress.isf.java.model.Document@7ce97bef]<br />

2014-02-16 20:08:53,861 DEBUG [main] End Result: [com.apress.isf.java.model.<br />

Document@7ce97bef]<br />

2014-02-16 20:08:54,361 DEBUG [main] Start Params:<br />

2014-02-16 20:08:54,362 DEBUG [main] Start Params:<br />

2014-02-16 20:08:54,362 DEBUG [main] End Result:[Lcom.apress.isf.java.model.<br />

Document;@44c7c7fa<br />

2014-02-16 20:08:54,362 DEBUG [main] End Result: [com.apress.isf.java.model.<br />

Document@5ae4e7df, com.apress.isf.java.model.Document@6a331017, com.apress.isf.java.model.<br />

Document@3e658c79, com.apress.isf.java.model.Document@7ce97bef]<br />

2014-02-16 20:08:54,366 DEBUG [main] This message should be printed 10 times..<br />

2014-02-16 20:08:54,367 DEBUG [main] This message should be printed 10 times..<br />

2014-02-16 20:08:54,367 DEBUG [main] This message should be printed 10 times..<br />

2014-02-16 20:08:54,368 DEBUG [main] This message should be printed 10 times..<br />

2014-02-16 20:08:54,368 DEBUG [main] This message should be printed 10 times..<br />

2014-02-16 20:08:54,369 DEBUG [main] This message should be printed 10 times..<br />

2014-02-16 20:08:54,369 DEBUG [main] This message should be printed 10 times..<br />

2014-02-16 20:08:54,370 DEBUG [main] This message should be printed 10 times..<br />

2014-02-16 20:08:54,370 DEBUG [main] This message should be printed 10 times..<br />

2014-02-16 20:08:54,370 DEBUG [main] This message should be printed 10 times..<br />

So the @Timed annotation will fail if the test takes more than the milliseconds parameter provided. Maybe you<br />

want to test some process time that should not take too long or maybe you need to test the response of the external<br />

call to another system that should not exceed the time you specified. And the @Repeat annotation will repeat the test<br />

as many times as the number provided. You can use the @Repeat in a test when you need to measure the average of<br />

process executing by repeating the same test several times.<br />

Summary<br />

In this chapter, I covered the <strong>Spring</strong> <strong>Framework</strong> Testing extension. You discovered how easy it is to use it. In previous<br />

chapters, you instantiated the ApplicationContext with the ClassPathXMLApplicationContext class and every<br />

time you needed a bean you had to call the getBean method. But thanks to the annotations provided by the <strong>Spring</strong><br />

<strong>Framework</strong> Test extension, you can have better readable code and faster development because you can focus on the<br />

test cases. If you download the companion source code, you can find other versions of the classes, which convert to<br />

the <strong>Spring</strong> Test extension, even the Groovy classes.<br />

85

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!