Solution #1
You can use inheritance or composition. E.g. you can create TestBase
class:
class TestBase {
@Before
protected setUp() {
// code for all tests
}
}
and then your tests can derive from it:
@RunWith(RobolectricTestRunner.class)
class SpecificTest extends TestBase {
...
}
Solution #2
You can also create some Utility class containing repeatable code in a method and call this method in every setUp()
method in your tests. Then, you won't need inheritance. Example:
class Util {
private Util() {}
public static setUp() {
// code for all tests
}
}
and your test could look like that:
@RunWith(RobolectricTestRunner.class)
class SpecificTest {
@Before
public setUp() {
Util.setUp();
}
}