SpringBoot Maven工程打包时(执行run as maven install命令),报以下错误:
1[INFO] Scanning for projects... 2[INFO] 3[INFO] -----------------------< com.swyx:swyx_form_api >----------------------- 4[INFO] Building swyx_form_api 0.0.1-SNAPSHOT 5[INFO] --------------------------------[ jar ]--------------------------------- 6[INFO] 7[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ swyx_form_api --- 8[INFO] Using 'UTF-8' encoding to copy filtered resources. 9[INFO] Copying 1 resource 10[INFO] Copying 1 resource 11[INFO] 12[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ swyx_form_api --- 13[INFO] Nothing to compile - all classes are up to date 14[INFO] 15[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ swyx_form_api --- 16[INFO] Using 'UTF-8' encoding to copy filtered resources. 17[INFO] skip non existing resourceDirectory H:\swyx\codes\swyx_form\swyx_form_api\src\test\resources 18[INFO] 19[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ swyx_form_api --- 20[INFO] Nothing to compile - all classes are up to date 21[INFO] 22[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ swyx_form_api --- 23[INFO] 24[INFO] ------------------------------------------------------- 25[INFO] T E S T S 26[INFO] ------------------------------------------------------- 2706:53:52.377 [main] DEBUG org.springframework.test.context.junit4.SpringJUnit4ClassRunner - SpringJUnit4ClassRunner constructor called with [class com.swyx.tools.SwyxFormApplicationTests] 2806:53:52.384 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate] 2906: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)] 3006: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] 3106: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 3206: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 3306: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 3406: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}. 3506: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. 3606: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] 3706:53:52.957 [main] DEBUG org.springframework.test.context.junit4.SpringJUnit4ClassRunner - SpringJUnit4ClassRunner constructor called with [class com.swyx.tools.SwyxFormApplicationTests] 3806:53:52.958 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate] 3906: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)] 4006: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] 4106: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 4206: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 4306: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 4406: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}. 4506: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. 4606: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] 47[INFO] Running com.swyx.tools.SwyxFormApplicationTests 48[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.02 s <<< FAILURE! - in com.swyx.tools.SwyxFormApplicationTests 49[ERROR] initializationError Time elapsed: 0.001 s <<< ERROR! 50java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test 51 52[INFO] 53[INFO] Results: 54[INFO] 55[ERROR] Errors: 56[ERROR] SwyxFormApplicationTests ? IllegalState Unable to find a @SpringBootConfigurat... 57[INFO] 58[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0 59[INFO] 60[INFO] ------------------------------------------------------------------------ 61[INFO] BUILD FAILURE 62[INFO] ------------------------------------------------------------------------ 63[INFO] Total time: 8.412 s 64[INFO] Finished at: 2019-06-04T06:53:53+08:00 65[INFO] ------------------------------------------------------------------------ 66[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. 67[ERROR] 68[ERROR] Please refer to H:\swyx\codes\swyx_form\swyx_form_api\target\surefire-reports for the individual test results. 69[ERROR] Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream. 70[ERROR] -> [Help 1] 71[ERROR] 72[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. 73[ERROR] Re-run Maven using the -X switch to enable full debug logging. 74[ERROR] 75[ERROR] For more information about the errors and possible solutions, please read the following articles: 76[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
意为在test类中指定启动类配置
现有的test启动类:
1@RunWith(SpringRunner.class) 2@SpringBootTest 3public class SwyxFormApplicationTests { 4 5 @Test 6 public void contextLoads() { 7 } 8 9}
修改后test启动类:
1@RunWith(SpringRunner.class) 2@SpringBootTest 3@ContextConfiguration(classes = SwyxFormApplication.class) 4public class SwyxFormApplicationTests { 5 6 @Test 7 public void contextLoads() { 8 } 9 10}
或
1@RunWith(SpringRunner.class) 2@SpringBootTest(classes = SwyxFormApplication.class) 3public class SwyxFormApplicationTests { 4 5 @Test 6 public void contextLoads() { 7 } 8 9}