I am trying to write integration tests and want to make it in a best way regarding programming best practices, etiquette, etc.
First I had a question on how to reuse existing tests in another test class and I found the answer in topic How to reuse existing JUnit tests in another test class?
After moving the base test functionality and relevant member fields into the base class I found out that I need to make the member fields protected or I need to have their appropriate getters and setters in order I could use them in inherited test classes. Here is an example:
public abstract class BaseTests {
private Class classObj; // or protected without getter/setter?
protected void somethingHelper() {
// Test something
}
protected void somethingElseHelper() {
// Test something for classObj
}
public Class getClassObj() {
return classObj;
}
public void setClassObj(Class other) {
classObj = other;
}
}
public class TestClass1 extends BaseTests {
@Test
public void testSomething() {
somethingHelper();
}
}
public class TestClass2 extends BaseTests {
@Test
public void testSomethingAndSomethingElse() {
somethingHelper();
ClassObj obj = new ClassObj();
// set obj member fields
// and then test something else
setClassObj(obj); // or classObj = obj;?
somethingElseHelper();
}
}
My questions are:
- Is it a good practice to have member fields in base test class and use them in inherited test classes?
- If yes, what is better: protected member fields or getters/setters?
- If no, is there another way to achieve what I wanted in example?