通过遵循此处的官方文档:http://docs.spring.io/spring-boot/docs/1.4.0.M2/reference/htmlsingle/#Testing
我想测试我的一种REST API方法:
@RunWith(SpringRunner.class)
@WebMvcTest(LoginController.class)
@SpringBootTest(classes = Application.class)
public class AuthorizationServiceTest {
@Autowired
private TestRestTemplate restTemplate;
@Test
public void test() {
Object returnedObject=his.restTemplate.getForObject("/login", Object.class);
}
}
如文档中所述:
搜索算法从包含测试的软件包中进行工作直到找到@SpringBootApplication或@SpringBootConfiguration带注释的类。只要您合理地构建代码通常可以找到您的主要配置的方式。
我已经正确构造了我的代码(至少我认为):
AuthorizationService:位于com.xxx.yyy.zzz.authorization包中;
AuthorizationServiceTest:在com.xxx.yyy.zzz.authorizationTest包中;
我正在获取此异常(完整跟踪):
java.lang.IllegalStateException: Configuration error: found multiple declarations of @BootstrapWith for test class [com.orangeraid.rasberry.gateway.authorizationTest.AuthorizationServiceTest]: [@org.springframework.test.context.BootstrapWith(value=class org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTestContextBootstrapper), @org.springframework.test.context.BootstrapWith(value=class org.springframework.boot.test.context.SpringBootTestContextBootstrapper)]
at org.springframework.test.context.BootstrapUtils.resolveExplicitTestContextBootstrapper(BootstrapUtils.java:155)
at org.springframework.test.context.BootstrapUtils.resolveTestContextBootstrapper(BootstrapUtils.java:126)
at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:105)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:152)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:143)
at org.springframework.test.context.junit4.SpringRunner.<init>(SpringRunner.java:49)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:84)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:70)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:43)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
[请帮助我,我已经花了2-3个小时没有运气。
谢谢。
当弹簧测试找不到主配置类时,会发生此异常。尝试将@ContextConfiguration修饰添加到测试类中。遵循弹簧测试文档以获取更多详细信息(Detecting test configuration部分)
我的示例Test类是这样的:
@RunWith(SpringRunner.class)
@ContextConfiguration(classes=Application.class)
@WebMvcTest(MyController.class)
public class MyConrollerTests {
...
}
只需删除@SpringBootTest,一切正常。我在使用@SpringBootTest和@DataJpaTest时遇到了相同的问题,当我将pom.xml父springboot版本升级到2.1.0时,出现了此错误,如下所示。当我使用2.0.5版时,此错误没有出现。
@RunWith(SpringRunner.class)
//@SpringBootTest//(classes = KalahApplication.class)
// DataJpaTest supports rollback after running every test case
@DataJpaTest
public class GameRepositoryTest {
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
我知道回答这个问题为时已晚,但是将来可能会帮助某个人,所以...我遇到了同样的问题,经过一番研究,我发现如果存在[C0 ]。因此只需移除@WebMvcTest
,@SpringBootTest
就会处理剩下的一切。
这是因为您同时声明了@WebMvcTest和@SpringBootTest,我通过删除@SpringBootTest解决了相同的问题