首页 > 解决方案 > Eclipse 插件 junit-tools 使用 @MethodRef 生成代码,但未定义

问题描述

我为 Eclipse 安装了 junit-tools 以简化 JUnit 测试的创建。该工具生成的测试类有一个 @MethodRef 注释,但它无法解析,我找不到 jar 文件,甚至找不到要添加的 Maven 依赖项。我也不知道完全限定的包名是什么。在我对其进行任何修改之前,问题出在生成的文件上。

关于插件: http ://junit-tools.org/index.php 插件名称:org.junit.tools 版本:1.1.0.201811090440 Plugin-Id:org.junit.tools

Eclipse 版本:版本:2018-09 (4.9.0) 内部版本号:20180917-1800

违规代码是 C:\eclipse-workspaces\jdbvc\dbvcs\src\test\java\com\enormacorp\dbvcs\model\ConnectionInformationTest.java

package com.enormacorp.dbvcs.model;

import javax.annotation.Generated;

import org.junit.Test;

@Generated(value = "org.junit-tools-1.1.0")
public class ConnectionInformationTest {

    private ConnectionInformation createTestSubject() {
        return new ConnectionInformation();
    }

    @MethodRef(name = "getJdbcDriverClassName", signature = "()QString;")
    @Test
    public void testGetJdbcDriverClassName() throws Exception {
        ConnectionInformation testSubject;
        String result;

        // default test
        testSubject = createTestSubject();
        result = testSubject.getJdbcDriverClassName();
    }

    @MethodRef(name = "setJdbcDriverClassName", signature = "(QString;)V")
    @Test
    public void testSetJdbcDriverClassName() throws Exception {
        ConnectionInformation testSubject;
        String jdbcDriverClassName = "";

        // default test
        testSubject = createTestSubject();
        testSubject.setJdbcDriverClassName(jdbcDriverClassName);
    }
}

pom文件:

<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.enormacorp.dbvcs</groupId>
    <artifactId>main</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>com.enormacorp.dbvcs.main</name>
    <packaging>pom</packaging>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-all -->
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-all</artifactId>
            <version>1.3</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

Eclipse 错误:MethodRef 无法解析为类型

在这个项目的 Eclipse 中的 Java 构建路径中,我只有 JRE 系统库和 maven 依赖项。

我希望这可以解决该工具生成的任何类。我的第一选择是添加一个 Maven 测试范围依赖项。我的第二个选择是将定义 @MethodRef 的任何 jar 显式添加到构建路径中。

如果你读到这里,感谢 Woodsman。

标签: eclipsejunit

解决方案


它缺少 Junit-tools jar 文件。Maven依赖不起作用。

<dependency>
    <groupId>net.praqma</groupId>
    <artifactId>junit-tools</artifactId>
    <version>0.1.0</version>
</dependency>

所以从“http://junit-tools.org/index.php/getting-started”下载依赖的jars文件并将它们放在类路径中。 在此处输入图像描述


推荐阅读