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 3 ■ Applying Different Configurations<br />

Also, take a look on the bean with id "doc2". You are embedding a new bean as value; this is allowed by the<br />

<strong>Spring</strong> configuration rules.<br />

As you can see, you are putting all your data about the types and documents in your XML file. Maybe there is a<br />

better way, but for now let’s create your unit test. Listing 3-7 shows your modified unit test.<br />

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

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

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

import java.util.List;<br />

import org.junit.Before;<br />

import org.junit.Test;<br />

import org.springframework.context.support.ClassPathXmlApplicationContext;<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 />

}<br />

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

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

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

}<br />

@Test<br />

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

@Test<br />

public void testWith<strong>Spring</strong>ListAll() {<br />

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

assertNotNull(documents);<br />

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

}

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

Saved successfully!

Ooh no, something went wrong!