Java 9模块和JUnit 4 [英] Java 9 Modules and JUnit 4

查看:463
本文介绍了Java 9模块和JUnit 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Eclipse氧气; Windows 7的; JDK 9决赛9月21日; JUnit 4.12和现有的应用程序。作为起点,可以编译,执行应用程序,并且所有JUnit测试都显示为绿色。现在我们使用eclipse生成文件module-info.java。结果如下:

Eclipse oxygen; windows 7; JDK 9 final from 9, 21; JUnit 4.12 and an existing application. As starting point the application can be compiled, executed and all JUnit Tests shows green. Now we use eclipse to generate the file module-info.java. The outcome looks like:

module ch.commcity.topsort {
   exports ch.commcity.topsort;

   requires junit;
}

但是出现错误:junit无法解析为模块。
问题是:如何告诉文件junit没有定义任何模块,它应该在兼容模式下运行?

But with the error: junit cannot be resolved to module. The question is: How to tell the file that junit does not define any module and it should be run in compatibility mode?

推荐答案


如何告诉文件junit没有定义任何模块,它应该在兼容模式下运行?

How to tell the file that junit does not define any module and it should be run in compatibility mode?

你的问题似乎是基于几个误解:

Your question seems to be based on several misconceptions:


  • 你不能告诉 module-info .java JUnit是否定义了模块。如果一个模块说它需要另一个模块,那么编译器希望该模块存在 - 无处可去。

  • JUnit 4是否作为一个模块出现并不是太重要 - 只要当你把它放在模块路径上时,它最终将被视为一个模块(可能是一个自动的)。

  • 没有兼容模式。您可以像在模块系统之前那样继续编写代码(几乎),但是一旦创建了模块声明,就需要按照规则进行编写。

  • You can not tell module-info.java whether JUnit defines a module or not. If a module says it requires another module then the compiler expects that module to be present - no way around that.
  • Whether JUnit 4 comes as a module or not is not overly important - as long as you place it on the module path, it will end up being treated as a module (possibly as an automatic one).
  • There is no "compatibility mode". You can continue to write code as you did before the module system (almost), but once you're creating module declarations you need to play by its rules.

我建议提供优秀的模块系统状态彻底阅读,然后问问自己,你究竟想要完成什么。你真的想创建一个依赖于JUnit的模块吗?或者这只是偶然的,因为您使用其API进行测试。如果是后者,你不应该依赖它 - 而是你的IDE /构建工具需要弄清楚如何编译和运行测试。

I recommend to give the outstanding State of the Module System a thorough read and then ask yourself what exactly you are trying to accomplish. Are you really trying to create a module that depends on JUnit? Or was that just accidental because you use its API for testing. If the latter, you should not depend on it - instead your IDE / build tool needs to figure out how to compile and run your tests.

模块系统不会将依赖关系分类为compile或test - 如果模块需要另一个模块,它必须存在,始终。这意味着需要junit 的模块会强制存在JUnit。除非模块提供与测试相关的功能,否则这肯定是错误的。

The module system does not classify dependencies as "compile" or "test" - if a module requires another module, it has to be present, always. That means a module that requires junit would force the presence of JUnit. Unless the module provides testing-related features, that is most certainly wrong.

换句话说,需要junit 是类似于将JUnit添加到项目的POM中,使用编译 范围

In other words, requires junit is akin to adding JUnit to a project's POM, using the compile scope.

这篇关于Java 9模块和JUnit 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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