25.02.2015 Views

Introducing Spring Framework

Introducing Spring Framework

Introducing Spring Framework

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

Listing 7-2 shows that the tag has an attribute of profile where you can define any environment or any<br />

string as a profile, so in this file you declared two profiles: dev and qa. So you have declared the profile dev that has<br />

the engine and documentDAO beans, and the profile qa that has only the documentDAO bean. But how does the <strong>Spring</strong><br />

container know what profile to use? You are going to see that with your test class (see Listing 7-3). Also, you added the<br />

that will search for any annotated class within the com.apress.isf.spring.service<br />

package. This tag will play an important part for your next tests.<br />

Now, let’s create your test class, as shown in Listing 7-3. In this test you will use a new annotation: @ActiveProfiles.<br />

This annotation will tell the <strong>Spring</strong> container which profile to use and the beans to instantiate for their use.<br />

Listing 7-3. MyDocumentsWithProfilesTest.java<br />

package com.apress.isf.spring.test;<br />

import static org.junit.Assert.assertEquals;<br />

import static org.junit.Assert.assertNotNull;<br />

import static org.junit.Assert.assertTrue;<br />

import java.util.List;<br />

import org.junit.Test;<br />

import org.junit.runner.RunWith;<br />

import org.slf4j.Logger;<br />

import org.slf4j.LoggerFactory;<br />

import org.springframework.beans.factory.annotation.Autowired;<br />

import org.springframework.test.context.ActiveProfiles;<br />

import org.springframework.test.context.ContextConfiguration;<br />

import org.springframework.test.context.junit4.<strong>Spring</strong>JUnit4ClassRunner;<br />

import com.apress.isf.java.model.Document;<br />

import com.apress.isf.java.model.Type;<br />

import com.apress.isf.java.service.SearchEngine;<br />

78

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

Saved successfully!

Ooh no, something went wrong!