Issue
Abstract Test
public abstract class AbstractTest
User user;
@Before
public void before(){
user = new User(); //wanna run this method in TestA, TestB, ...
}
Extending test
public class TestA extends AbstractTest
User user;
@Test
public void testUser(){
AssertTrue(user.test);
}
Of course the user is not initialized. How I can initialize the user using the @Before
-method from the abstract
test class?
Solution
Declare the field only in AbstractTest and make it protected, so that child test classes (regardless of the package they're in) can access it:
public abstract class AbstractTest
protected User user;
...
Do not re-declare the field in child classes.
Answered By - david a.
Answer Checked By - Marie Seifert (JavaFixing Admin)