Why EclEmma doesn't coverage code with tests with #RunWith(PowerMockRunner.class) - java

I am using EclEmma with Eclipse to help me to know where is missing code tests in my project, but all tests with #RunWith(PowerMockRunner.class) are not called and thus not tested.
I´m using JUnit 4.8.1 with Mockito.
What could it be?

Its a known bug reported for both parties:
http://code.google.com/p/powermock/issues/detail?id=402
https://github.com/jacoco/eclemma/issues/15#issuecomment-9565210
eCoberture seems however to provide correct coverage. The only problem, that it seems not to be maintained anymore, and you cannot remove the highlights im Eclipse Juno.

Here you can find example that works and may help you solve this problem
https://github.com/Godin/jacoco-experiments
use mvn clean package to see jacoco report

We have a static classes to mock. With mocking static classes, eclEmma code coverage plugin is not working in Eclipse. So what we did is, so placed #RunWith(JUnit4.class) (Instead of #RunWith(PowerMockRunner.class) ) before class and placed following lines inside class
static {
PowerMockAgent.initializeIfNeeded();
}
#Rule
public PowerMockRule rule = new PowerMockRule();
Compiled the class and ran the test class. Code coverage is working fine for class. This change is only to run eclEmma plugin in Eclipse IDE with no issues.
After writing test cases, we reverted code back to normal. Placed #RunWith(PowerMockRunner.class) instead of #RunWith(JUnit4.class) and commented above static code and powermockrule lines.

AFAIK eclEmma, as well as many other coverage systems, modify your .class files to add coverage instructions. Most of these tools do that at "compile time", not at run time.
PowerMock instead, as well as AspectJ LTW and many other systems, manipulate the same bytecode but at "run time":
PowerMock is a framework that extend other mock libraries such as EasyMock with more powerful capabilities. PowerMock uses a custom classloader and bytecode manipulation to enable mocking of static methods, constructors, final classes and methods, private methods, removal of static initializers and more.
I have a similar problem with both eclEmma (various versions) and Cobertura in combination with AspectJ LTW, cause when the runtime modification of .class files happen, it somehow corrupts the modification done previously by the coverage tool.
I don't have yet found a solution, but at least found the symptom.
The right solution would be to debug PowerMock instrumentation and find out where and how it breaks coverage tools. It's quite a problem, for a testing tool, to break coverage tools, since the two are quite often used together :)

Related

How to list all classes without test in IntelliJ IDEA?

How to list all classes without tests in IntelliJ IDEA?
if we already can,
jump from a test and its source
create a test for a class?
Gradle task jacoco helps, and it what I am using, but I still want to know if there is something built-in in Intellij IDEA.
You could run your test suite with code coverage:
https://www.jetbrains.com/help/idea/running-test-with-coverage.html
https://www.jetbrains.com/help/idea/viewing-code-coverage-results.html

Can I run PMD analysis in a Junit test?

I would like to create an automated junit test that passes if my code meets the requirements specified by PMD, and fails if it does not. Is this possible?
It is possible tu run a PMD analysis in a JUnit test. However, there exist PMD plugins for every Java IDE, build and CI tool out there. Are you sure they don't already do what you want to achieve?
If you still want to do it yourself, take a look at the class PMD. It has the static method PMD.doPMD(configuration) which starts the analysis.
The same class also contains the main method in case you want to start it like from the command line.

Getting test coverage results with Cobertura

I'm using Cobertura 2.3.0 from http://cobertura.github.io/cobertura/ to analyze my project for test coverage, and I'm getting a
Error: Could not find or load main class net.sourceforge.cobertura.instrument.Main
When trying to execute cobertura-instrument.bat from the command line. I've looked at this batch file and it's trying to load several JARs from the %COBERTURA_HOME%/lib folder that don't exist. I've manually downloaded these JARs but am getting other strange errors.
java.lang.IncompatibleClassChangeError: class net.sourceforge.cobertura.instrument.pass1.DetectIgnoredCodeClassVisitor has interface org.objectweb.asm.ClassVisitor as super class
Is it possible that this release was incorrectly updated? I don't see a way to make this work any other way. Further, does anybody know of a working test-coverage utility, preferably a working different version of Cobertura?
I've tried Nounit and several others, but have not gotten the type of clean output that I know Cobertura can produce.
Answering my own question in hopes it can help somebody.
At time of writing, there is a known bug with Cobertura throwing a ClassNotFoundException in certain circumstances. See the following issue tracker page: https://github.com/cobertura/cobertura/issues/74#issuecomment-41383903
For the time being, I've gone with EclEmma Eclipse plugin for EMMA for my test code coverage, and got nice, pretty results the way I wanted.

Merging jUnit coverage reports with EclEMMA plugin

I am measuring the code coverage in my project using the EclEmma plugin for Eclipse. This involves running the coverage for the whole project. But due to some dependency issues tests in some packages are failing altogether. When the coverage for these package is taken individually, the tests run properly and the package is showing the coverage correctly.
Is it possible to get a Coverage report, by running the coverage for each package separately and then merging these reports into one.
Or Alternatively, are there any other free plugins which offer the above capability.
Note: Modifying the test methods to remove the dependency may not be possible due to logical and size constraints.
It's been a while since I shifted to IDEA, but I seem to recall that there was an option (as in "button in the EclEMMA view") to merge several coverage runs.
A visit to http://www.eclemma.org/ confirms this - look for "Merge Sessions". Also:
http://www.eclemma.org/userdoc/sessions.html and
http://www.eclemma.org/userdoc/coverageview.html
It is the button to the right of the "double-X" Remove all sessions button.
Cheers,

Code coverage of java code tested by groovy code

I've a small project where the main code is wrote in java but the tests are mostly groovy code. If i use JaCoCo for code coverage (called by Sonar), the code coverage is not done on my java code, as it seems JaCoCo does not use its special classloader when loading the java classes with groovy. Strangely, I remember it worked before (before sonar version 2.12 which embed JaCoCo).
Does anyone know how to fix it ?
My mistake: forgot to add the integration-test maven profile to the sonar configuration in jenkins.

Categories