Introducing Spring Framework
Introducing Spring Framework Introducing Spring Framework
Chapter 7 ■ Testing Your Spring Application @Component @Profile("qa") public class FileSearchEngineService implements SearchEngine { } public List findByType(Type documentType) { throw new UnsupportedOperationException("QA Environment. Not yet implemented operation."); } public List listAll() { throw new UnsupportedOperationException("QA Environment. Not yet implemented operation."); } Now if you change the @ActiveProfiles to qa, as shown in Listing 7-3, and run it again, then you should get the following output: 2014-02-16 18:22:30,110 DEBUG [main] Using Spring Test fixtures: 2014-02-16 18:22:30,113 ERROR [main] QA Environment. Not yet implemented operation. The preceding output is the result of running the Profile as QA, and because you annotated the class FileSearchEngineService with @Profile("qa") the Spring test will use it and you will get that exception. With profiling, you can get even more detail. You can define a custom Profile by using environment variables; in other words, you can pass a variable name and if it matches with the value given, then the test method will run. For creating a custom Profile you need to implement the ProfileValueSource interface as shown in Listing 7-5. Listing 7-5. CustomProfile.java package com.apress.isf.spring.test.profile; import org.springframework.test.annotation.ProfileValueSource; public class CustomProfile implements ProfileValueSource { } public String get(String key) { if(key.equals("dev")) return "Development"; else if (key.equals("qa")) return "QA"; return null; } In Listing 7-5, you created a CustomProfile class that will help you to reach every method depending on a value passed in a special annotation, and as you can see, you have implemented the get method from the ProfileValueSource interface. Now let’s modify your XML and test class as shown in Listing 7-6 and Listing 7-7. 80
Chapter 7 ■ Testing Your Spring Application Listing 7-6. mydocuments-custom-profiles-context.xml You just removed the tag from Listing 7-6. Let’s look at Listing 7-7 to see what happens to the test. Listing 7-7. MyDocumentsWithCustomProfilesTest.java 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; import org.springframework.test.annotation.IfProfileValue; import org.springframework.test.annotation.ProfileValueSourceConfiguration; 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; import com.apress.isf.spring.test.profile.CustomProfile; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:META-INF/spring/mydocuments-custom-profiles-context.xml") @ProfileValueSourceConfiguration(CustomProfile.class) public class MyDocumentsWithCustomProfilesTest { private static final Logger log = LoggerFactory.getLogger(MyDocumentsWithCustomProfilesTest.class); 81
- Page 31 and 32: Chapter 3 ■ Applying Different Co
- 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: Chapter 7 ■ Testing Your Spring A
- Page 85 and 86: 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
Chapter 7 ■ Testing Your <strong>Spring</strong> Application<br />
@Component<br />
@Profile("qa")<br />
public class FileSearchEngineService implements SearchEngine {<br />
}<br />
public List findByType(Type documentType) {<br />
throw new UnsupportedOperationException("QA Environment. Not yet implemented operation.");<br />
}<br />
public List listAll() {<br />
throw new UnsupportedOperationException("QA Environment. Not yet implemented operation.");<br />
}<br />
Now if you change the @ActiveProfiles to qa, as shown in Listing 7-3, and run it again, then you should get the<br />
following output:<br />
2014-02-16 18:22:30,110 DEBUG [main] Using <strong>Spring</strong> Test fixtures:<br />
2014-02-16 18:22:30,113 ERROR [main] QA Environment. Not yet implemented operation.<br />
The preceding output is the result of running the Profile as QA, and because you annotated the class<br />
FileSearchEngineService with @Profile("qa") the <strong>Spring</strong> test will use it and you will get that exception.<br />
With profiling, you can get even more detail. You can define a custom Profile by using environment variables; in<br />
other words, you can pass a variable name and if it matches with the value given, then the test method will run. For<br />
creating a custom Profile you need to implement the ProfileValueSource interface as shown in Listing 7-5.<br />
Listing 7-5. CustomProfile.java<br />
package com.apress.isf.spring.test.profile;<br />
import org.springframework.test.annotation.ProfileValueSource;<br />
public class CustomProfile implements ProfileValueSource {<br />
}<br />
public String get(String key) {<br />
if(key.equals("dev"))<br />
return "Development";<br />
else if (key.equals("qa"))<br />
return "QA";<br />
return null;<br />
}<br />
In Listing 7-5, you created a CustomProfile class that will help you to reach every method depending on<br />
a value passed in a special annotation, and as you can see, you have implemented the get method from the<br />
ProfileValueSource interface. Now let’s modify your XML and test class as shown in Listing 7-6 and Listing 7-7.<br />
80