我一直在玩JUnit 5 http://junit.org/junit5/ and 弹簧测试junit5 https://github.com/sbrannen/spring-test-junit5。然后我尝试使用嵌套测试 http://junit.org/junit5/docs/current/user-guide/#writing-tests-nested功能,但我的测试失败了。当我使用 gradle 直接从命令行运行时也会发生这种情况。
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = TestConfig.class)
@DisplayName("In cat club")
public class NestedCatTests {
@Autowired
Cat cat;
@Autowired
List<Cat> cats;
@Test
@DisplayName("Catbert is the cat")
void catWasAutowired() {
assertEquals(
"Catbert",
cat.getName()
);
}
@Nested
@DisplayName("Catbert")
class IsMemberOfClub {
@Test
@DisplayName("is a member")
void isMemberOf() {
assertTrue(cats.contains(cat));
}
}
}
我得到以下异常:
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.junit.jupiter.SpringExtension.postProcessTestInstance(SpringExtension.java:91)
at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.lambda$null$1(ClassTestDescriptor.java:196)
at org.junit.jupiter.engine.descriptor.JupiterTestDescriptor.executeAndMaskThrowable(JupiterTestDescriptor.java:102)
...
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.lang.IllegalStateException: Neither GenericXmlContextLoader nor AnnotationConfigContextLoader was able to load an ApplicationContext from [MergedContextConfiguration@4816278d testClass = NestedCatTests.IsMemberOfClub, 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)
... 44 more
我有一个工作@嵌套测试示例 https://github.com/hantsy/spring-reactive-jwt-sample/blob/master/src/test/java/com/example/demo/IntegrationTests.java使用Spring Boot。
@SpringBootTest(webEnvironment = RANDOM_PORT)
@Slf4j
@DisplayName("API endpoints integration tests")
class IntegrationTests {
@LocalServerPort
private int port;
private WebTestClient client;
@Autowired
private JwtTokenProvider jwtTokenProvider;
@BeforeEach
private void setup() {
this.client = WebTestClient.bindToServer()
.baseUrl("http://localhost:" + port)
.build();
}
@Nested
@DisplayName("if user is not logged in")
class NotLoggedIn { ... }
@Nested
@DisplayName("if user logged as user")
class LoggedAsUser{ ... }
//...
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)