Adding Tests with a JUnit Theory - junit

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.

Related

Create reusable mocks for model in junit test and spring boot

I am writing junit test cases for a spring boot application. I am having lot of doubts and I have listed them below.
Is it enough to write unit test cases only for service layer?
How to re-use the stubs / mocks created for the models. Each model is created with lot of dependencies. If we don't reuse them, we will be creating the same objects again and again. If we reuse how to accommodate the test values for all test cases?
Is there any best practices when creating the stubs?
Do we need to write the unit test cases for utility methods?
Rest controllers needs unit test cases?
I'll try to give the best general answers, though keep in mind that the specific case might require different approach.
No, generally speaking you should test everything that contains logic which might be subject to maintenance, and therefore unwanted changes.
I'm not quite sure on what is the problem here. The way I understood it, you want to write the stubs/mocks once and use them for multiple tests; maybe, you might use one test class and generate the stubs in a #Before, #BeforeClass annotated methods.
Well, the stub is intended to be used in place of a specific method when provided a given input. So, first, you should identify what inputs your stubbed method is going to receive and be sure you are passing them along (Note: if you provide the wrong inputs the stub won't work). Second, you need to stub the return object or the answer. Anyway, you might need to use sequential stubbing for cases when the method is called multiple times and different returns are required.
Yes, a maintenance change might cause the change in the behavior of such methods heavily affecting the product. You should always use JUnit to constraint the logic. Anyway, utility classes should be trivial and I don't expect it to be difficult for you to test them.
Like I already said, if it contains logic, yes, it should. Anyway I kinda remember there are different frameworks to mock rest calls.
Daniele

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.

JUnit global before/after

I need some way to execute code before/after each test. This code should be the same for all tests and test cases globally. I was hoping I could use RunListener, but it seems that I need to alter all the tests (or their parent class) to specify custom test runner or use JUnitCore for it to work.
Is there any way I can register this listener without affecting the code base? (there are many many tests and I would need to alter a lot of them for the listener to work)
I believe you could use a JUnit Rule feature for it. I assume that all your tests extend some basic test class. So you could add the Rule there.
I think you'll need to implement a custom rule. Quick googling reveals a pretty good tutorial.

Which PMD rules to activate for JUnit tests?

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?

Subclassing a test subject for Junit testing

I want to test validation logic in a legacy class. The class uses a method to load effective dates from a config file.
I have written a subclass of the class in question and overridden the config method so I can run my unit test against the subclass with any combination of effective dates.
Is this an appropriate strategy? It strikes me as a clean technique for testing code that you don't want to mess with.
I like it, its the most simple and straight forward way to get this done. And since it is a legacy class, it will not change anymore, so you don't run danger of bumping into the fragile base class problem neither.
It seems to be an appropriate strategy to me. Ofcourse with this override you won't
be able to test the code (in the original class) that loads the config data, but if you have other tests to cover this sceario then I think the approach you outlined is fine.