EclEmma code coverage plugin and PowerMockRunner - eclipse

I met a problem of using EclEmma plugin in Eclipse.
It doesn't show code coverage when I use annotation #RunWith passing (PowerMockRunner.class) as an argument. In case of using (MockitoJUnitRunner.class) everything is fine.
Waiting for your suggestions? :)

This is a known problem with Eclemma (JaCoCo). Basically both libraries modify byte code but Mockito modifies it during runtime which produces a different binary .class file. JaCoCo apparently uses keeps track of the classes it's watching via a hashMap whose hashcode is derived by the class definition so the hashcode will be different after Mockito modifies the class so JaCoco can't find the class in it's internal Maps anymore.
Discussed in more detail:
Why EclEmma doesn't coverage code with tests with #RunWith(PowerMockRunner.class)
Emma code coverage with JUnit and Powermock
http://code.google.com/p/powermock/issues/detail?id=402

Per this link last update, updating EclEmma plugin (by Uninstalling and Reinstalling) along with latest PowerMock and Mockito JARs in Eclipse (3.7) resolved the issue.
This change log confirms that.

Powermock works well with Eclemma 1.5.3; but doesn't work with latest Eclemma 3.0.0 !!
Eclipse - Kepler Service release 2
JUnit4
PowerMockito2 - 1.7.0.
Eclemma 1.5.3
The below given steps are working and I am able to see non-zero coverage % on running junit tests.
Download eclemma-1.5.3.zip from http://www.eclemma.org/download.html
Extract the contents to a local folder (eclemma-1.5.3)
Add this path to Menu bar : Window -> Preferences -> Install/Update -> Available Software Sites
Help -> Install New Software -> in "Work with" drop down, select the new entry (added in prev. step)
Uncheck the check boxes one after another then the "Eclemma Coverage" plugin options will be shown. Eclemma Coverage option will be shown in selection window.
Proceed to next and complete...
Enjoy !!!

EclEmma modifies bytecode to detect code coverage as PowerMock does to allow you to run an inaccessible code. Both changes are incompatible
The solution is to use a runner offered by PowerMock which workaround that incompatibility
http://www.notonlyanecmplace.com/make-eclemma-test-coverage-work-with-powermock/

Related

Disable import of JUnit 3 classes

I want to develop JUnit 4 tests only. When writing Unit Tests, Eclipse often imports classes from junit.framework, which is JUnit 3.
This has lead to various problems, e.g. when expecting an Exception, it simply doesn't catch it if it's in the wrong package like this:
import static org.junit.Assert.*;
import junit.framework.ComparisonFailure;
[...]
try
{
assertEquals(0, 1);
}
catch(ComparisonFailure cfe)
{
}
Strange enough, if I Ctrl+Click on ComparisonFailure, it says
Source not found
The JAR of this class belongs to container 'JUnit 4' [...]
Perhaps helpful environment information:
I don't have JUnit 3 in my build path.
Eclipse Luna 4.4.1
How can I stop Eclipse from importing JUnit 3 classes?
I have read Why is Eclipse using JUnit 3 when I have junit-4.3.1.jar in my build path?, but it's rather old and probably does not apply to Luna any more. Also, my problem is not in running the test, it's in implementing the test.
Another workaround for Eclipse's users is the following solution:
Windows -> Preferrences -> Java -> Appearance -> Type filters
and add junit.framework.* to the exclusion list.
Actually, JUnit 4 depends on some of the classes that were developed originally within JUnit 3 or reside in packages junit.*. One of such class is ComparisonFailure. If you look at latest JUnit 4.12 you will see that these packages are still there.
However, sources jar do contain java files for these classes. Perhaps your library that contains JUnit (do you use Eclipse JUnit library?) lacks source files for these? Where does your dependency (junit.jar) come from?
Which dependencies has your plugin ? Junit 3.X or 4.X ?
You could search your workspace for any references in junit 3 and change/remove them.

Eclipse plugin development: Missing dependencies at runtime

