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

As you can see, you just implemented the findByType and you used a private storage method that will retrieve<br />

some documents and their types. Also, you implemented the listAll method that directly uses the storage method.<br />

Note that this is a naïve example to start showing some of the <strong>Spring</strong> <strong>Framework</strong> features. Next, you need to test what<br />

you have done. So let’s create a unit test using JUnit (see Listing 2-5).<br />

Listing 2-5. MyDocumentsTest.java<br />

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

import java.util.List;<br />

import org.junit.Test;<br />

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

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

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

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

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

public class MyDocumentsTest {<br />

private SearchEngine engine = new MySearchEngine();<br />

@Test<br />

public void testFindByType() {<br />

Type documentType = new Type();<br />

documentType.setName("WEB");<br />

documentType.setDesc("Web Link");<br />

documentType.setExtension(".url");<br />

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

assertNotNull(documents);<br />

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

assertEquals(documentType.getName(),<br />

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

assertEquals(documentType.getDesc(),<br />

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

assertEquals(documentType.getExtension(),<br />

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

}<br />

}<br />

@Test<br />

public void testListAll() {<br />

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

assertNotNull(documents);<br />

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

}<br />

17

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

Saved successfully!

Ooh no, something went wrong!