如何在Spring Boot应用程序中运行所有JUNIT测试?
我正在尝试生成我的Spring Boot应用程序的代码覆盖报告,但同时运行所有现有的单元测试面临一些问题。
目前,我有很多测试类(〜20+),并且在Intellij上单独运行时,它们可以正常运行,没有问题或例外。但是,当我创建从“所有目录”中构建和运行的JUNIT配置时,我的测试类中有70-80%失败并抛出相同的异常IllegalStateException:无法加载ApplicationContext
。
堆栈痕迹似乎暗示了依赖注入的一些问题。它遵循这种模式:
java.lang.illegalstateException:无法加载ApplicationContext ... org.springframework.beans.factory.beancreationexception:错误 用名称“ foo”创建bean:注入资源依赖性 失败的; org.springframework.beans.factory.beancreationexception:错误 用名称“ bar”创建bean:FactoryBean在对象上抛出了例外 创建org.springframework.beans.factory.beancreationexception: 使用名称“ Foobar”创建BEAN的错误:Bean的初始化失败;
我不确定这是否与@springboottest注释有关,该注释在所有测试类中都重复(我认为也许它多次旋转了该问题的应用程序 /上下文?
) m做错了或错误地配置了一些注释?如何在Intellij上运行所有JUNIT测试课程?
ps很抱歉缺乏上下文,因为这不是一个个人项目,我必须隐藏一些信息,而且我对Springboot和Junit的新手也很新。
I am trying to generate a code coverage report of my Spring Boot application but am facing some issues with running all the existing unit tests at once.
Currently, I have quite a number of test classes (~20+) and when run individually on IntelliJ, they work fine with no issues or exceptions. However, when I create a JUnit configuration that builds and runs from "All in directory", 70-80% of my test classes fail and throws the same exception IllegalStateException: Failed to load ApplicationContext
.
The stack trace seem to suggest some issues with the dependency injection. It follows this pattern:
java.lang.IllegalStateException: Failed to load ApplicationContext ...
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'foo': Injection of resource dependencies
failed; org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'bar': FactoryBean threw exception on object
creation org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'foobar': Initialization of bean failed;
I'm not sure if this has something to do with the @SpringBootTest annotation which is repeated throughout all the test classes (I'm thinking maybe it's spinning up the application / context multiple times resulting in this issue?)
I think perhaps I'm doing it wrongly or have configured some annotations wrongly? How can I run all the JUnit test classes on IntelliJ?
P.S Sorry for the lack of context as it's not a personal project and I have to hide some information, and also I'm very new to SpringBoot and JUnit.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在文件夹结构中的IDE中,转到test/java< - 右键单击,然后单击“在Java中运行测试” 在此处输入图像描述。
这就是我在Intellij中运行所有测试的方式。
我不知道您提到的错误,但是我遇到类似错误的时间是因为我需要设置环境变量。如果是这种情况,而不是单击“在Java中运行测试”,则转到“更多运行/调试 - > modify Run Configuration”,并在相应的字段中设置您的环境变量。
在此处输入图像描述
In the IDE in the folder structure go to test/java <-- right click and then click "Run test in java" enter image description here.
This is how I run all the tests in IntelliJ.
I don't know about the error you mention, but the times I've gotten a similar error is because I needed to set up environment variables. If that's the case instead of click on "Run test in java" go to "More Run/Debug --> Modify Run Configuration" and there set your environment variables in the corresponding field.
enter image description here