Introducing Spring Framework
Introducing Spring Framework Introducing Spring Framework
Chapter 7 ■ Testing Your Spring Application @Autowired private SearchEngine engine; @Autowired private Type webType; @IfProfileValue(name = "environment", values = "dev") @Test public void testUsingSpringTestWithCustomProfilesX() { try{ 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()); } documents = engine.listAll(); assertNotNull(documents); assertTrue(documents.size() == 4); }catch(Exception ex){ log.error(ex.getMessage()); } @IfProfileValue(name = "os.name", values = "Unix") @Test public void testUsingSpringTestWithCustomProfilesY() { try{ log.debug("Using Spring Test fixtures on Unix:"); //More Testing } } }catch(Exception ex){ log.error(ex.getMessage()); } Listing 7-7 shows two new annotations: @ProfileValueSourceConfiguration that points to your CustomProfile class and tells the unit test that it will be required in order to perform some business logic, and the @IfProfileValue annotation where you passed a name (key) and the value returned. If you run the test, you are going to see the following output: 2014-02-16 19:55:11,903 DEBUG [main] SearchEngineService created: com.apress.isf.spring.service. SearchEngineService@3b897b54 2014-02-16 19:55:11,993 DEBUG [main] Document DAO set: com.apress.isf.spring.data. DocumentRepository@2c24be25 2014-02-16 19:55:12,026 DEBUG [main] Using Spring Test fixtures: 2014-02-16 19:55:12,026 DEBUG [main] Start Params: Type Definition: Name: WEB 82
Chapter 7 ■ Testing Your Spring Application Description: Web Link Extension: .url 2014-02-16 19:55:12,026 DEBUG [main] Start Params: 2014-02-16 19:55:12,026 DEBUG [main] Start Params: 2014-02-16 19:55:12,026 DEBUG [main] End Result:[Lcom.apress.isf.java.model. Document;@b29b889 2014-02-16 19:55:12,027 DEBUG [main] End Result: [com.apress.isf.java.model. Document@60123018, com.apress.isf.java.model.Document@4a13a8ee, com.apress.isf.java.model. Document@50b7c740, com.apress.isf.java.model.Document@517a6426] 2014-02-16 19:55:12,027 DEBUG [main] End Result: [com.apress.isf.java.model. Document@517a6426] 2014-02-16 19:55:12,027 DEBUG [main] Start Params: 2014-02-16 19:55:12,027 DEBUG [main] Start Params: 2014-02-16 19:55:12,027 DEBUG [main] End Result:[Lcom.apress.isf.java.model. Document;@3a452494 2014-02-16 19:55:12,027 DEBUG [main] End Result: [com.apress.isf.java.model. Document@60123018, com.apress.isf.java.model.Document@4a13a8ee, com.apress.isf.java.model. Document@50b7c740, com.apress.isf.java.model.Document@517a6426] 2014-02-16 19:55:12,029 DEBUG [main] Using Spring Test fixtures on Unix: The two tests run because the correct value is returned in each. ■ ■Note If you removed the @ProfileValueSourceConfiguration, then the unit test will be based on the System.getProperty(propertyKey). This means that if you add the Environment variable os.name with value Unix, then the test method testUsingSpringTestWithCustomProfilesY will be executed. More Test Annotations The Spring Framework provides even more annotations to test with. Let’s see some examples. Listing 7-8 shows the @Timed and @Repeat annotations that can be used only when using the SpringJUnit4ClassRunner or JUnit support classes. Listing 7-8. MyDocumentsMoreAnnotationsTest.java (@Timed, @Repeat) package com.apress.isf.spring.test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.util.List; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; 83
- Page 33 and 34: Chapter 3 ■ Applying Different Co
- 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: Chapter 7 ■ Testing Your Spring A
- Page 87 and 88: 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
Chapter 7 ■ Testing Your <strong>Spring</strong> Application<br />
@Autowired<br />
private SearchEngine engine;<br />
@Autowired<br />
private Type webType;<br />
@IfProfileValue(name = "environment", values = "dev")<br />
@Test<br />
public void testUsing<strong>Spring</strong>TestWithCustomProfilesX() {<br />
try{<br />
log.debug("Using <strong>Spring</strong> Test fixtures:");<br />
List documents = engine.findByType(webType);<br />
assertNotNull(documents);<br />
assertTrue(documents.size() == 1);<br />
assertEquals(webType.getName(),documents.get(0).getType().getName());<br />
assertEquals(webType.getDesc(),documents.get(0).getType().getDesc());<br />
assertEquals(webType.getExtension(),documents.get(0).getType().<br />
getExtension());<br />
}<br />
documents = engine.listAll();<br />
assertNotNull(documents);<br />
assertTrue(documents.size() == 4);<br />
}catch(Exception ex){<br />
log.error(ex.getMessage());<br />
}<br />
@IfProfileValue(name = "os.name", values = "Unix")<br />
@Test<br />
public void testUsing<strong>Spring</strong>TestWithCustomProfilesY() {<br />
try{<br />
log.debug("Using <strong>Spring</strong> Test fixtures on Unix:");<br />
//More Testing<br />
}<br />
}<br />
}catch(Exception ex){<br />
log.error(ex.getMessage());<br />
}<br />
Listing 7-7 shows two new annotations: @ProfileValueSourceConfiguration that points to your CustomProfile<br />
class and tells the unit test that it will be required in order to perform some business logic, and the @IfProfileValue<br />
annotation where you passed a name (key) and the value returned. If you run the test, you are going to see the<br />
following output:<br />
2014-02-16 19:55:11,903 DEBUG [main] SearchEngineService created: com.apress.isf.spring.service.<br />
SearchEngineService@3b897b54<br />
2014-02-16 19:55:11,993 DEBUG [main] Document DAO set: com.apress.isf.spring.data.<br />
DocumentRepository@2c24be25<br />
2014-02-16 19:55:12,026 DEBUG [main] Using <strong>Spring</strong> Test fixtures:<br />
2014-02-16 19:55:12,026 DEBUG [main] Start Params: Type Definition:<br />
Name: WEB<br />
82