Background
I'm currently trying to extend Junit (org.eclipse.jdt) in order to do some tracing.
Therefore I created my own plugin project which (among others) provides a class which should be used by the junit runtime plugin.
I have setup my project as a dependency for junit and the package containing my class is listed as "exported" in my Manifest in the runtime tab. If I run the plugins as an eclipse application everything builds and starts allright and.
Problem
However, if in the started eclipse I perform a "Run as Junit", I get a NoClassDefFoundError concerning my class. In my understanding this means, that at runtime my class is not visible in the started eclipse. I thus printed the classpath while running and like expected my plugin doesn't show up.
Do I have to add my plugin to the "runtime" settings of the junit plugin? In the Manifest, my package is not available under the "export -> add" button. I tried importing a jar of my plugin and configuring those exports manually here, but this did not help either.
My thought process kind of gets stuck with this "run in run" configuration...
Any help is welcome!
Digging deeper in the code I found that the reason for the error is, that the part which calls my code and throws the error is run in a seperately started VM which has its classpath set via code. Nothing was wrong with the configuration, it just was not used in this case.
So if you ever stumble upon a "VMRunnerConfiguration" while having classpath problems, this might be your problem. ;)

ScalaTest Run Configuration in Eclipse: cannot find Suite Class

Problem: I cannot setup Run Configurations to run scalatest for the Scalatests in my project.
Steps to reproduce:
Right click on Scala Suite and click on Run as -> Run configurations..
On the left, I see a configuration template for ScalaTest. I click on New and fill the Name but it cannot find the suite-class.
Note: It is mentioned here that I should see Run as -> ScalaTest - Suite but I do not see that option. I tried using context menu in the editor, and in the package explorer
Steps taken:
Using: Scala IDE for Eclipse version: 2.1.0.m3-2_09
Using SBT, assemble project, run eclipse command and then import project and dependencies into Eclipse
Project compiles. ScalaTest code compiles(scalatest_2.9.2-1.8.jar is in the 'Referenced Libraries' configuration)
I've been fighting a similar problem for the past few days; Lily / Jimbo's answer didn't quite match my situation, but helped me find the right direction.
In my case, I was using a third-party library that I'd copied in. The package names of the classes and tests matched, but the folder structure did not -- all of my tests were directly in Play's "/test" folder, rather than in folders that matched the package names. This didn't show any errors, but was broken: packages ought to match folders. When I built the right folder structure underneath test, and recompiled, the expected "Run As -> ScalaTest - Suite" options showed up.
Don't know if your problem is the same, but you might check this if you haven't already found the issue...
This could be caused by a misalignment between the scalatest and the scala eclipse IDE version. Try scalatest_2.9.0-2.0.M5b.jar or scalatest_2.10-2.0.M5b.jar. The former jar definitely works with ide 2.0.9.x so maybe the new version needs the 2.10 jar. Pick your version carefully from here
Willem's answer is what worked for me. Getting both plugins from the same update site (from the list on Scalatest's github site), seemed to work for me using Kepler.
for my case, one click on 'Reimport All Maven Projects' icon, like 'Refresh' icon, solved the problem.
Not sure if you fixed the error, but I had similar a error yesterday and was pulling my hair trying to fix it (none of the suggestions I found by googling seemed to help me). So for me, it turns out that it's as easy as package hierarchy in my test suite.
I am using the play framework, so naturally my folders look like this controllers.package1.package2.... and this applies to my test folders also.
Now my test classes however, have the package definition package1.package2..... (no "controllers" as prefix).
If I run the tests on sbt/play command prompt, it's not a problem. But running them through Eclipse would give me the problem you described.
So anyways... thought I'd share this, in case this could help.
it's a bit annoying combined with the view template compile issue in play framework. but my approach is to regenerate the eclipse project file and add view template path into the class path

ClassNotFoundException when running JUnit unit tests within Eclipse (using Maven)

