将 Java 8 项目迁移到 Jigsaw 时,我应该将单元测试放在哪里 [英] Where should I put unit tests when migrating a Java 8 project to Jigsaw

查看:17
本文介绍了将 Java 8 项目迁移到 Jigsaw 时,我应该将单元测试放在哪里的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在测试使用 jdk-9+149 将 Java 8 应用程序迁移到 Java 9/Jigsaw.

项目已经按照标准的Maven目录布局进行布局,即具有src/main/javasrc/test/java

一旦我将 module-info.java 添加到 src/main/java,maven-compiler-plugin 就会失败并抛出 NullPointerException.这是因为它也希望为 test 目录找到一个模块信息.所以,据我所知,选项是:

  • 将测试类保存在单独的模块中,这意味着它们只能访问主模块的导出包.
  • 将测试类与源代码一起移到主模块中,这意味着主模块需要测试依赖项.

显然,这两个选项似乎都不是可取的,所以我假设有一种推荐的方法可以在 Maven 项目中测试 Jigsaw 模块.不幸的是,我既找不到建议也找不到示例.

添加一些我在发布问题时不认为相关的信息(抱歉)

解决方案

Maven 对 Java 9 的一般支持和特别是测试仍在开发中 - 许多事情有效,但其他人可能无效.没有看到 NPE 的堆栈跟踪,这当然是猜测,但我假设您遇到了这个错误.>

更一般地说,单元测试如何与 Jigsaw 一起工作的问题仍在讨论中 - 甚至 在 Jigsaw 邮件列表中.

以下是我对此事的看法:

  1. 正如您所注意到的,将测试放入单独的模块意味着只有导出包中的公共类型是可测试的,这绝对是不够的.可能有解决方法,但那些需要编辑模块声明(源代码;module-info.java)或描述符(字节码,module-info.class)在编译和运行测试的 javacjava 命令中添加大量 --add-exports 命令行标志.这些听起来都不是特别有趣,尤其是如果您想手动完成.

  2. 出于显而易见的原因,将测试移到源代码树中也是一个坏主意,尤其是在创建没有测试的 JAR 时需要进行大量操作.

  3. 另一个选项是使用 --patch-module 选项,该选项允许将 class-files 或 JAR 的内容添加到现有模块.这样testCompile 步骤可以创建一个包含源 测试文件的JAR.不幸的是,除非它如上所述操作模块声明/描述,否则如果不使用 java --add-reads 为测试依赖项添加读取边缘,则生成的 JAR 无法执行.不过还是比上面的好.

  4. 作为最后的手段,有一些方法可以将生产 JAR 视为常规 JAR 而不是模块.最简单的方法可能是将它与测试 JAR 一起转储到类路径上.这样一切都像在 Java <9 中一样.不幸的是,这会破坏使用功能的代码,假设它位于命名模块内(例如,与反射 API 的某些类型的交互).

就个人而言,我认为 3. 是上述选项中最好的.它不需要更改项目布局,只需对 javacjava 命令进行相对较小的添加.

I am currently testing to migrate a Java 8 application to Java 9 / Jigsaw, using jdk-9+149.

The project has been laid out in standard Maven directory layout, i.e. having src/main/java, src/test/java, etc.

As soon as I add the module-info.java to src/main/java, maven-compiler-plugin fails throwing a NullPointerException. This is because it expects to find a module-info for the test directory, too. So, as far as I can see, the options are:

  • keep test classes in a separate module, which means they can only access exported packages of the main module.
  • move test classes into the main module, together with the sources, which means that the main module needs test dependencies.

Obviously, neither of those options seems to be desirable, so I assume there is a recommended way of testing Jigsaw modules in a Maven project. Unfornately, I could neither find recommendations nor examples.

EDIT: Adding some information that I didn't regard relevant when posting the question (sorry)

解决方案

Maven support for Java 9 in general and tests in particular is still under development - many things work but others might not. Without seeing the stack trace for the NPE, it is of course speculation, but I assume you ran into this error.

More generally, the question of how exactly unit tests will work with Jigsaw is still being discussed - even on the Jigsaw mailing list.

Here's my opinion on the matter:

  1. As you note, putting tests into a separate module would mean that only public types in exported packages would be testable, which is definitely not enough. There could be workarounds but those require to either edit the module declaration (source code; module-info.java) or descriptor (byte code, module-info.class) on the fly or add a ton of --add-exports command line flags to the javac and java commands compiling and running the tests. None of these sound particularly fun, especially if you want to do it by hand.

  2. Moving tests into the source tree is a bad idea as well for obvious reasons, not least among them that creating a JAR without tests would then require a lot of fiddling.

  3. Another option is to use the --patch-module option that allows to add class-files or the content of a JAR to an existing module. This way the testCompile step could create a JAR containing the source and test files. Unfortunately, unless it manipulates the module declaration/description as described above, the resulting JAR can not be executed without adding reads edges with java --add-reads for the test dependencies. Still, better than above.

  4. As a kind of last resort, there are ways to have the production JAR be treated like a regular JAR instead of as a module. The easiest would probably be to dump it on the class path together with the test JAR. This way everything works as in Java <9. Unfortunately this will break code that uses features under the assumption that it is inside a named module (e.g. certain kinds of interactions with the reflection API).

Personally, I consider 3. to be the best of the above options. It would require no changes in project layout and only comparatively minor additions to javac and java commands.

这篇关于将 Java 8 项目迁移到 Jigsaw 时,我应该将单元测试放在哪里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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