JMockit - 初始化问题

发布于 2024-09-03 02:04:56 字数 967 浏览 9 评论 0原文

当我使用以下测试时,我收到警告:

警告: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

一杯敬自由 2024-09-10 02:04:56

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

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

1 - 指定 javaagent

将其添加到您的 JUnit 执行环境(针对您的版本):

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

2 - 在 Maven 中配置 Surefire 插件以避免出现这种情况< /strong>

将以下内容添加到您的 Maven 配置中(选择您自己的版本)


<依赖关系>
  mockit
  jmockit;


<依赖关系>
  junit
  junit

确保您的 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 {}

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):

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

2 - configure the Surefire plugin in Maven to avoid it

Add the following to your Maven configuration (choose your own versions)

<!-- 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):

<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

Add this JUnit runner annotation on each and every test class

@RunWith(JMockit.class)
public class ExampleTest {}
凉月流沐 2024-09-10 02:04:56

据我了解,此异常是 当尝试调用 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.

朱染 2024-09-10 02:04:56

除了 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>

In addition to Gary Rowe's solution:

A more robust (i.e. version and repository path agnostic) integration of JMockit into Surefire would be

<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:

<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.5.1</version>
<executions>
    <execution>
        <id>getClasspathFilenames</id>
        <goals>
            <goal>properties</goal>
        </goals>
    </execution>
</executions>

雨后彩虹 2024-09-10 02:04:56

我在类路径中设置了一个属性文件,以便轻松配置 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

I setup a properties file in the classpath for easy configuration of Junit 5:

It MUST be named 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.

Maven Central reference:
https://mvnrepository.com/artifact/org.jmockit/jmockit

蓦然回首 2024-09-10 02:04:56

我只是添加了:

@RunWith(JMockit.class)

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

I simply added:

@RunWith(JMockit.class)

Which resolved the issue, as per the documentation in the accepted answer.

旧城空念 2024-09-10 02:04:56

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

It still doesnt run for me in IntelliJ. I am able to run it command line though.

谎言月老 2024-09-10 02:04:56

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

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

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

其中之一这个插件的特点是

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

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

Automatically adds JMockit jar as -javaagent argument to JUnit
launches.

玩套路吗 2024-09-10 02:04:56

对于 VSCode JMockito 错误 -

您必须使用 java.test.config 在其中。

在该文件中添加:

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

        "vmargs": [ 
          "-Xmx512M", //this was here by default
          "-javaagent:/path/to/jmockito/" 
        ]
    ]

For VSCode JMockito error -

You'll have to make a settings.json file with java.test.config in it.

In that file add :

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

        "vmargs": [ 
          "-Xmx512M", //this was here by default
          "-javaagent:/path/to/jmockito/" 
        ]
    ]
挽手叙旧 2024-09-10 02:04:56

当我配置我的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文