I have just upgraded my SpringSource Tools Suite (STS, a variant IDE of Eclipse) to the latest version (v3.6.1). Then all my JUnit unit tests can not be run again. I am getting this error:
Class not found ClassToTest
java.lang.ClassNotFoundException: ClassToTest
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
According to this blog:
ClassNotFoundException when running JUnit unit tests within Eclipse (using Maven),
it is because of some misconfiguration of Maven plugin in Eclipse. However, in STS 3.6.2 I cannot find this option "Include Modules" in the Maven plugin. How can I fix this problem and re-enable my unit tests?
There are still some back-draws on the current m2e plugin. Unit-test-cases couldn't be run as their including project grouped in a working-set. Following may help:
Right click on project including junit-tests.
Select Maven -> Disable Workspace Resolution
Try then to run your test again.
OK it seems I'll have to answer this question by myself.
The main reason for this problem is still the m2eclipse eclipse plug-in. The new version of this plugin does not support nested modules in one project. If we really need to see multiple modules we have to remove the old one from the package explorer and create a working set and import the project again using the option "import existing maven projects". The eclipse import wizard will pop up a window and ask you which module to be imported. Then we can select all the modules and finish the import. As a result the working set will contain all the modules of your project and treat each module as a separate project, although in the workspace your modules are still in one project. By doing this it resolves all the problems that I have come across in Eclipse related to classpath, such as not being able to search a class or not being able to run the unit tests and get a java.lang.ClassNotFoundException.
For reference, here is an article to teach you how to create a working set:
Working set
I think you can import the project without creating a new working set but the working set will keep all the modules that belong to your project in one set so it is easier to organize.
I wish I had the answer two months ago so I wouldn't have spent much time searching for a solution.
I think I have found the solution, at least this worked for me:
right click on the project and choose 'Maven' -> 'Update Dependencies'
Then when I tried , I did not get ClassNotFoundException .
I tried everything mentioned here and in other posts. Some of the solutions that people proffered were:
Change the output folder for the test classes.
Create a custom builder for the project that would run test-compile from Maven.
Move the Maven dependencies higher in the Order and Export list in the project build path.
There were many, many more but the one that I found to work was as follows:
Close the development environment.
Delete the jars used by the project from my local Maven repository.
Open the IDE.
Build the project.
Run the test.
After hours of beating my head against my keyboard and following suggested solutions, this one worked!
We too faced the same ClassNotFoundException while trying to run JUnit test class. But when we tried using the following steps, it successfully started running.
Select your project.
Click on the project option displayed in toolbar in the eclipse IDE.
Select “clean”.
Now try running the test file.
As a work-around, try running mvn test-compile (either in Eclipse or from the command line), then try running the Junit test within Eclipse.

Eclipse RCP: How to troubleshoot plugin dependencies & classpath problems?

I am working on an RCP project based on eclipse. It has been working fine but recently I thought I'd upgrade it to use a new eclipse version (3.2 -> 3.5).
After a bit of trouble, it was running on the new platform. Then I did something. Don't know what. The end result is that I'm now getting a classpath error when one of my plugins (A) tries to access a class in one of the dependent plugins (B) (also one of mine).
As far as I can see, Plugin A has Plugin B in its' dependency list and the compiler shows no errors. To test, I created a new Plugin C with one class and accessed the class from Plugin A. That works fine.
Does anyone have any hints for troubleshooting such issues? A checklist of settings to check? I've been struggling with this for hours and getting nowhere! Particularly frustrating as it was working until I changed something!
Thanks
Update
I should also say that the project is quite old and uses the plugin.xml and not the manifest. Could this be a problem? Is there a way to upgrade?
Some ideas:
Clean-and-build every project (maybe after a restart of Eclipse)
Check, whether the required packages are also exported
Check the Error log, maybe there is some unloadable plug-in, etc.
Check the Run configuration, whether every required plug-in is present there
If the problem does not happen in a Runtime workbench, but in an Exported RCP app, then check whether you compile the project with the same settings during export than in Eclipse
I hope, something helps about these.
You can try the following in your run configuration:
In the Main tab check Clear (workspace) under Workspace Data
In the Configuration tab check clear the configuration area before launching under Configuration Area
This helps me every time I encounter some strange ClassNotFound errors.
If I understand you correctly you have no compiler errors but when you run you get class not found exceptions? You could try PDE Tools > Update Classpath from the project's context menu to see if you declared any dependencies outside of the MANIFEST.MF file, which would result in compiling code that fails at runtime.
I developed for RCP quite a long ago, but if I'm not wrong, since 3.4 or 3.5 you have to declare in the plugin configration which packages/classes are exported for dependent plugins and which are not.
This is unlike the old convention of using 'internal' in the package to mark non-exported packages.
Since you have plugin.xml from very old version of eclipse, it might be the problem, as exported resources from one plugin to another were not enforced in Eclipse RCP 3.2.
One more complement, hope it helps.
Open plugin.xml, check "build" tab, see how "binary build" are configured. It affects which files will compiled and exported.
See binary build here : http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.pde.doc.user%2Fguide%2Ftools%2Feditors%2Fmanifest_editor%2Fbuild.htm