Include test code in another module
Hello, I'm facing an issue with dependencies / class imports. For demonstration purposes, I have created a minimal reproduction repository: https://github.com/stechy1/java-module-test-issue
There are two modules in the repository: submodule1 and submodule2. Submodule2 depends on submodule1. Dependent classes from submodule1 are visible in submodule2 without any issues. Same is for tests. So it means it is possible to create an instance of the class Example1 in the class Example2. Same is possible to do in Test2 with class Test1 from submodule1.
Now the issue come, when java modules are activated. To do so, rename module-info.java.txt to module-info.java. The compilation fail, because class Test1 become not visible in class Test2.
I have my own reasons why I'm trying to reach classes from submodule1 in submodule2. Also module-info is mandatory to be activated, because the whole project is using modules.
Thank you for any ideas how to solve this issue. If you do not understand something, ping me, I will try explain better.
GitHub
GitHub - stechy1/java-module-test-issue: Minimal reproduction repos...
Minimal reproduction repository of an issue with including test files from one module to another. - GitHub - stechy1/java-module-test-issue: Minimal reproduction repository of an issue with includi...
15 Replies
⌛
This post has been reserved for your question.
Hey @stechy1! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
this is the issue right:
?
and is there a specific reason you want to use modules?
In my case it is a "compilation failure: can not find a symbol "Test1" in "Test2".
Yes, it is required to use modules. I have quite a big project, which uses modules.
Main reason: I wanted to learn, how things works...
ok
your test jar doesn't have module-info which is the core issue, so:
1. rename module-info.java.txt to module-info.java in module1
2. add a module-info.java to module1's
src/test
with the same content as the one in src/main
here's a patch containing all the changes if you wanted to apply it
Yes, I was thinking about that, but than I will have an issue with module naming.
you mean because there might be a module clash between src/test's com.submodule1 and src/main's?
if so then you can change the package name in tests to com.submodule1.test and export that
(but i wouldn't worry because test classes will only be available at test time)
but than I will loose access to package private classes and others
in tests
that's kinda the point of modules and packages
to restrict access
it sounds like you might want a common test module accessible by both
yeah, but it is kind of not desired in case of tests right?
which is also a valid thing to do
not quite; if you need to be subclassing things in tests then something is wrong
the point of tests is to test interfaces, so if you're having to subclass stuff that's kind of a smell
so your options are:
1. a new third test module
2. the steps i suggested in my patch
3. test specific package and module-info but inside module1
hm...
1. option is not possible, since I'm generating classes based on some resources and these generated classes should be available to other modules.
2. and 3. will break some stuff in my codebase 😄 For some reason I have to avoid creating module-info in my tests.
not sure why the generated stuff would affect a new third test module
Than maybe I missunderstand this option...
Anyway, thank you for your ideas.
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.