2016-12-09 77 views
8

多次聲明遵循這裏的官方文檔: http://docs.spring.io/spring-boot/docs/1.4.0.M2/reference/htmlsingle/#Testing春季啓動1.4測試:配置錯誤:發現@BootstrapWith

我想測試這樣我的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); 
    } 
} 

由於在doc說:

The search algorithm works up from the package that contains the test until it finds a @SpringBootApplication or @SpringBootConfiguration annotated class. As long as you’ve structure your code in a sensible way your main configuration is usually found.

我已妥善安排我的代碼(至少我認爲):

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.gatcbiotech.blueberry.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個小時,沒有任何運氣。

謝謝。

+8

'@ SpringBootTest'和'@ WebMvcTest'是互斥的......要麼你測試整個應用程序或您測試切片但不是兩者。 –

+1

@ M.Deinum:它的大提示,謝謝 – Roxy

回答

6

當春季測試找不到主配置類時,會發生此異常。 嘗試將@ContextConfiguration anootation添加到您的測試類。按照有關詳細信息,春試機制的文檔(部分Detecting test configuration

我的例子測試類是這樣的:

@RunWith(SpringRunner.class) 
@ContextConfiguration(classes=Application.class) 
@WebMvcTest(MyController.class) 
public class MyConrollerTests { 
    ... 
} 
+0

工作完美! – Kaushal28