首页 > 解决方案 > 如何正确使用 java 流形库中的越狱进行 junit 测试?

问题描述

我正在使用 java 的多方面扩展库进行 junit 测试,即使完全遵循他们的文档,我也无法弄清楚我做错了什么。

// My Class 
package practice_junit;

public class SomeClass
{
    public SomeClass()
    {

    }

    private String get_string()
    {
        return "ABCDE";
    }
}

// My Unit Test Class -- first way
package practice_junit;

import manifold.ext.api.Jailbreak;
import static org.junit.Assert.assertEquals;
import org.junit.Test;

public class SomeClassTest
{
    public SomeClassTest()
    {

    }

    @Test
    public void assert_equals_true_test()
    {
        @Jailbreak SomeClass sc = new SomeClass();
        assertEquals("Error equals","ABCDE",sc.get_string());
    }
}

// My Unit Test Class -- second way
package practice_junit;

import manifold.ext.api.Jailbreak;
import static org.junit.Assert.assertEquals;
import org.junit.Test;

public class SomeClassTest
{
    public SomeClassTest()
    {

    }

    @Test
    public void assert_equals_true_test()
    {
        SomeClass sc = new SomeClass();
        assertEquals("Error equals","ABCDE",sc.jailbreak().get_string());
    }
}

在这两种情况下,我都会收到相同的错误日志:-

PS C:\Users\> gradle build

> Task :compileTestJava FAILED
C:\Users\SomeClassTest.java:19: error: get_string() has private access in SomeClass
                assertEquals("Error equals","ABCDE",sc.get_string());
                                                      ^
1 error

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileTestJava'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 1m 9s
3 actionable tasks: 1 executed, 2 up-to-date

我正在使用compile group: 'systems.manifold', name: 'manifold-ext', version: '2019.1.12'来自https://mvnrepository.com/artifact/systems.manifold/manifold-ext/2019.1.12的 gradle 和流形扩展依赖项

标签: javaunit-testinggradlejunit

解决方案


您使用的是什么版本的 Java?如果 Java 9 或更高版本,您是否使用 JPMS(模块)?如果您发布 Gradle 脚本,我可以帮助您正确设置它。更好的是,在多方面的 github 上发布一个带有项目链接的问题。可能是 --module-path 没有明确设置,这是使用 Java 9+ 的 Gradle 脚本非常常见的问题。以下是相关位:

dependencies {
    compile group: 'systems.manifold', name: 'manifold-ext', version: '2019.1.12'
    testCompile group: 'junit', name: 'junit', version: '4.12'

    // Add manifold to -processorpath for javac (for Java 9+)
    annotationProcessor group: 'systems.manifold', name: 'manifold-ext', version: '2019.1.12'
}

compileJava {
    doFirst() {
        // If you DO NOT define a module-info.java file:
        options.compilerArgs += ['-Xplugin:Manifold']

        // if you DO define a module-info.java file:
        //options.compilerArgs += ['-Xplugin:Manifold', '--module-path', classpath.asPath]
        //classpath = files()
    }
}

Manifold 项目倾向于到处使用 Maven;Gradle 设置文档没有那么完善。


推荐阅读