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

查看:63
本文介绍了将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中工作的问题仍在讨论中-甚至the NullPointerException I ran into

  • Maven 3.3.9
  • maven-compiler-plugin 3.6.0
  • source and target config: 9
  • 解决方案

    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天全站免登陆