Before I knew about "@RunWith(Enclosed.class)" approach, I used the following (similar) solution, with inner classes extending outer class. I keep using this structure because I like that the tests are in same place and share some properties and methods and things seems clearer to me. Then, using Eclipse, in my run configuration, I choose that option "Run all tests in the selected project, package or source folder" and all these tests will be performed with just a click.
public class TestBooksDAO {
private static BooksDAO dao;
@Parameter(0)
public String title;
@Parameter(1)
public String author;
@Before
public void init() {
dao = BooksDAO.getInstancia();
}
/** Tests that run only once. */
public static class SingleTests extends TestBooksDAO {
@Test(timeout=10000)
public void testGetAll() {
List<Book> books = dao.getBooks();
assertNotNull(books);
assertTrue(books.size()>0);
}
@Test(timeout=10000)
public void testGetNone() {
List<Book> books = dao.getBooks(null);
assertNull(books);
}
}
/** Tests that run for each set of parameters. */
@RunWith(Parameterized.class)
public static class ParameterizedTests1 extends TestBooksDAO {
@Parameters(name = "{index}: author=\"{2}\"; title=\"{0}\";")
public static Collection<Object[]> values() {
return Arrays.asList(new Object[][] {
{"title1", ""},
{"title2", ""},
{"title3", ""},
{"title4", "author1"},
{"title5", "author2"},
});
}
@Test(timeout=10000)
public void testGetOneBook() {
Book book = dao.getBook(author, title);
assertNotNull(book);
}
}
/** Other parameters for different tests. */
@RunWith(Parameterized.class)
public static class ParameterizedTests2 extends TestBooksDAO {
@Parameters(name = "{index}: author=\"{2}\";")
public static Collection<Object[]> values() {
return Arrays.asList(new Object[][] {
{"", "author1"},
{"", "author2"},
{"", "author3"},
});
}
@Test(timeout=10000)
public void testGetBookList() {
List<Book> books = dao.getBookByAuthor(author);
assertNotNull(books);
assertTrue(books.size()>0);
}
}
}