예제에서 볼 수 있듯이 문서 유형과 문서에 관한 모든 데이터를 XML 파일에 두고 있다. 아마도 더 나은 방법이 있을테지만, 당장은 유닛 테스트를 작성해보자. 예제 3-7은 수정된 유닛 테스트를 보여준다.
예제 3-7 MyDocumentsTest.java
package com.apress.isf.spring.test;
import static org.junit.Assert.*;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.apress.isf.java.model.Document;
import com.apress.isf.java.model.Type;
import com.apress.isf.java.service.SearchEngine;
public class MyDocumentsTest {
private ClassPathXmlApplicationContext context;
private SearchEngine engine;
private Type webType;
@Before
public void setup() {
context = new ClassPathXmlApplicationContext(
"META-INF/spring/mydocuments-context.xml");
engine = context.getBean(SearchEngine.class);
webType = context.getBean("webType",Type.class);
}
@Test
public void testWithSpringFindByType() {
List<Document> documents = engine.findByType(webType);
assertNotNull(documents);
assertTrue(documents.size() == 1);
assertEquals(webType.getName(), documents.get(0).getType().getName());
assertEquals(webType.getDesc(), documents.get(0).getType().getDesc());
assertEquals(webType.getExtension(),
documents.get(0).getType().getExtension());
}
@Test
public void testWithSpringListAll() {
List<Document> documents = engine.listAll();
assertNotNull(documents);
assertTrue(documents.size() == 4);
}
}
예제 3-7의 setup() 메서드(이 메서드는 이 클래스에 있는 각 메서드가 실행되기 전에 실행된다)에서는 ClassPathXmlApplicationContext 클래스를 사용한다. 이 클래스는 모든 인스턴스를 생성하고 여러분이 필요할 때 사용할 수 있게 준비하여 스프링 컨테이너를 실행한다.