Introducing Spring Framework
Introducing Spring Framework Introducing Spring Framework
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
- Page 35 and 36: Chapter 3 ■ Applying Different Co
- Page 37 and 38: Chapter 3 ■ Applying Different Co
- Page 39 and 40: Chapter 3 ■ Applying Different Co
- Page 41 and 42: Spring Stereotypes Chapter 3 ■ Ap
- Page 43 and 44: Chapter 3 ■ Applying Different Co
- Page 45 and 46: Chapter 3 ■ Applying Different Co
- Page 47 and 48: Chapter 3 ■ Applying Different Co
- Page 49 and 50: Chapter 4 ■ Using Beans Scopes pu
- Page 51 and 52: Chapter 4 ■ Using Beans Scopes Th
- Page 53 and 54: Chapter 4 ■ Using Beans Scopes @A
- Page 55 and 56: Chapter 4 ■ Using Beans Scopes do
- Page 57 and 58: Chapter 5 ■ Working with Collecti
- Page 59 and 60: Chapter 5 ■ Working with Collecti
- Page 61 and 62: Chapter 5 ■ Working with Collecti
- Page 63 and 64: Chapter 5 ■ Working with Collecti
- Page 65 and 66: Chapter 6 ■ Using Resource Files
- Page 67 and 68: Chapter 6 ■ Using Resource Files
- Page 69 and 70: Chapter 6 ■ Using Resource Files
- Page 71 and 72: Chapter 6 ■ Using Resource Files
- Page 73 and 74: Chapter 6 ■ Using Resource Files
- Page 75 and 76: Chapter 6 ■ Using Resource Files
- Page 77 and 78: Chapter 7 Testing Your Spring Appli
- Page 79 and 80: Chapter 7 ■ Testing Your Spring A
- Page 81 and 82: Chapter 7 ■ Testing Your Spring A
- Page 83 and 84: Chapter 7 ■ Testing Your Spring A
- Page 85: Chapter 7 ■ Testing Your Spring A
- Page 89 and 90: Chapter 8 Give Advice to Your Sprin
- Page 91 and 92: Chapter 8 ■ Give Advice to Your S
- Page 93 and 94: Chapter 8 ■ Give Advice to Your S
- Page 95 and 96: Chapter 8 ■ Give Advice to Your S
- Page 97 and 98: Chapter 8 ■ Give Advice to Your S
- Page 99 and 100: Chapter 8 ■ Give Advice to Your S
- Page 101 and 102: Chapter 8 ■ Give Advice to Your S
- Page 103 and 104: Chapter 8 ■ Give Advice to Your S
- Page 105 and 106: Chapter 8 ■ Give Advice to Your S
- Page 107 and 108: Chapter 8 ■ Give Advice to Your S
- Page 109 and 110: Chapter 8 ■ Give Advice to Your S
- Page 111 and 112: Chapter 9 ■ Adding Persistence to
- Page 113 and 114: Chapter 9 ■ Adding Persistence to
- Page 115 and 116: Chapter 9 ■ Adding Persistence to
- Page 117 and 118: Chapter 9 ■ Adding Persistence to
- Page 119 and 120: Chapter 9 ■ Adding Persistence to
- Page 121 and 122: Chapter 9 ■ Adding Persistence to
- Page 123 and 124: Chapter 9 ■ Adding Persistence to
- Page 125 and 126: Chapter 9 ■ Adding Persistence to
- Page 127 and 128: Chapter 9 ■ Adding Persistence to
- Page 129 and 130: Chapter 9 ■ Adding Persistence to
- Page 131 and 132: Chapter 9 ■ Adding Persistence to
- Page 133 and 134: Chapter 10 ■ Showing Your Spring
- Page 135 and 136: Chapter 10 ■ Showing Your Spring
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