Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

SpringBoot Maven工程打包时(执行run as maven install命令),报以下错误:

[INFO] Scanning for projects...
[INFO] 
[INFO] -----------------------< com.swyx:swyx_form_api >-----------------------
[INFO] Building swyx_form_api 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ swyx_form_api ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ swyx_form_api ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ swyx_form_api ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory H:\swyx\codes\swyx_form\swyx_form_api\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ swyx_form_api ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ swyx_form_api ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
06:53:52.377 [main] DEBUG org.springframework.test.context.junit4.SpringJUnit4ClassRunner - SpringJUnit4ClassRunner constructor called with [class com.swyx.tools.SwyxFormApplicationTests]
06:53:52.384 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]
06:53:52.399 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating BootstrapContext using constructor [public org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,org.springframework.test.context.CacheAwareContextLoaderDelegate)]
06:53:52.469 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating TestContextBootstrapper for test class [com.swyx.tools.SwyxFormApplicationTests] from class [org.springframework.boot.test.context.SpringBootTestContextBootstrapper]
06:53:52.505 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.swyx.tools.SwyxFormApplicationTests], using SpringBootContextLoader
06:53:52.512 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.swyx.tools.SwyxFormApplicationTests]: class path resource [com/swyx/tools/SwyxFormApplicationTests-context.xml] does not exist
06:53:52.513 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.swyx.tools.SwyxFormApplicationTests]: class path resource [com/swyx/tools/SwyxFormApplicationTestsContext.groovy] does not exist
06:53:52.514 [main] INFO org.springframework.test.context.support.AbstractContextLoader - Could not detect default resource locations for test class [com.swyx.tools.SwyxFormApplicationTests]: no resource found for suffixes {-context.xml, Context.groovy}.
06:53:52.515 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils - Could not detect default configuration classes for test class [com.swyx.tools.SwyxFormApplicationTests]: SwyxFormApplicationTests does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
06:53:52.602 [main] DEBUG org.springframework.test.context.support.ActiveProfilesUtils - Could not find an 'annotation declaring class' for annotation type [org.springframework.test.context.ActiveProfiles] and class [com.swyx.tools.SwyxFormApplicationTests]
06:53:52.957 [main] DEBUG org.springframework.test.context.junit4.SpringJUnit4ClassRunner - SpringJUnit4ClassRunner constructor called with [class com.swyx.tools.SwyxFormApplicationTests]
06:53:52.958 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]
06:53:52.958 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating BootstrapContext using constructor [public org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,org.springframework.test.context.CacheAwareContextLoaderDelegate)]
06:53:52.960 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating TestContextBootstrapper for test class [com.swyx.tools.SwyxFormApplicationTests] from class [org.springframework.boot.test.context.SpringBootTestContextBootstrapper]
06:53:52.962 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.swyx.tools.SwyxFormApplicationTests], using SpringBootContextLoader
06:53:52.963 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.swyx.tools.SwyxFormApplicationTests]: class path resource [com/swyx/tools/SwyxFormApplicationTests-context.xml] does not exist
06:53:52.964 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.swyx.tools.SwyxFormApplicationTests]: class path resource [com/swyx/tools/SwyxFormApplicationTestsContext.groovy] does not exist
06:53:52.965 [main] INFO org.springframework.test.context.support.AbstractContextLoader - Could not detect default resource locations for test class [com.swyx.tools.SwyxFormApplicationTests]: no resource found for suffixes {-context.xml, Context.groovy}.
06:53:52.966 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils - Could not detect default configuration classes for test class [com.swyx.tools.SwyxFormApplicationTests]: SwyxFormApplicationTests does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
06:53:52.970 [main] DEBUG org.springframework.test.context.support.ActiveProfilesUtils - Could not find an 'annotation declaring class' for annotation type [org.springframework.test.context.ActiveProfiles] and class [com.swyx.tools.SwyxFormApplicationTests]
[INFO] Running com.swyx.tools.SwyxFormApplicationTests
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.02 s <<< FAILURE! - in com.swyx.tools.SwyxFormApplicationTests
[ERROR] initializationError  Time elapsed: 0.001 s  <<< ERROR!
java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

[INFO] 
[INFO] Results:
[INFO] 
[ERROR] Errors: 
[ERROR]   SwyxFormApplicationTests ? IllegalState Unable to find a @SpringBootConfigurat...
[INFO] 
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  8.412 s
[INFO] Finished at: 2019-06-04T06:53:53+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test (default-test) on project swyx_form_api: There are test failures.
[ERROR] 
[ERROR] Please refer to H:\swyx\codes\swyx_form\swyx_form_api\target\surefire-reports for the individual test results.
[ERROR] Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

意为在test类中指定启动类配置

现有的test启动类:

@RunWith(SpringRunner.class)
@SpringBootTest
public class SwyxFormApplicationTests {

    @Test
    public void contextLoads() {
    }

}

修改后test启动类:

@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = SwyxFormApplication.class)
public class SwyxFormApplicationTests {

    @Test
    public void contextLoads() {
    }

}

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SwyxFormApplication.class)
public class SwyxFormApplicationTests {

    @Test
    public void contextLoads() {
    }

}

猜你喜欢

转载自www.cnblogs.com/ShouWangYiXin/p/10971288.html