Junit: use the same test object in multiple test classes

I have several separate test classes that use the same test object. At the moment I am using @Before in each of these classes, which is obviously not a good solution.

One option, of course, is to inherit from the abstract class that creates that object. As stated here , this is not a good idea either.

Another option would be an external resource, but this is - as the name says - for resources, not test objects.

I feel like I'm missing something as this should be the main focus in JUnit.

+3


source to share


4 answers


You may be looking for TestRule

. To quote the Javadoc:

A TestRule

is a change in how a test method or set of test methods is started and reported. A TestRule

can add additional checks that trigger a test that would otherwise fail, or it can do the necessary tweak or cleanup for the tests, or it can watch the test run to report it elsewhere. TestRules can do all that could have been done earlier by methods annotated using the Before

, After

, BeforeClass

or AfterClass, but they are more powerful and more easily shared between projects and classes.



Then he will list several types, of which ExternalResource

one; but there are others.

Considering that you are talking about doing something better than using methods @Before

, and you want to share between classes, this is very similar to what you are describing.

+3


source


Congratulations, you've discovered something that has improved in your design! This is a great result when writing tests.

You say you are using the same test object across multiple tests. I am assuming your tests are testing different classes. I expect you to find some common behavior in those different classes that can abstract away from the new class.



After that, your test object will test the new class, you can remove it from other tests, and any behavior remains different every time. Seems like a good result!

+1


source


You can add a generic class to your test suite where you can make it static. This will make it easier to use in your tests because of the static link. But I'm not sure if it gives a gain in testing time consumption ...

In the case of the unit test, I actually think the time gain with a static object is less, because it's all rebuilt from scratch again with each test class, but I can remember where the JVM makes things faster. (Pls who can confirm or otherwise tell me how interesting I am)

0


source


Don't take everything you read on interwebz at face value. Everyone has an opinion and writes about it on the blog, so. Inheritance may not be the best solution to the problem you are facing, but it is the solution. The alternative solutions are not prettier. I have tested tests with inheritance several times and they work like a charm. Just don't go crazy with it, don't create entire inheritance hierarchies and you should be fine.

If you really want to stay away from inheritance, you can make it work with parameterized tests. I assume you are using JUnit 4, so you can read about them here: org.junit.runners Class Parameterized

Parameterized tests weren't exactly invented to make you want them; they instantiate cross-product test methods of the same class and some test data items you supply, but if you are using one item and if you are using a factory to create the same item for all of your test classes then you could achieve the desired effect.

Personally, I would prefer inheritance.

0


source







All Articles