JMockit - 初始化问题
当我使用以下测试时,我收到警告:
警告:JMockit 是按需初始化的,这可能会导致某些测试失败; 请查看文档以获取更好的初始化方法。
这是我的测试实施:
package test;
import static mockit.Mockit.*;
import junit.framework.TestCase;
import mockit.*;
import mockit.integration.junit4.*;
import org.junit.*;
import org.junit.runner.*;
import filip.ClassUnderTest;
import filip.LowerClass;
@RunWith(JMockit.class)
public class MockTest extends TestCase {
@MockClass(realClass = LowerClass.class)
public static class LowerClassMock {
@Mock(invocations = 1)
public String doWork() {
return "Mockowanie dziala :D";
}
}
@Before
public void setUp() { setUpMocks(LowerClassMock.class); }
@After
public void tearDown() { tearDownMocks(); }
@Test
public void testJMockit() {
ClassUnderTest classUnderTest = new ClassUnderTest();
classUnderTest.print();
}
}
有什么想法吗?
When I use the following test I get a WARNING:
WARNING: JMockit was initialized on demand, which may cause certain tests to fail;
please check the documentation for better ways to get it initialized.
This is my test implemenation:
package test;
import static mockit.Mockit.*;
import junit.framework.TestCase;
import mockit.*;
import mockit.integration.junit4.*;
import org.junit.*;
import org.junit.runner.*;
import filip.ClassUnderTest;
import filip.LowerClass;
@RunWith(JMockit.class)
public class MockTest extends TestCase {
@MockClass(realClass = LowerClass.class)
public static class LowerClassMock {
@Mock(invocations = 1)
public String doWork() {
return "Mockowanie dziala :D";
}
}
@Before
public void setUp() { setUpMocks(LowerClassMock.class); }
@After
public void tearDown() { tearDownMocks(); }
@Test
public void testJMockit() {
ClassUnderTest classUnderTest = new ClassUnderTest();
classUnderTest.print();
}
}
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
关于链接,接受的答案已经有点过时了,因此值得直接提及各种解决方案。
要解决此问题,请执行以下操作之一:
1 - 指定 javaagent
将其添加到您的 JUnit 执行环境(针对您的版本):
2 - 在 Maven 中配置 Surefire 插件以避免出现这种情况< /strong>
将以下内容添加到您的 Maven 配置中(选择您自己的版本)
确保您的 Surefire 插件配置如下(针对您的特定版本):
3 - 使用 JUnit @RunWith 注释
在每个测试类上添加此 JUnit 运行器注释
The accepted answer has fallen a little out of date regarding the links so it's worth mentioning the various solutions directly.
To fix this problem do one of the following:
1 - Specifiy a javaagent
Add this to your JUnit execution environment (for your version):
2 - configure the Surefire plugin in Maven to avoid it
Add the following to your Maven configuration (choose your own versions)
Ensure that your Surefire plugin is configured as follows (for your particular versions):
3 - Use the JUnit @RunWith annotation
Add this JUnit runner annotation on each and every test class
据我了解,此异常是 当尝试调用 JMockit 方法而 JMockit 尚未正确初始化时抛出。
确保遵循 JMockit 安装说明,尤其是第 3 点和第 4 点。如果 JMockit jar 位于类路径中的 JUnit jar 之后,它可能会导致问题。
As I understand it, this exception is thrown when one attempts to call a JMockit method, while JMockit has not been properly initialized.
Make sure you follow the JMockit installation instructions, especially points 3 and 4. If the JMockit jar comes after the JUnit jar in the classpath, it might cause problems.
除了 Gary Rowe 的解决方案之外:
将 JMockit 更强大(即版本和存储库路径不可知)集成到 Surefire 中为了
使这个解决方案发挥作用,maven-dependency-plugin(版本> = 2.5.1!)需要像这样配置:
In addition to Gary Rowe's solution:
A more robust (i.e. version and repository path agnostic) integration of JMockit into Surefire would be
To make this resolution work, the maven-dependency-plugin (version >= 2.5.1!) needs to be configured like this:
我在类路径中设置了一个属性文件,以便轻松配置 Junit 5:
它必须命名为 junit-platform.properties
确保您使用的是具有 JmockitExtension 类的较新版本的 Jmockit。注意:Jmockit 版本 1.8 并不比版本 1.41 新。 1.8版本应该是1.08。
Maven 中心参考:
https://mvnrepository.com/artifact/org.jmockit/jmockit
I setup a properties file in the classpath for easy configuration of Junit 5:
It MUST be named junit-platform.properties
Make sure you're using a newer version of Jmockit that has the JmockitExtension class. NOTE: Jmockit version 1.8 IS NOT newer than version 1.41. The 1.8 version should have been 1.08.
Maven Central reference:
https://mvnrepository.com/artifact/org.jmockit/jmockit
我只是添加了:
@RunWith(JMockit.class)
根据已接受答案中的文档,这解决了问题。
I simply added:
@RunWith(JMockit.class)
Which resolved the issue, as per the documentation in the accepted answer.
它仍然无法在 IntelliJ 中运行。不过我可以通过命令行运行它。
It still doesnt run for me in IntelliJ. I am able to run it command line though.
我在 Maven 中运行测试没有遇到任何困难,但在 Eclipse 中运行测试时遇到了同样的错误。
JMockit Eclipse 插件允许我在 Eclipse 中运行所有测试,而无需任何额外的配置。
https://marketplace.eclipse.org/content/jmockit-eclipse
其中之一这个插件的特点是
I had no difficulties running my tests in Maven but I got the same error when I was running them in eclipse.
The JMockit Eclipse plugin allowed me to run all the test in eclipse without any extra configuration.
https://marketplace.eclipse.org/content/jmockit-eclipse
One of the featues of this plugin is
对于 VSCode JMockito 错误 -
您必须使用 java.test.config 在其中。
在该文件中添加:
For VSCode JMockito error -
You'll have to make a settings.json file with java.test.config in it.
In that file add :
当我配置我的 maven-surefire-plugin 时,我有两个带有标签
...
的行,这就是阻止 JMockit 初始化的原因。当您调试 Maven 时,尝试使用
-e
标志运行您的生命周期目标,以便您知道 JVM 值是否设置正确。When i configured my maven-surefire-plugin i had two lined with the tag
<argLine>...</argLine>
and that is what prevented JMockit from initializing.When you debug maven, try to run your lifecycle target with a
-e
flag so you'll know if the JVM values were set correctly.