32

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?

4

8 回答 8

44

关于链接,公认的答案已经过时了,因此值得直接提及各种解决方案。

要解决此问题,请执行以下操作之一:

1 - 指定一个 javaagent

将此添加到您的 JUnit 执行环境(适用于您的版本):

 -javaagent:path/to/your/jmockit/jmockit-0.998.jar 

2 - 在 Maven 中配置 Surefire 插件以避免它

将以下内容添加到您的 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>

确保您的 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 - 使用 JUnit @RunWith 注释

在每个测试类上添加这个 JUnit 运行器注释

@RunWith(JMockit.class)
public class ExampleTest {}
于 2010-12-06T17:12:35.927 回答
20

据我了解,当尝试调用 JMockit 方法时会引发此异常,而 JMockit 尚未正确初始化。

确保遵循JMockit 安装说明,尤其是第 3 点和第 4 点。如果 JMockit jar位于类路径中的 JUnit jar之后,则可能会导致问题。

于 2010-05-25T16:54:25.203 回答
4

除了 Gary Rowe 的解决方案

JMockit 到 Surefire 的更健壮(即版本和存储库路径不可知)集成将是

<argLine>-javaagent:${org.jmockit:jmockit:jar}

为了使这个解决方案起作用,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>

于 2015-03-11T09:06:17.087 回答
1

我在类路径中设置了一个属性文件,以便于配置 Junit 5:

必须命名为 junit-platform.properties

junit.jupiter.extensions.autodetection.enabled = true
junit.jupiter.testinstance.lifecycle.default = per_class

确保您使用的是具有 JmockitExtension 类的较新版本的 Jmockit。注意:Jmockit 1.8 版并不比 1.41 版更新。1.8 版本应该是 1.08。

Maven 中心参考: https ://mvnrepository.com/artifact/org.jmockit/jmockit

于 2018-08-01T16:32:07.240 回答
0

我在 Maven 中运行测试没有任何困难,但是当我在 eclipse 中运行它们时遇到了同样的错误。

JMockit Eclipse 插件允许我在 Eclipse 中运行所有测试,而无需任何额外配置。

https://marketplace.eclipse.org/content/jmockit-eclipse

这个插件的特点之一是

自动将 JMockit jar 作为 -javaagent 参数添加到 JUnit 启动中。

于 2020-02-25T02:08:41.313 回答
0

它仍然无法在 IntelliJ 中为我运行。我可以在命令行运行它。

于 2017-08-16T08:26:55.527 回答
0

我只是补充说:

@RunWith(JMockit.class)

根据已接受答案中的文档,这解决了该问题。

于 2017-05-07T11:37:06.727 回答
0

对于 VSCode JMockito 错误 -

您必须在其中创建一个带有java.test.config的 settings.json 文件。

在该文件中添加:

"java.test.config": [
    {
        "name": "myConfiguration",
        "workingDirectory": "${workspaceFolder}",

        "vmargs": [ 
          "-Xmx512M", //this was here by default
          "-javaagent:/path/to/jmockito/" 
        ]
    ]
于 2021-06-04T15:43:22.903 回答