Which PMD rules to activate for JUnit tests? - junit

I'm in the middle of setting up PMD as a tool in our team to support us writing better code. Basically I'm building Ant scripts and try to set up some rules for everyone to use.
But right now I hit this problem:
When I write JUnit tests I don't want to use the same rules I apply on our main source code. I don't care that much about String rules (like string dupliates or weird instantiations) in the junit tests.
My questions is:
Is that a fault on my side and should I start writing better JUnit tests?
Should I provide a 2nd set of rules that disables some of the string/design/finalizers rules?

The second option - I don't run PMD against my tests at all. I could and PMD provides some JUnit specific rules. I would definitely use a separate ruleset against the test code though. I expect more String literals and some thing specified instead of using conditionals/loops. After all, I don't want to duplicate the code I am trying to test.

Two things. Why are you trying to set up rules why not using the existing rules? (Special requirements?). And second yes of course Unit tests should have a good quality as well. Your Unit test test you production code so shouldn't they have at least the same quality as your production code?

Related

How can I filter a few methods to be analysed for code coverage

I using Jacoco as code-coverage plugin configured inside my pom.xml. I want to test and analyse coverage of only a few methods from my class file and want to show coverage percentage accordingly for them only. But as jacoco analyse whole file it shows less coverage, though the methods concerned are covered 100%.
Is there any way out in jacoco to exclude some methods being analysed without changing source file code?
That's not possible. Jacoco allows inclusions and exclusions at class level but not at method level.
There is some support for filtering at method level, discussed here. This allows Jacoco to ignore extraneous byte code generated by the Java compiler. On a similar note; Jacoco can also ignore some generated code on the basis of annotations (such as code generated by Lombok)
Although there is currently no way to tell Jacoco (via the Maven plugin, for example) to ignore specific methods, there are some open Jacoco issues related to this:
Filtering options for coverage analysis
Investigate filtering with annotations
You could perhaps vote for those and/or raise another issues for your specific requirements.
It is not clear why you "want to test and analyse coverage of only a few methods from my class file and want to show coverage percentage accordingly for them only."
May be you have some code which is not related to main class? In this case think about design. One of possible solution is to split your class to parent and child or main class and some utilities.
May be 2 developers are working with the same class you each wants to show only own results?
May be some code hard to test? Try the mocking way.

How to take screenshot on test failure with junit 5

Can someone tell me please: how to take a screenshot when test method fails (jUnit 5). I have a base test class with BeforeEach and AfterEach methods. Any other classes with #Test methods extends base class.
Well, it is possible to write java code that takes screenshots, see here for example.
But I am very much wondering about the real problem you are trying to solve this way. I am not sure if you figured that yet, but the main intention of JUnit is to provide you a framework that runs your tests in various environments.
Of course it is nice that you can run JUnit within your IDE, and maybe you would find it helpful to get a screenshot. But: "normally" unit tests also run during nightly builds and such - in environments where "taking a screenshot" might not make any sense!
Beyond that: screenshorts are an extremely ineffective way of collecting information! When you have a fail, you should be locking for textual log files, html/xml reports, whatever. You want that failing tests generate information that can be easily digested.
So, the real answer here is: step back from what you are doing right now, and re-consider non-screenshot solutions to the problem you actually want to solve!
You don't need to take screen shots for JUnit test failes/passes, rather the recommended way is to generate various reports (Tests Passed/Failed Report, Code coverage Report, Code complexity Report etc..) automatically using the below tools/plugins.
You can use Cobertura maven plugin or Sonarqube code quality tool so that these will automatically generate the reports for you.
You can look here for Cobertura-maven-plugin and here for Sonarqube for more details.
You need to integrate these tools with your CI (Continuous Integration) environments and ensure that if the code is NOT passing certain quality (in terms of tests coverage, code complexity, etc..) then the project build (war/ear) should fail automatically.

Adding Tests with a JUnit Theory

Is there any way to use a rule to add additional tests? For example, I would like to convert the Theories runner to a rule, but I haven't been able to find a way to make Rules add additional tests.

Can you have 2 different suites in 1 JUnit TestCase Class?

I'd like to define two different suites in the same JUnit TestCase Class; one for behaviour tests and another for efficiency tests. Is it possible?
If yes, how? If not, why not?
Additional details: I'm using JUnit 3.8.1.
If I understand you correctly, you're trying to partition your tests. Suites, on their own, are not really the mechanism you need, rather it's JUnit's categories you need to investigate:
http://java.dzone.com/articles/closer-look-junit-categories
I've not used these as I've usually found the overhead of test partitioning too much effort, but this may work for you. I think TestNG has supported this concept for quite a while.
Also, if you're using Maven you get a partitioning of tests into unit and integration tests for free - check out the Failsafe plugin - which is good for separating tests you want to run quickly as part of every build from longer running tests.

Conditional skipping of unit tests

I'm currently working on a class, dealing with network issues. Using JUnit 3.8.1 and having a hardware device, that's not always around to test against, I'd like to conditionally suppress individual tests. Is there a way to achive this with a simple annotation like #if(!gatewayAvailable) -> test's suppressed?
Thanx for any pointers, marcus
There is no such feature in JUnit 3.8.1. You have to use JUnit4 and its Assume class.