더북(TheBook)

다음으로, 유닛 테스트를 수정하고 컨테이너를 구동하는 몇 가지 스프링 프레임워크 클래스를 사용하기 시작할 것이다. 컨테이너는 여러분이 만든 클래스들의 인스턴스를 생성하며, 애플리케이션의 동작을 알 것이다. 예제 2-8은 유닛 테스트 클래스를 보여준다.

 예제 2-8 MyDocumentsWithSpringTest.java

package com.apress.isf.spring.test;

 

import static org.junit.Assert.assertEquals;

import static org.junit.Assert.assertNotNull;

import static org.junit.Assert.assertTrue;

 

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 MyDocumentsWithSpringTest {

 

private ClassPathXmlApplicationContext context;

private SearchEngine engine;

private Type documentType;

 

@Before

public void setup() {

context = new ClassPathXmlApplicationContext(

"META-INF/spring/mydocuments-context.xml");

engine = context.getBean(SearchEngine.class);

documentType = context.getBean(Type.class);

}

 

@Test

public void testWithSpringFindByType() {

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

assertNotNull(documents);

assertTrue(documents.size() == 1);

assertEquals(documentType.getName(), documents.get(0).getType().getName());

assertEquals(documentType.getDesc(), documents.get(0).getType().getDesc());

assertEquals(documentType.getExtension(),

documents.get(0).getType().getExtension());

}

 

@Test

public void testWithSpringListAll() {

List<Document> documents = engine.listAll();

assertNotNull(documents);

assertTrue(documents.size() == 4);

}

 

}


신간 소식 구독하기
뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.