在没有Maven或Gradle的情况下将JUnit 5与Java 9结合使用 [英] Using JUnit 5 with Java 9 without Maven or Gradle

查看:76
本文介绍了在没有Maven或Gradle的情况下将JUnit 5与Java 9结合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Eclipse(Oxygen 4.7.1a)中使用 JUnit 5 创建一个JUnit测试. 此JUnit测试应位于名为Test的单独src文件夹中.但是,由于我是JUnit和 Java 9 的新手,因此遇到了以下问题.

I would like to create a JUnit test using JUnit 5 in Eclipse (Oxygen 4.7.1a). This JUnit test should be inside a seperate src folder called Test. However, I ran into the following problems as I'm new to JUnit and Java 9.

不希望为此使用诸如 Gradle或Maven 之类的构建工具.

I do not want to use build tools like Gradle or Maven for this.

由于我有两个不同的src文件夹,一个用于项目src,一个用于测试用例:

As I've got two different src folders, one for the projects src and one for the test cases:

  • 我需要两个module-info.java文件吗? (每个src文件夹一个)
  • 要使JUnit 5正常工作,我的module-info.java文件中需要哪些模块?

推荐答案

通常,不需要模块化您的测试代码(至少我认为没有合理的理由,也许有人可以给出令人满意的反例).在src下的主代码中只能存在一个module-info.java文件(毕竟,甚至不需要模块化主代码).

In general there is no need to modularize your test code (at least I can't think of a valid reason, maybe someone could give a satisfactory counter-example). Only one module-info.java file can (after all, it's not even required to modularize your main code) be present in your main code under src.

由于module-info.java文件将仅位于您的主源目录中,而不位于测试源目录中,因此从逻辑上讲,它不应依赖于JUnit模块.因此,现在的问题变成了如何依靠模块(代表被测系统)和JUnit模块来编译和运行JUnit测试类.

Since the module-info.java file will be only in your main source directory and not in the test source directory, then logically it should not depend on the JUnit module. So the questions now become how to compile and run the JUnit test classes by relying on the module (that represents the system under test) and the JUnit module.

为此,您需要使用 javac java :

To do that, you'll need to use the new options provided by the javac and java:

因此,假设您具有以下树:

So assuming you have the following tree:

src
    module-info.java (declares a module called "my.module")
    mypackage
        MyClass.java
test_src
    mypackage
        MyClassTest.java
lib/junit-platform-console-standalone.jar

(注意:专门针对JUnit 5,您可以使用junit-platform-console-standalone工件,该工件包含核心JUnit引擎并允许在控制台中运行测试;请参见

(note: specifically for JUnit 5, you can use the junit-platform-console-standalone artifact that contains the core JUnit engine and allows running tests in the console; see the user guide)

然后您可以按以下方式编译代码:

then you can compile the code as follows:

cd root_dir
javac -d mods/my.module src/module-info.java src/mypackage/MyClass.java

cd test_src
javac -d test_out --module-path ../mods;../lib/junit-platform-console-standalone.jar \
--add-modules org.junit.platform.console.standalone,my.module --patch-module my.module=. \
--add-reads my.module=org.junit.platform.console.standalone mypackage/MyClass.java

,您可以运行已编译的测试类:

and you can run the compiled test class:

cd test_src/test_out
java --module-path=../../mods;../../lib/junit-platform-console-standalone.jar \
--add-modules my.module,org.junit.platform.console.standalone \
--add-reads my.module=org.junit.platform.console.standalone \
--patch-module my.module=. \
--add-opens my.module/test=org.junit.platform.console.standalone \ 
org.junit.platform.console.ConsoleLauncher test.MyClassTest

笨拙的命令,但这就是不使用Maven的代价.建议您在了解模块路径的概念后阅读命令文档中的这些选项.这里要注意的重要事项是几个选项:

Awkward commands but that's the cost of not using Maven. I advise you to read about these options in the command documentation after understanding the concept of a module path. An important thing to note here are a couple of options:

--patch-module my.module=.

这是必需的,因为示例测试代码与模块my.module具有相同的软件包(mypackage).没有它,模块系统将抱怨.

This is needed because the example test code has the same package (mypackage) as the module my.module. Without it, the module system will complain.

--add-reads my.module=org.junit.platform.console.standalone

这使my.module要求使用junit,即使未在module-info.java中声明它.

This makes junit required by my.module even though it was not declared in module-info.java.

org.junit.platform.console.standalone automatic模块的名称,并且是从Jar清单(如JUnit 5的情况)派生的,否则是从Jar文件的名称(例如JUnit 4).

org.junit.platform.console.standalone is the name of the automatic module and is derived from the Jar manifest (as is the case with JUnit 5), otherwise from the name of the Jar file (e.g. in the case of JUnit 4).

还请注意,这是Maven在编译和运行单元测试时可能在幕后所做的(请参阅

Also note that this is what Maven probably does behind the scenes when compiling and running unit tests (see this issue for an equivalent plugin configuration that manually does the above).

如果出于某种原因,您还想模块化单元测试,该怎么办?

What if for some reason, you also want to modularize your unit tests?

在这种情况下,由于在上面的示例中,单元测试共享同一包,因此您可以将它们包含在my.module中,并向JUnit添加要求:

In this case, since in the example above the unit tests share the same package, you can include them in my.module and add a requirement to JUnit:

module my.module {
    exports mypackage;
    requires org.junit.platform.console.standalone;
}

如果单元测试位于不同的程序包中,则还可以将它们分为两个模块(两个module-info.java),一个my.module和一个my.test.module,其中只有后者需要JUnit.

If the unit tests were in a different package, you can also split them into two modules (two module-info.java), a my.module and a my.test.module where only the latter requires JUnit.

如果确实在模块中包含测试类,则在上述命令中,您不需要--add-reads--patch-module.

If you do include test classes in a module, then in the above commands, you don't need --add-reads and --patch-module.

这篇关于在没有Maven或Gradle的情况下将JUnit 5与Java 9结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