Introducing Spring Framework

Introducing Spring Framework Introducing Spring Framework

25.02.2015 Views

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

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

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

Saved successfully!

Ooh no, something went wrong!