我尝试运行mvn integration-test
阶段,并且在执行集成测试时出现Failed to load ApplicationContext
错误(单元测试正确执行)。我正在使用SpringJUnit4ClassRunner
类使用。
这是完整的堆栈跟踪:
2017-02-09 03:22:15.705 [main] ERROR o.s.t.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@5c072e3f] to prepare test instance [com.dentilax.app.accountservice.AccountServiceIT@768b970c]
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.IllegalStateException: Neither GenericXmlContextLoader nor AnnotationConfigContextLoader was able to load an ApplicationContext from [MergedContextConfiguration@71623278 testClass = AccountServiceIT, locations = '{}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextCustomizers = set[[empty]], contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]].
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:263)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
... 25 common frames omitted
另外,我使用原型,您可以看到带注释的Configuration
类here。我做错了什么?
这是我的测试类:
@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
public class PatientServiceIT {
private static final String EMAIL = "account@test.com";
private static final String NAME = "test";
private static final String SURNAMES = "account";
private static final String PASSWORD = "testaccount";
private static final String POSTAL_CODE = "15002";
private static final String MOBILE_NUMBER = "694749217";
@Autowired
private AccountRepository accountRepository;
@Autowired
private PatientRepository patientRepository;
@Autowired
private PatientService patientService;
private PatientDetails createPatientDetails() {
return new PatientDetails(EMAIL, PASSWORD, NAME, SURNAMES, MOBILE_NUMBER, POSTAL_CODE);
}
private Account createPatient() {
Account patientAccount = new Account(EMAIL, PASSWORD, NAME, SURNAMES, Role.ROLE_DENTIST);
Patient patient = new Patient(POSTAL_CODE, MOBILE_NUMBER);
patientAccount.setPatient(patient);
return patientAccount;
}
@Test
public void savePatient() {
// call
Patient patient = patientService.save(createPatientDetails());
// assert
assertEquals(patient.getAccount(), createPatient());
}
}
PS:我对integration-test
目标起诉maven失败,并确保test
目标。
答案 0 :(得分:2)
您在测试中缺少使用@ContextConfiguration(classes = ...)
注释的上下文定义。作为classes
,您可以定义单个配置或整个生产应用程序上下文(包括所有其他上下文)。声明所需的配置类的好处是测试的整个引导速度更快。
注意:Spring测试缓存其指定的应用程序上下文。如果你必须使用整个配置运行9/10测试,那么再次使用整个配置所需的时间比声明一组新的上下文配置要少。但是,您应该为集成测试获得一个小的配置占用空间,这样您就可以专注于您正在使用的域切片,而不必处理或维护其他上下文配置。
在使用SpringJUnit4ClassRunner
的一般测试中,期望运行应用程序上下文。
进一步阅读:Link to the spring docs