51Testing软件测试论坛

标题: springboot进行单元测试报错 [打印本页]

作者: 测试积点老人    时间: 2019-12-9 10:47
标题: springboot进行单元测试报错
springboot进行单元测试报错,求各位大佬指点迷津
报错信息:

  1. <p>java.lang.IllegalStateException: Failed to load ApplicationContext</p><p>at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125)
  2. at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108)
  3. at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:190)
  4. at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:132)
  5. at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:246)
  6. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227)
  7. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
  8. at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
  9. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
  10. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246)
  11. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
  12. at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
  13. at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
  14. at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
  15. at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
  16. at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
  17. at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
  18. at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
  19. at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
  20. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
  21. at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
  22. at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
  23. at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
  24. at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
  25. at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)</p>
复制代码



作者: 梦想家    时间: 2019-12-10 09:29
没有加载ApplicationContext.xml
作者: bellas    时间: 2019-12-10 09:39
是不是版本不对?
作者: qqq911    时间: 2019-12-10 11:42
环境没配置好
作者: jingzizx    时间: 2019-12-10 12:25
感觉像配置有问题
作者: litingting0214    时间: 2019-12-10 14:42
重新配置一下环境
作者: 海海豚    时间: 2019-12-10 15:12
https://blog.csdn.net/baofengyu90/article/details/86500016  参考下这个
作者: Miss_love    时间: 2019-12-14 14:27
支持下
作者: 旺达    时间: 2020-5-22 11:08
其实就是 Springboot 项目的启动类 和 单元测试类的包名必须相同,如下所示:
[attach]128841[/attach]
红框部分的包名必须相同,我的不相同,所以报错了,改为同一个就好了。


作者: 旺达    时间: 2020-5-22 11:10
下面附单元测试的代码:
单元测试pom:
<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
</dependency>
定义一个测试基类:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public abstract class BaseTest {

}
而后其他的测试类继承该类就可以了,非常方便。
业务测试类:
public class SettleFacadeTest extends BaseTest{
       
        @Resource
        ISettleQueryFacade settleQueryFacade;

        @Test
        public void queryDetailList(){
            List<SettleDetail> detailList = settleQueryFacade.queryDetailList();         
        }
}







欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2