how can I reuse JUnit tests in another testclass?
For example:
public TestClass1 {
@Test
public void testSomething(){...}
}
public TestClass2 {
@Test
public void testSomethingAndSomethingElse() {
// Somehow execute testSomething()
// and then test something else
}
}
As usual you can:
TestClass2
from TestClass1
TestClass1
from TestClass2
using delegation:Example 1:
// Instantiate TestClass1 inside test method
public TestClass2 {
public void testSomethingAndSomethingElse1() {
new TestClass1().testSomething();
}
}
Example 2:
// Instantiate TestClass1 as a member of TestClass2
public TestClass2 {
private TestClass1 one = new TestClass1();
public void testSomethingAndSomethingElse1() {
one.testSomething();
}
}