Java JMockit - 初始化问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2905735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JMockit - initialization problem
提问by Filip
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.
警告:JMockit 是按需初始化的,这可能会导致某些测试失败;请查看文档以获得更好的初始化方法。
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?
有任何想法吗?
采纳答案by Etienne Neveu
As I understand it, this exception is thrownwhen one attempts to call a JMockit method, while JMockit has not been properly initialized.
据我了解,当尝试调用 JMockit 方法时抛出此异常,而 JMockit 尚未正确初始化。
Make sure you follow the JMockit installation instructions, especially points 3 and 4. If the JMockit jar comes afterthe JUnit jar in the classpath, it might cause problems.
请确保您按照JMockit安装说明,尤其是分3和4。如果JMockit罐子来后在classpath中JUnit的罐子,它可能会导致问题。
回答by Gary Rowe
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
1 - 指定一个 javaagent
Add this to your JUnit execution environment (for your version):
将此添加到您的 JUnit 执行环境(适用于您的版本):
-javaagent:path/to/your/jmockit/jmockit-0.998.jar
2 - configure the Surefire plugin in Maven to avoid it
2 - 在 Maven 中配置 Surefire 插件以避免它
Add the following to your Maven configuration (choose your own versions)
将以下内容添加到您的 Maven 配置中(选择您自己的版本)
<!-- JMockit must be before JUnit in the classpath --> <dependency> <groupId>mockit</groupId> <artifactId>jmockit</artifactId> </dependency> <!-- Standard unit testing --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency>
<!-- JMockit must be before JUnit in the classpath --> <dependency> <groupId>mockit</groupId> <artifactId>jmockit</artifactId> </dependency> <!-- Standard unit testing --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency>
Ensure that your Surefire plugin is configured as follows (for your particular versions):
确保您的 Surefire 插件配置如下(针对您的特定版本):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<argLine>-javaagent:${settings.localRepository}/mockit/jmockit/0.998/jmockit-0.998.jar</argLine>
<useSystemClassLoader>true</useSystemClassLoader>
</configuration>
</plugin>
3 - Use the JUnit @RunWith annotation
3 - 使用 JUnit @RunWith 注释
Add this JUnit runner annotation on each and every test class
在每个测试类上添加此 JUnit 运行器注释
@RunWith(JMockit.class)
public class ExampleTest {}
回答by Peter Wippermann
In addition to Gary Rowe's solution:
除了 Gary Rowe 的解决方案:
A more robust (i.e. version and repository path agnostic) integration of JMockit into Surefirewould be
将 JMockit 更强大(即版本和存储库路径不可知)集成到 Surefire将是
<argLine>-javaagent:${org.jmockit:jmockit:jar}
To make this resolution work, the maven-dependency-plugin (version >= 2.5.1!) needs to be configured like this:
为了使这个解决方案起作用,maven-dependency-plugin(版本>= 2.5.1!)需要像这样配置:
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<id>getClasspathFilenames</id>
<goals>
<goal>properties</goal>
</goals>
</execution>
</executions>
回答by Benjamin Slabbert
I simply added:
我简单地补充道:
@RunWith(JMockit.class)
@RunWith(JMockit.class)
Which resolved the issue, as per the documentation in the accepted answer.
根据接受的答案中的文档,它解决了这个问题。
回答by Ronn Macc
It still doesnt run for me in IntelliJ. I am able to run it command line though.
它仍然不适合我在 IntelliJ 中运行。不过,我可以在命令行中运行它。
回答by TheJeff
I setup a properties file in the classpath for easy configuration of Junit 5:
我在类路径中设置了一个属性文件,以便轻松配置 Junit 5:
It MUSTbe named junit-platform.properties
它必须命名为 junit-platform.properties
junit.jupiter.extensions.autodetection.enabled = true
junit.jupiter.testinstance.lifecycle.default = per_class
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.
确保您使用的是具有 JmockitExtension 类的较新版本的 Jmockit。注意:Jmockit 1.8 版并不比 1.41 版更新。1.8 版本应该是 1.08。
Maven Central reference: https://mvnrepository.com/artifact/org.jmockit/jmockit
Maven 中心参考:https: //mvnrepository.com/artifact/org.jmockit/jmockit
回答by Peter Lustig
I had no difficulties running my tests in Maven but I got the same error when I was running them in eclipse.
我在 Maven 中运行我的测试没有任何困难,但是当我在 Eclipse 中运行它们时我遇到了同样的错误。
The JMockit Eclipse plugin allowed me to run all the test in eclipse without any extra configuration.
JMockit Eclipse 插件允许我在 Eclipse 中运行所有测试而无需任何额外配置。
https://marketplace.eclipse.org/content/jmockit-eclipse
https://marketplace.eclipse.org/content/jmockit-eclipse
One of the featues of this plugin is
这个插件的特点之一是
Automatically adds JMockit jar as -javaagent argument to JUnit launches.
自动将 JMockit jar 作为 -javaagent 参数添加到 JUnit 启动。