简体   繁体   English

使用 Java 9+ 模块的内部测试构建项目

[英]Structuring a project with internal tests for Java 9+ modules

I'm trying to learn how the Java 9+ module system works and I ran into an aspect where my old way of structuring a project no longer works.我正在尝试了解 Java 9+ 模块系统是如何工作的,但我遇到了一个方面,即我构建项目的旧方法不再有效。

What I have done in several projects is that for a test to check if the internals of my class were in the right state I created a package private access to get to that state. What I have done in several projects is that for a test to check if the internals of my class were in the right state I created a package private access to get to that state. Then my test class would be in the same package and could access this field/method, yet normal users of my class would not be able to get to this.然后我的测试 class 将在同一个 package 中并且可以访问这个字段/方法,但是我的 class 的普通用户将无法访问这个。

Entering JPMS I find myself sliding down this slope (my attempts https://github.com/nielsbasjes/learnjavamodules ):进入 JPMS,我发现自己滑下这个斜坡(我的尝试https://github.com/nielsbasjes/learnjavamodules ):

  1. If main code is a module then the test code also needs to be a module.如果main代码是一个模块,那么test代码也需要是一个模块。
  2. Each module must have a distinct module name.每个模块必须有一个不同的模块名称。
    • So the main and test code lives in different modules所以main代码和test代码存在于不同的模块中
  3. A single java package may not exist in two modules.单个 java package 可能不存在于两个模块中。
    • So the main and test code cannot share a package.所以main代码和test代码不能共享一个 package。
  4. The package private trick no longer works because that would need the same package to exist in both my main and my test modules. package private技巧不再有效,因为这需要相同的 package 存在于我的main模块和我的test模块中。
  5. Which forces me to open the internals I want to test.这迫使我打开我想要测试的内部结构。
    1. So I either make it fully public.所以我要么完全公开。
    2. Or I have to do 'opens' and resort to reflection magic to get to the field.或者我必须做'opens'并诉诸反射魔法才能到达现场。

To me this all feels like I fell down the wrong rabbit hole because of my current lack of knowledge.对我来说,这一切都感觉就像我掉进了错误的兔子洞,因为我目前缺乏知识。

I have not been able to find/figure out what the normal/intended way of doing these seemingly trivial things.我一直无法找到/弄清楚做这些看似微不足道的事情的正常/预期方式。

How do you do this properly?你如何正确地做到这一点?

You can use a module also from code that is not in a module, and currently, JUnit is not modularised.您也可以从不在模块中的代码中使用模块,目前,JUnit 未模块化。 So you write your JUnit tests as usual, with the drawback that you cannot test features that behave different when used by a module.因此,您像往常一样编写 JUnit 测试,缺点是您无法测试模块使用时表现不同的功能。 But as these are not internal by definition, this should not harm you much.但由于这些都不是内部定义的,所以这应该不会对您造成太大伤害。

For the API tests (the public stuff), you create a new project with a separate module, and wrote your tests there.对于 API 测试(公共的东西),您创建一个带有单独模块的新项目,并在那里编写您的测试。 Currently, I do not JUnit for these tests.目前,我没有 JUnit 进行这些测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM