25.02.2015 Views

Introducing Spring Framework

Introducing Spring Framework

Introducing Spring Framework

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

@RunWith(<strong>Spring</strong>JUnit4ClassRunner.class)<br />

@ContextConfiguration("classpath:META-INF/spring/mydocuments-profiles-context.xml")<br />

@ActiveProfiles("dev")<br />

public class MyDocumentsWithProfilesTest {<br />

private static final Logger log = LoggerFactory.getLogger(MyDocumentsWithProfilesTest.class);<br />

@Autowired<br />

private SearchEngine engine;<br />

@Autowired<br />

private Type webType;<br />

@Test<br />

public void testUsing<strong>Spring</strong>TestWithProfiles() {<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().getExtension());<br />

}<br />

}<br />

documents = engine.listAll();<br />

assertNotNull(documents);<br />

assertTrue(documents.size() == 4);<br />

}catch(Exception ex){<br />

log.error(ex.getMessage());<br />

}<br />

If you run the test in Listing 7-3 with<br />

gradle :ch07:test<br />

you should get the same result as the previous test (see Listing 7-1). Now, what about the QA environment? Let’s<br />

create a new class by adding the @Profile annotation, as shown in Listing 7-4. This annotation will mark your<br />

FileSearchEngineService class to be instantiated only when the qa profile is activated.<br />

Listing 7-4. FileSearchEngineService.java<br />

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

import java.util.List;<br />

import org.springframework.context.annotation.Profile;<br />

import org.springframework.stereotype.Component;<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 />

79

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

Saved successfully!

Ooh no, something went wrong!