启动测试类,首先报错:java.lang.NullPointerException 空指针异常
代码如图:
后来加上:
@SpringBootTest(classes = GulimallProductApplication.class)
空指针异常解决:
但这个问题解决后,新的问题又来了:
运行GulimallProductApplicationTest报错:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-09-23 12:42:56.308 ERROR 22272 --- [ main] o.s.boot.SpringApplication : Application run failedorg.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'categoryController' method
public com.atguigu.common.utils.R com.atguigu.gulimall.product.controller.CategoryController.update(com.atguigu.gulimall.product.entity.CategoryEntity)
to { /update}: There is already 'attrAttrgroupRelationController' bean method
有方法说应该先把主模块启动类启动后,再运行测试类。但运行GulimallProductApplication依旧报错:
2021-09-23 12:42:56.309 ERROR 22272 --- [ main] o.s.test.context.TestContextManager : Caught exception while allowing TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@4b14c583] to prepare test instance [com.atguigu.gulimall.product.GulimallProductApplicationTests@28da7d11]
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125) ~[spring-test-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108) ~[spring-test-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:190) ~[spring-test-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:132) ~[spring-test-5.1.9.RELEASE.jar:5.1.9.RELEASE]
解决方法:
找了好久突然发现自己粗心把代码生成器中@RequestMapping注解不小心注释掉了
于是在每个controller层的文件下都添加@RequestMapping注解,问题解决。
@RequestMapping("product/attr")