简体   繁体   中英

Junit: Use same test object in multiple test classes

I have multiple, individual test classes which all use the same test object. At the moment I'm using @Before in each of these classes, which is obviously not a very good solution.

One option of course is to inherit from an abstract class which creates this object. As stated here , this is not a good idea as well.

Another option would be External Resource, but that is - as the name says - for resources rather than test objects.

I feel like I'm missing something since this must be a basic task in JUnit.

You might be looking for a TestRule . To quote the Javadoc:

A TestRule is an alteration in how a test method, or set of test methods, is run and reported. A TestRule may add additional checks that cause a test that would otherwise fail to pass, or it may perform necessary setup or cleanup for tests, or it may observe test execution to report it elsewhere. TestRules can do everything that could be done previously with methods annotated with Before , After , BeforeClass , or AfterClass, but they are more powerful, and more easily shared between projects and classes.

It then goes on to list a few types, of which ExternalResource is one; but there are others also.

Given that you talk about doing things better than using @Before methods, and you're wanting to share between classes, this sounds very much like what you are describing.

Congratulations you've discovered something to improve in your design! This is an excellent result from writing tests.

You say you are using the same test object in multiple tests. I assume your tests are testing different classes. I expect you have uncovered some common behaviour in those different classes that could abstract out into a new class.

Afterwards your test object would test the new class, you could remove it from th other tests, and whatever behaviour remains is different each time. Seems like a good outcome!

You can add a general class in your test package where you make it available static. It will make it easier to use it in your tests due to static reference. But I'm not sure if it gives a win in test time consumption...

In case of unit test I actually think the win in time with the static object is less because it's all being built up from scratch again each test class, but I can remember somewhere the JVM has it done quicker. (Pls anyone who can confirm or otherwise, tell me as I'm curious about this)

Do not take everything that you read on the interwebz at face value. Everyone has an opinion and writes about it in a blog, so what. Inheritance may not be the nicest solution to the problem that you are facing, but it is a solution. Alternative solutions are not any prettier. I have tried tests with inheritance on multiple occasions, and they work like a charm. Just don't go crazy with it, don't create entire inheritance hierarchies, and you will be fine.

If you really want to stay away from inheritance, you might be able to make it work with Parametrized Tests . I presume you are using JUnit 4, so you can read about them here: org.junit.runners Class Parameterized

Parametrized tests were not exactly invented for the purpose that you want them; they create instances for the cross-product of the test methods of a single class and some test data elements that you supply, but if you use a single data element, and if you use a factory to instantiate the same data element for all of your test classes, then you could possibly achieve the effect that you want.

Personally, I'd still prefer inheritance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM