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 2 ■ Working with Classes and Dependencies<br />

In this case, it’s the com.apress.isf.java.service.MySearchEngine class and this class will be used by the <strong>Spring</strong><br />

<strong>Framework</strong> to create the instance you need. Next, you defined a “documentType” bean that will create a new instance of<br />

a com.apress.isf.java.model.Type with its values. This will be similar to using the new keyword and the use of setters.<br />

Next, you are going to modify your unit test, and you are going to start using some of the <strong>Spring</strong> <strong>Framework</strong><br />

classes that will start up the container. It will instantiate your classes and it will know about the behavior of your<br />

application. Listing 2-8 shows your unit test class.<br />

Listing 2-8. MyDocumentsTestWith<strong>Spring</strong>.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.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 MyDocumentsWith<strong>Spring</strong>Test {<br />

private ClassPathXmlApplicationContext context;<br />

private SearchEngine engine;<br />

private Type documentType;<br />

@Before<br />

public void setup(){<br />

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

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

documentType = context.getBean(Type.class);<br />

}<br />

@Test<br />

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

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

assertNotNull(documents);<br />

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

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

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

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

}<br />

20

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

Saved successfully!

Ooh no, something went wrong!