Tools :
Maven 3
JUnit 3
Issue :
I have a legacy project which uses JUnit 3.
I have created a new test module with src/test/java as project structure and added JUnits 3 classes inside it.
JUnits classes follow correct naming convention as - Test*.java
But when I run mvn clean test , maven is not picking up the tests.
It seems no tests to run message is also not coming given by maven-surefire-plugin
Can anybody please advice what is the issue here ?
I am really not able to find why tests are not picked up
There was a mistake in pom.xml .
The packaging was declared as <packaging>pom</packaging>
The packaging is changed to jar and it started working
Related
Hey its the first time i use JUnit with Java and i'm not sure how to set it up inside Visual studio Code. I tried to put Junit-4.13.jar inside my a lib file in the root directory but nothing changed. any ideas? I am on MacOS.
There were 2 main issues with this configuration:
JUnit 4 dependency jar was used while in the source code imports from JUnit 5 API were used
Tests resided in the same location as sources, while the dependencies defined via Gradle/Maven for tests limit the scope to src/test/java roots.
In a non-Gradle project the issue was fixed by adding the proper JUnit 5 dependencies to the module.
In the Gradle project the issue was fixed by moving the test class from src/main/java to src/test/java.
Sample Gradle project is available on GitHub.
More details about using JUnit 5 can be found in the official documentation.
SonarQube: 8.2.0.32929
sonar-scanner: 3.0.3.778
jacoco: 0.8.4
jdk: 1.8
mvn: 3.6.3
What are you trying to achieve
I am trying to achieve code coverage by using sonar-scanner but I am getting code coverage 0 in sonarqube dashboard.
What have you tried so far to achieve this
I configured the multi-module java project using https://github.com/SonarSource/sonar-scanning-examples/tree/master/sonarqube-scanner-maven/maven-multimodule
and created sonar-project.properties file in base directory with below configuration
sonar.projectKey=org.sonarqube.sonarscanner-maven-aggregate
sonar.projectName=Sonar Scanner Maven Aggregate
sonar.projectVersion=1.0
sonar.language=java
sonar.java.binaries=.
If I use mvn sonar:sonar it works. but with sonar-scanner it is not working.
It works fine with sonarqube 7.8.
Any insight would be appreciated.
I fixed this issue.
while running sonar-scanner command, I added the xmlReportPaths as a define property like sonar-scanner -Dsonar.coverage.jacoco.xmlReportPaths=tests/target/site/jacoco-aggregate/jacoco.xml,../tests/target/site/jacoco-aggregate/jacoco.xml
If your projects are set up as multi-module, you will have to feed the coverage report from each module to the last module that will run as part of the build (probably integration tests modules)
https://community.sonarsource.com/t/coverage-test-data-importing-jacoco-coverage-report-in-xml-format/12151
https://community.sonarsource.com/t/in-sonarqube-8-2-code-coverage-is-always-showing-0/21666
https://docs.sonarqube.org/latest/analysis/coverage/
https://stackoverflow.com/a/15535970
Following the steps mentioned in this example project by Sonar team helped me
https://github.com/SonarSource/sonar-scanning-examples/tree/master/sonarqube-scanner-maven/maven-multimodule
As you already followed these steps, one thing I was missing was the aggregated report was not generated in the last module and I was doing it in one module before that which was our integration test module. But there was one more deployment module in our case
I am working on understanding Maven and I'm learning about building your Java app with it.
So when I do a :
maven package
It does build my jar as expected but I see in the output console that Maven does build tests (it always say that the test a run and there are no failure).
I researched on the web about that and learned that Maven use a plugin called Maven Surefire. But I can't understand what does that plugin do to my code, what does the tests "means" ? What does the tests do with my code and how it works behind the console ?
The Maven surefire plugin runs the tests you have written. These are usually in the src/test/java folder. If you have none, the plugin does nothing.
Is this only one question? :D
So. Different things are going on.
You create an application with Java. To test the single components / packages / classes that you create most people use JUnit or TestNg. You usually have dedicated test classes that verify your production code behaves as intended without you clicking through all the things on every change.
When you now use maven to run your build the pom.xml file defines a packaging - in your case "jar" since you create a jar file. The packaging defines what set of default plugins run in the defined maven phases. You probably recognize package here. Maven executes all phases up to package and the registered / configured plugins.
To execute those tests maven provides the surefire plugin which supports running JUnit or TestNg tests. If you follow the directory conventions your tests reside in src/test/java and the surefire includes naming convention maven will execute those tests in every build (as this is the best practice). If you also want to write integration tests then there is the failsafe plugin. That plugin is not enabled by default and runs in different maven phases.
So the tests just run your production code - in fact they just do what you implement in the tests. They don't alter it in any way.
The maven introduction documentation has step by step explanations: Maven in 5 Minutes and the Getting Started Guide.
Starting from scratch this is probably a lot. So don't rush this. The build setup and test setup are very important things to have.
I'm writing code in Intellij and have a JUnit test class included in a project and i understand that running of JUnit should always be done at build time.
Is there a way to first run the JUnit and only if there were no test error run the project itself ? I want them to run together with 1 click (NOT run them seperately/manually).
Also, i would like the above to work even when the project is packed as a .jar file.
How can it be done ?
Thanks !
In Intellij:
Run -> Edit Configurations
Create a JUnit configuration for your tests
Create a Run configuration for your project.
And on "Before Launch": Add -> Run Another Configuration and choose the one created at point 1.
It doesn't matter how your project is packed (jar, ...)
Normally this is done by using a build management tool like: maven, gradle, ant. In this way the build tool will run tests for you and stop if they fail.
With maven, it's just a command: mvn clean package exec:java which will compile code, build project, run tests and execute your code.
See example project: https://github.com/HaveACupOfJava/maven-jar-demo
Does Maven surefire require a testng.xml file to run testng classes and methods? I have a multi module reactor setup in maven. Testng and some custom tools are located on one module and our tests are located on another module. Our structure is like this
/pom.xml
/testng-utils/pom.xml
/testng-utils/src
/tests/pom.xml
/tests/src
The tests module has a dependency on testng utils. testng-utils module brings in testng and hamcrest. Surefire is a plugin in tests/pom.xml.
I use testng annotations inside my tests, and I don't use an testng.xml file, I realize I'm providing very little information so I'm not expecting anything too in depth for an answer, just maybe a hint as to why my tests aren't being found.
Does anyone use a setup at all similar to this, and invoke testng without the use of a testng.xml file through surefire? I can try to include more information if anyone wants it if I'm able, but this is for the company I work for so I can't put much.
Thanks!
As long as you place your tests under src/test/java directory and follow the *Test.java naming convention surefire should be able to pick them up without any further configuration.
Surefire + TestNG