Can I run PMD analysis in a Junit test? - java

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.

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

Is there an auto highlight for runtime used code

Does eclipse has a feature or plugin that shows the used code branch for an execution?
I known about coverage plugins for junit test, but I would like one for normal runtime execution instead of unit tests.
The EclEmma code coverage plug-in is not specific to JUnit executions. Measuring code coverage is an added launch mode, like Run, or Debug.
https://www.eclemma.org/userdoc/launching.html

How to specify which #Tag should be used for JUnit in IntelliJ IDEA

Is there a way to specify from the build in JUnit5 Runner of IntelliJ that only test methods with specific #Tag should be tested (or the opposite, that all tests except those with a specific #Tag, should be executed)?
I know how to do it with maven, but is it possible with just the IntelliJ UI?
Best regards,
Dieter
Now it is possible with Intellij IDEA 2018.1:
See Tag Expressions to select which tests to execute according with their tagging.
Also, you can take a look at this answer for details on how to achieve the same goal using maven-surefire-plugin.
With https://youtrack.jetbrains.com/issue/IDEA-163481 fixed, it is now possible since IJ 2018.1 -- for details see #Nicolau 's answer.

Eclipse plugin to run specific method

I am making plugin in Eclipse that would run selected method with specific parameters (right click on method in package explorer and choose "Check" runs selected method with specific parameters and shows results).
What would be the best way to run selected method without having to compile whole project and use reflection (as project might not be complete and might not even compile yet)? I will also have to use EMMA.
Take a look on JUnit plugin and create similar implementation. I believe that it provides almost what you need except the fact that it works with JUnit tests only.

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

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 :)

Categories