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 />

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

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

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

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

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

public class MyDocumentsTest {<br />

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

@Autowired<br />

private SearchEngine engine;<br />

@Autowired<br />

private Type webType;<br />

@Test<br />

public void testUsing<strong>Spring</strong>Test() {<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 />

In Listing 7-1, you are using the @RunWith annotation. This annotation is just a JUnit annotation that is telling it<br />

to use the <strong>Spring</strong>JUnit4ClassRunner class to run the unit tests instead of the JUnit engine. This will allow it to use all<br />

the features of the <strong>Spring</strong> integration testing such as having access to the <strong>Spring</strong> container. You are also using the @<br />

ContextConfiguration annotation, which is part of the <strong>Spring</strong> testing fixtures. This will help to load the beans into the<br />

<strong>Spring</strong> container by passing the location of your XML configuration and have them (the beans) ready and accessible<br />

when using the context or, even better, when using the well-known @Autowired annotation.<br />

If you run this test (see Listing 7-1) using the gradle tool<br />

gradle :ch07:test<br />

then you should have the following output:<br />

2014-02-16 17:46:45,744 DEBUG [main] SearchEngineService created: com.apress.isf.spring.service.<br />

SearchEngineService@60d1a23c<br />

2014-02-16 17:46:45,815 DEBUG [main] Document DAO set: com.apress.isf.spring.data.<br />

DocumentRepository@5e2842b6<br />

2014-02-16 17:46:45,839 DEBUG [main] Using <strong>Spring</strong> Test fixtures:<br />

2014-02-16 17:46:45,839 DEBUG [main] Start Params: Type Definition:<br />

Name: WEB<br />

Description: Web Link<br />

76

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

Saved successfully!

Ooh no, something went wrong!