2014-12-18 2 views
1

몇 가지 속성을 주입하는 클래스가 @Service입니다. 주입 프로세스가 올바르게 작동 했으므로 junit 테스트를 작성하고 싶습니다.JUnit을 사용하여 Spring에서 단일 서비스를 테스트하는 방법은 무엇입니까?

내 전체 응용 프로그램 컨텍스트 구성을로드 할 필요없이 JUnit 테스트를 어떻게 정의 할 수 있습니까?

@Service 
public class MyService { 
    @Value("${test.property}") 
    private String value; 

    public String getValue() { return value; } 
} 

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration 
public class MyTest { 
    @Autowired 
    private MyService service; 

    @Test 
    public void test() { 
     assertNotNull(service.getValue()); 
    } 
} 

하지만이 실행할 때 : 당신은 당신이 당신의 시험에 @Autowired 주석 필드를 주입하는 곳에서 콩 파일을 지정하지 않은

java.lang.IllegalStateException: Neither GenericXmlContextLoader nor AnnotationConfigContextLoader was able to detect defaults, and no ApplicationContextInitializers were declared for context configuration 
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.processContextConfiguration(AbstractDelegatingSmartContextLoader.java:208) 
    at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:348) 
    at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:305) 
+0

속성은 ** ** 어디에서 주입해야합니까? –

+0

application.properties 파일 – membersound

+0

제 질문은 _how_ 및/또는 _why_의 의미에서 더 많이 얻었습니다. –

답변

1

합니다.

@ContextConfiguration(locations = { 
"classpath:applicationContext.xml", 
"classpath:junit-applicationContext.xml" 
}) 
-2

그것은 봄 물건을 테스트 할 수있는 모듈이 unitils

에서보세요 : 당신은 무언가 같이해야한다. 서비스가 데이터베이스와 상호 작용하는 경우에도 매우 편리합니다.

관련 문제