I have an interface that defines a contract (i.e. a Repository
), with few implementations. Each method in the interface represents a feature, and I would like to test each feature in its suite test class.
Let's assume a UserRepository
interface as follows:
public interface UserRepository {
Set<User> search(String query);
Set<User> findBySomethingSpecific(String criteria1, Integer criteria2);
}
At the moment, to ensure I run the same test cases, I create an abstract test class, and each of my implementations have a test class that extends the abstract test class.
public abstract UserRepositoryTest {
private UserRepository userRepository;
@Before
public void setUp() {
userRepository = createUserRepository();
}
@Test public void aTestForSearch() { ... }
@Test public void anotherTestForSearch() { ... }
@Test public void aTestForSomethingSpecific() { ... }
@Test public void anotherTestForSomethingSpecific() { ... }
protected abstract UserRepository createUserRepository();
}
//------------------------
public class UserRepositoryImplementationTest extends UserRepositoryTest {
@Override
protected UserRepository createUserRepository() {
return new UserRepositoryImplementation();
}
}
I would like to find a way to divide this abstract test class into a set of small tests, because the test class becomes rapidly overwhelmed. I've looked at test suites, but I don't understand how can I create a Suite test class by injecting my different implementations.
As a side not, I've found this question, but some of my repositories require some logic at its creation (for instance, ConnectionPool
for a SQL implementation). I currently use the anti-pattern ServiceLocator
with different Context
classes to handle the creation, but this is static
. That's why I had an approach of a test class by implementation, so I can create the context and inject it afterward.