I tried to get a good coverage file in my java spring project, I mean; see where my tests are good and where they are not. But my problem is that #Data generate a lot of function/class that I don't want to test, and just ruin my coverage.
I already search for a solution, but there is nothing to fix my problem. Looks like Jacoco have something to fix my problem but not Pitest. Maybe Im just not taking the problem right.
Since version 1.2.1 Pitest ignores any method or class annotated with Generated.
You can configure Lombok to add the annotation by adding the following in lombok.config :
lombok.addLombokGeneratedAnnotation = true
Related
After running test and generating coverage report via Jacoco, I realized that there is a static method where a #Data annotation is used and the line of it is marked yellow indicating that it is not tested.
Report
So, is it normal or how can I test that line?
jacoco can ignore code generated by lombok.
put lombok.config in your project’s root
lombok.addLombokGeneratedAnnotation = true
jacoco Release 0.8.0 (2018/01/02)
Methods annotated with #lombok.Generated to better integrate with Lombok >= 1.16.14. Initial analysis and contribution by Rüdiger zu Dohna (GitHub #513).
Methods annotated with #groovy.transform.Generated to better integrate with Groovy >= 2.5.0. Thanks to Andres Almiray for adding the annotation to Groovy (GitHub #610).
jacoco Release 0.8.2 (2018/08/21)
Classes and methods annotated with annotation whose retention policy is runtime or class and whose simple name is Generated are filtered out during generation of report (GitHub #731).
Seems opinion based, but in my opinion there is no point for testing generated code from an external dependency. I would do noting about it and move on.
My aplication uses Spring 4.3.1 and JUnit 4.12.
I have been trying test my methods with JUnit test cases.
I placed the #RunWith(SpringJUnit4ClassRunner.class) annotation in my test class, but eclipse complains "Class cannot be resolved to a type".
I read that this was enough for Eclipse to recognize SpringJUnit4ClassRunner, but isn't.
Is there Anything else remaining to do?
Try to:
Add spring-starter-test dependency through your configuration file & include relevant import statements in test files.
Delete local dependency repository & force new build to your application.
Adding the dependency spring-test the problem has been solved.
I didn't know there was need for this additional dependence, but someone told me here. Thank you.
I initially said that this measure was not enough because I had written the dependency version name wrongly and Maven was not finding the resource. But now it works!
I have a simple Java program which uses the Lombok Annotation - #Builder.
I am testing my code using junit and everytime I run my unit tests, my coverage is always below 50% inspite of the fact that I am testing my entire code.
I looked into the junit generated code coverage and I saw that it was the Lombok annotation which was making the coverage drop.
I see something like:
toString() - 0%
build() - 0%
MyMethod.MyMethodBuilder() - 0%
How do I test these methods for the #Builder annotation? Or the only way to improve coverage is to exclude those from the test coverage?
Starting JaCoCo v0.8.0, you should create lombok.config file to the root of your project and add following content to it:
lombok.addLombokGeneratedAnnotation = true
This will add #Generated annotation to Lombok generated files and JaCoCo will know that code coverage of such a code should be ignored.
Credits to original author here
Create lombok.config file and add texts below:
config.stopBubbling = true
lombok.addLombokGeneratedAnnotation = true
config.stopBubbling = true is telling Lombok that this is the root directory and that it shouldn’t search parent directories for more configuration files (you can have more than one lombok config files in different directories/packages).
lombok.addLombokGeneratedAnnotation = true is telling Lombok to add the #lombok.Generated annotation to all generated methods.
Decompile class which contains lombok annotation to see how lombok has generated source code and then write junits considering that code.
e.g. In case of #ToString, make explicit call to toString method on class object i.e. obj.toString();
Better to use pl.pojo library to test the pojo model classes.
Sonarqube generates issues as
TestCases should contain tests
regarding that test classes does not have any tests inside but there are methods marked with #Test annotation and classes annotated with #RunWith(SpringJunit4ClassRunner.class).
I have no problem running TestSuites and importing unit test and coverage reports into sonarqube. I checked sonarqube source code and it should find annotations but unfortunately it does not. Rule id for the mentioned issue is S2187.
Is there a configuration for this rule to find annotated methods or is a known issue?
Edit - config:
sonar.projectKey=x
sonar.projectName=x
sonar.projectVersion=1.0
sonar.modules=a,b,c
a.sonar.projectBaseDir=modules/a
b.sonar.projectBaseDir=modules/b
c.sonar.projectBaseDir=modules/c
sonar.sources=src
sonar.tests=test
sonar.java.binaries=../**/classes/production,../**/classes/test
sonar.java.libraries=../../libraries/repository
sonar.junit.reportsPath=junit/
sonar.java.coveragePlugin=jacoco
sonar.jacoco.reporthPath=coverage/jacoco.exec
sonar.language=java
sonar.sourceEncoding=UTF-8
Issue has been fixed by correctly setting sonar.java.test.libraries/binaries attributes as answered in https://stackoverflow.com/a/32197912/343806. They were not not in the runner configuration and after adding raised issues in sonarqube results marked as fixed.
I just used MyEclipse to automatically generate some JUnit test cases. One of the generated methods looks like this:
#Ignore("Ignored") #Test
public void testCreateRevision()
{
fail("Not yet implemented"); // TODO
}
I added the #Ignore annotation manually. However, when I run the test, JUnit lists that method, and others like it, under "failures," rather than ignoring them (related: What's the difference between failure and error in JUnit?). And it displays the "Not yet implemented" message instead of the "Ignored" message. Clearly, fail() must be getting called, and therefore, the #Ignore assertion is not working.
What's going on here? Is there a setting I need to enable for this to work?
EDIT :
Things I have considered/tried so far:
I am using JUnit 4, so it's not a version problem.
I am importing org.junit.Ignore, so it's not a case of the wrong Ignore being used.
I have tried using #Ignore alone, #Ignore #Test and #Ignore("message") #Test; all fail.
EDIT 2 :
I created the tests with MyEclipse, via New > Other; Java > JUnit > JUnit Test Case; New JUnit 4 test, and the library in my build path is JUnit 4. I'm building with ant and actually running the case with MyEclipse.
Make sure you are importing the right #Ignore. To be sure use #org.junit.Ignore explicitly.
Double check if your test is being executed by JUnit 4, not 3. The easiest way to do this is to either change the test name so it is not prefixed by test (now it shouldn't be executed at all and JUnit 4 does not need this prefix anyway) or examine your test case inheritance hierarchy: it shouldn't extend directly or indirectly from junit.framework.TestCase (Junit 3 requirement).
I had this problem also even though JUnit 3 was not on my classpath. I believe that compatibility mode on Junit 4 picks up on the 'test' prefix in your testname and thus operates as JUnit 3 would rather than picking up the #Ignore. The solution is to rename your test.
In JUnit5 use #Disabled or #Disabled("Reason text")
Are you sure the test classes were recompiled?
It's a quite common problem, that the recompilation fails because there was typo somewhere in the sources (like a missing semicolon), and the IDE does not tell you that compiling failed.
Try deleting the target/test-classes folder.
In my case I found my IDE was executing the test disregarding the #Ignore annotation. When I ran mvn install (or any other maven phase) the test was skipped and that's what I was actually aiming for (see attached ilustration).
Ilustration of ways of execution
I think its just #Ignore that will skip the test
JUnit Ignore
Ensuring the test class is also imported fixed the issue for me i.e.
import org.junit.Ignore;
import org.junit.Test;