使用maven设置一个简单的jmockit测试的问题

时间:2015-05-28 17:32:12

标签: java maven junit pom.xml jmockit

今天我花了一些非零时间尝试设置一个最简单的maven项目来运行最简单的jmockit测试。

在尝试编写这样的xml时,我遇到了一些问题,从

开始
java.lang.NoClassDefFoundError: org.junit.runner.Runner
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:61)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
....

后来遇到运行问题。

不幸的是,尝试使用谷歌找到快速回答并没有帮助。

那么,与maven一起使用jmockit框架的最小pom.xml是什么?

2 个答案:

答案 0 :(得分:0)

最后,我想出了一个我想分享的工作pom.xml - 这可能对某人有用。

$ cat pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany</groupId>
    <artifactId>jmockit-test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>org.jmockit</groupId>
            <artifactId>jmockit</artifactId>
            <version>1.17</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>   
    <build>
        <defaultGoal>test</defaultGoal>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <argLine>-javaagent:"${settings.localRepository}"/org/jmockit/jmockit/1.17/jmockit-1.17.jar</argLine>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>
</project>

源文件是:

$ cat src/main/java/com/test/jmock/DataProvider.java
package com.test.jmock;

public interface DataProvider {
    Integer getInt();
    Boolean getBoolean();
}

$ cat src/test/java/com/test/jmock/TrivialTest.java
package com.test.jmock;

import mockit.Mocked;
import mockit.NonStrictExpectations;

import static org.junit.Assert.assertEquals;
import org.junit.Test;

public final class TrivialTest {

    @Mocked
    private DataProvider provider;

    @Test
    public void test() {
        init();
        Integer mockIntData = provider.getInt();
        System.out.println("Mock int data is " + mockIntData);
        assertEquals("Unexpected result", mockIntData, Integer.valueOf(12345));

        Boolean mockBoolData = provider.getBoolean();
        System.out.println("Mock bool data is " + mockBoolData);
        assertEquals("Unexpected result", mockBoolData, Boolean.TRUE);
    }

    private void init() {
        new NonStrictExpectations() {
            {
                provider.getInt();
                result = 12345;
                provider.getBoolean();
                result = Boolean.TRUE;
            }
        };
    }
}

现在这可以按预期工作了!

$ mvn test
...
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.025 sec - in com.test.jmock.TrivialTest

答案 1 :(得分:0)

我有同样的问题。我使用了@ andrew-krasny的答案,并将其修改为使用其他解决方案(-Djdk.attach.allowAttachSelf),因此在更新jmockit时不必更新它。

  <build>
    <plugins>
      <!--  Compiler -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.7.0</version>
        <configuration>
          <source>10</source> <!-- was 1.8 -->
          <target>10</target> <!-- was 1.8 -->
        </configuration>
      </plugin>

      <!-- FIX START -->
      <!-- Test -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.1</version>
        <configuration>
          <argLine>-Djdk.attach.allowAttachSelf</argLine>
        </configuration>
      </plugin>
      <!-- FIX END -->
    </plugins>
  </build>