I´m having a module C which contains a jar files from modudle A and B
I´m running a java process with CP java -cp A.jar B.jar Main.class and I´m running some integration tests.
Then a jacoco-it.exec file it´s created in C module, but I´´m afraid that when Sonar Queue scan the module in C tell me
10:55:50.562 INFO - Analysing D:\Users\nb38tv\workspace\WS_F2ECORE_LTD\DG2\core\f2e-mock\..\target\jacoco-it.exec
10:55:50.578 INFO - Analysing D:\Users\nb38tv\workspace\WS_F2ECORE_LTD\DG2\core\f2e-mock\target\sonar\jacoco-overall.exec
10:55:50.593 INFO - No information about coverage per test.
Why Sonar cannot find the coberture of classes of A.jar and B.jar??
Regards.
This sentence does not refer to display of coverage in general, but to the ability of SonarQube to show which test contributes which coverage. This requires some additional configuration - quoting https://docs.sonarqube.org/display/PLUG/Usage+of+JaCoCo+with+Java+Plugin
Using some unit test listeners you can collect the information on which lines where covered by which tests
and this page also refers to an example at https://github.com/SonarSource/sonar-examples/tree/master/projects/languages/java/code-coverage/ut/ut-maven-jacoco
Related
I have a maven project (multimodule, JDK 11) that has a lot of modules and tests within it.
I suddenly noticed that one of my tests is NOT invoked as part of the surefire plugin.
Other tests in the same module do get to be invoked successfully.
I am able to run this test successfully via Intellij. The test is not disabled nor ignored. it contains a set of #Test methods. In addition, I am able to run it specifically using mvn test -Dtest=... argument.
The test class gets to be compiled successfully and appears in target/test-classes.
(I have already run with -X and could not find relevant additional data there)
I am working with surefire plugin 2.22.1 ; junit-vintage-engine:5.2.0 ; junit-platform-runner:1.2.0 ; Spring Boot 2.5.3 (with BOM) ; My test is spring based and invoked using #ExtendWith(SpringExtension.class)
(I do have other spring based tests that are invoked as expected though).
Following this question, I have also tried to work with 2.22.2. It did not solve the problem
Following this question, I have verified and indeed all tests in the test class that does not invoke are prefixed with the 'test' word
EDIT:
The problem happens in a test that belongs to a module that is part of a larger project in my company, which inherits various defaults across the POM inheritance chain. Unfortunately, I can not isolate only my POM out of it and present it.
I have tried to reproduce the problem in a new isolated project but did not succeed (that is, all the tests were invoked as expected).
What am I missing? Is there a hidden switch of the surefire plugin that reveals what's going on under the hood and where do surefire pull the list of tests he is meant to execute?
The surefire plugin (by default) will run only tests with the following name syntax ...
"**/Test*.java" - includes all of its subdirectories and all Java filenames that start with "Test".
"**/*Test.java" - includes all of its subdirectories and all Java filenames that end with "Test".
"**/*Tests.java" - includes all of its subdirectories and all Java filenames that end with "Tests".
"**/*TestCase.java" - includes all of its subdirectories and all Java filenames that end with "TestCase".
More details can be found here
Problem solved!
Following #Kathrin Geilmann comment:
...By default surefire only executes: ...Test.java, Test....java,
...Tests.java and ...TestCase.java
The name of my file ended with ...Tester (!##$$%^)
Thanks.
This is my base module which needs implementations of interfaces defined in myspi package. Various providers can offer MyProvider implementations. Base module uses them via myspi.MyProvider interface implementation.
module base {
exports myspi;
uses myspi.MyProvider;
}
This is my sample implementation module which provides the MyProvider implementation with MyProviderImpl
module myspi.provider {
provides myspi.MyProvider with myspi.provider.MyProviderImpl;
}
All these work fine when I load the implementations in base module, with
public static List<MyProvider> getMyProviders() {
var myProviders = new ArrayList<MyProvider>();
for (MyProvider myProvider : ServiceLoader.<MyProvider>load(MyProvider.class)) {
myProviders.add(myProvider);
}
return myProviders;
}
But same code returns empty list in Junit 5 test code (ServiceLoader returns null). How can I test the service provider modules with Junit 5. Or is there any alternative to Junit that allows us to create test modules (modularized test API) that declares "uses myspi.MyProvider" in the module-info and works fine with getMyProviders()?
Basically you're on the right track. You need to convince the Java module system that your test modules are the single source of thruth when it comes to resolve modules are test runtime.
Black-box testing is easy.
White-box testing in the modular world, meaning testing protected and package private members within a module, is tricky. There are at least two ways to achieve this: a) use java command line options to configure the Java module system at test startup or b) blend main sources into the test sources at compile time and maintain a dedicated module-info.java in your test sources.
Please visit the links to the blogs and examples posted over at How to make a modular build with jdk > 1.8
Here is an excerpt for convenience:
Examples
Work-in-progress blueprint https://github.com/sormuras/sandbox/tree/master/sors-modular-testing-blueprint
Integration tests starting with "modular-world-" at https://github.com/sormuras/junit-platform-maven-plugin/tree/master/src/it
Background and other resources
https://github.com/junit-team/junit5-samples/tree/master/junit5-modular-world
https://github.com/forax/pro
https://blog.codefx.org/java/five-command-line-options-to-hack-the-java-9-module-system/
And expect most IDE to not support you either. For now.
SOLVED!
I've removed the Junit from class-path to module-path and also removed all Junit 4 compatibility stuff such as RunWith() etc, and made my test pure Junit 5 test.
I've added a module-info.java (Junit 5 doesn't require an open module although the books tell the opposite)
After I've modularized the tests I found that it still doesn't execute the ServiceLoader stuff. Then I've started looking for the fault myself.
And I found it! Running the ServiceLoader stuff in base module was possible, because the base module refers to the exported myProvider.jar, which in turns access a myProvider-config.properties file in the same directory. Without this config file myProvider cannot work properly.
The problematic test module on the other hand, refered the eclipse project of the myProvider instead of its exported .jar file and hence could not find its config file and exits. I'd moved this config file from Netbeans to Eclipse simply copying it into the same directory. Thus missing config file was the problem.
Changing the project settings I could run the tests without any failure.
I would like to thank all the contributors who responded.
This is quite an old post but if anyone gets here trying to test java modules with junit 5 with gradle, especially the consumer/provider as presented in this post , Sormuras solution is the easy way, to patch the consumer module with the tests classes.
it is supported by gradle-modules-plugin that does that out of the box:
https://github.com/java9-modularity/gradle-modules-plugin
Now ,I have a test project need using emma.
But when I open the "$ANDROID_HOME/sdk/tools/lib" path, I found three emma*.jar.
It's emma.jar, emma_ant.jar, emma_device.jar.
But anybody can tell me what's there difference ?
Thanks
Here is a partial answer, about two of the three jars :
emma.jar Contains the implementation of EMMA core components command
line tools, and EMMA runtime classes (EMMA classes that are needed by
Java application code that has been instrumented for coverage).
emma_ant.jar Contains the implementation of EMMA ANT tasks (this
archive depends on emma.jar and does not overlap with it in content).
See Emma Reference Manual
Junit test classes reside in totally different target folders, but while running test cases how is it possible for test class to get reference to the source classes?
The test framework makes use of the classpath feature of a JVM. The classpath indicates where the program running the tests should find the classes it needs. The environment (your IDE, for example) sets the classpath to search in both the location of your test classes and the location of the classes you are testing. It will probably set the classpath to search in the location of your test classes and then search in the location of the classes being tested.
I am using Jenkins with Sonar to perform a build of my Java EE application through an ANT script.
The build works fine and the unit test case run fine as well.
I am currently getting a unit test coverage % but the "Unit test sucess" shows as zero. When I click on the "0 tests" which is available to me as a link on the Sonar dashboard, I get to see the different modules of my Java EE application which Sonar ran through as defined in the build.xml used by Jenkins.
After some research online and reading through a similar Stackoverflow question at this link - Unit test success reported as zero by Sonar
I added the following property tags in my build.xml (used by Jenkins) located in my Java EE application root folder and in the sonar.xml files residing in every sub folder which is defined as a module in the build.xml file:
<property name="sonar.java.coveragePlugin" value="jacoco" />
<property name="sonar.junit.reportsPath" value="ABCD/testreport/" />
<property name="sonar.jacoco.reportPath" value="ABCD/build/classes/jacoco.exec" />
where "ABCD" is the directory which I use to house my Junit test case JAVA files.
The ANT script xml file does contain a junit task which compiles executes my unit test cases in the Jenkins build before giving the hanndle to Sonar. Since I know I am getting a unit test coverage, I know these unit tests are being seen to a degree but what additional steps do I need to take to make the unit test success show up with a value greater than zero?
Please advise.
Thanks in advance.
Subbu