몇 가지 속성을 주입하는 클래스가 @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)
속성은 ** ** 어디에서 주입해야합니까? –
application.properties 파일 – membersound
제 질문은 _how_ 및/또는 _why_의 의미에서 더 많이 얻었습니다. –