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 4 ■ Using Beans Scopes<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 />

public class MyDocumentsTest {<br />

private ClassPathXmlApplicationContext context;<br />

private SearchEngine engine;<br />

private Type webType;<br />

@Before<br />

public void setup(){<br />

context = new ClassPathXmlApplicationContext("META-INF/spring/mydocuments-context.xml");<br />

}<br />

@Test<br />

public void testAll() {<br />

engine = context.getBean(SearchEngine.class);<br />

webType = context.getBean("webType",Type.class);<br />

List documents = engine.findByType(webType);<br />

assertNotNull(documents);<br />

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

assertEquals(webType.getName()<br />

,documents.get(0).getType().getName());<br />

assertEquals(webType.getDesc()<br />

,documents.get(0).getType().getDesc());<br />

assertEquals(webType.getExtension()<br />

,documents.get(0).getType().getExtension());<br />

engine = context.getBean(SearchEngine.class);<br />

}<br />

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

assertNotNull(documents);<br />

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

}<br />

In Listing 4-2, you have simply created one test method that contains the call of the getBean method. In this case,<br />

you want to know how the <strong>Spring</strong> container can deal with the creation of the SearchEngine and the injection of your<br />

DocumentDAO implementation.<br />

If you run Listing 4-2 without modifying the XML file (mydocuments-context.xml), then you should see the<br />

following output:<br />

$ gradle -Dtest.single=MyDocumentsTest test<br />

DEBUG [main] ServiceSearchEngine created: com.apress.isf.spring.service.ServiceSearchEngine@1aee419f<br />

DEBUG [main] Document DAO set: com.apress.isf.spring.data.DocumentRepository@53b97d73<br />

47

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

Saved successfully!

Ooh no, something went wrong!