더북(TheBook)

이제 예제 7-3과 같이 테스트 클래스를 생성하자. 이 테스트에서는 새로운 애너테이션인 @ActiveProfiles를 사용할 것이다. @ActiveProfiles 애너테이션은 스프링 컨테이너에 사용할 프로파일과 해당 프로파일에서 사용하기 위해 인스턴스를 생성할 빈을 알려준다.

 예제 7-3 MyDocumentsWithProfilesTest.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.Test;

import org.junit.runner.RunWith;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.test.context.ActiveProfiles;

import org.springframework.test.context.ContextConfiguration;

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

 

import com.apress.isf.java.model.Document;

import com.apress.isf.java.model.Type;

import com.apress.isf.java.service.SearchEngine;

 

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration("classpath:META-INF/spring/mydocuments-profiles-context.xml")

@ActiveProfiles("dev")

public class MyDocumentsWithProfilesTest {

 

private static final Logger log =

LoggerFactory.getLogger(MyDocumentsWithProfilesTest.class);

 

@Autowired

private SearchEngine engine;

@Autowired

private Type webType;

 

@Test

public void testUsingSpringTestWithProfiles() {

try {

log.debug("Using Spring Test fixtures:");

 

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());

 

documents = engine.listAll();

assertNotNull(documents);

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

} catch(Exception ex) {

log.error(ex.getMessage());

}

}

 

}


다음 명령으로 예제 7-3에 있는 테스트를 수행하면 이전 테스트(예제 7-1 참고)와 동일한 결과를 얻는다.

 

$ gradle :ch07:test

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