gpt4 book ai didi

spring - 运行集成测试时无法加载 ApplicationContext

转载 作者:行者123 更新时间:2023-12-04 16:10:41 33 4
gpt4 key购买 nike

我正在尝试运行 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

另外,我使用的是原型(prototype),您可以看到带注释的 Configuration类(class) 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目标。

最佳答案

您缺少 @ContextConfiguration(classes = ...) 的上下文定义测试中的注释。如 classes您可以定义单个配置或整个生产应用程序上下文(包括所有其他配置)。仅声明您需要的配置类的好处是整个测试的引导速度更快。

注意:Spring 测试缓存其指定的应用程序上下文。如果您必须使用整个配置运行 9/10 测试,与声明一组新的上下文配置相比,再次使用整个配置将花费更少的时间。但是您应该致力于为您的集成测试获得一个小的配置足迹,以便您可以专注于您正在工作的域切片,而不必处理或维护其他上下文配置。

通常,测试使用 SpringJUnit4ClassRunner 运行期望有一个应用程序上下文来运行。
进一步阅读:Link to the spring docs

关于spring - 运行集成测试时无法加载 ApplicationContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42127297/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com