Skip to content

Instantly share code, notes, and snippets.

@rsds143
Created December 2, 2015 18:59
Show Gist options
  • Select an option

  • Save rsds143/7838f03ccfb7c9586681 to your computer and use it in GitHub Desktop.

Select an option

Save rsds143/7838f03ccfb7c9586681 to your computer and use it in GitHub Desktop.
sharing junit tests between different repository types
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
/**
* Created by rsvihla on 12/2/2015.
*/
public abstract class BaseIntegrationTest {
protected abstract DataRepository getDataRepository();
private MyService service;
@Before
public void before(){
service = new MyService();
service.setRepository(getDataRepository());
}
@Test
public void testGetInts(){
assertThat(service.getAllInts().size(), is(0));
}
@Test
public void testSaveInts(){
service.saveInt(1);
assertThat(getDataRepository().getAllInts().get(0), is(1));
}
}
import org.junit.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
/**
* Created by rsvihla on 12/2/2015.
*/
public abstract class BaseTestCase {
protected abstract DataRepository getDataRepository();
@Test
public void testGetInts(){
assertThat(getDataRepository().getAllInts().size(), is(0));
}
@Test
public void testSaveInts(){
getDataRepository().saveInt(1);
assertThat(getDataRepository().getAllInts().get(0), is(1));
}
}
class CassandraIntegrationTest extends BaseIntegrationTest {
@Override
protected DataRepository getDataRepository() {
return new CassandraDataRepository();
}
}
public class CassandraTest extends BaseTestCase{
@Override
protected DataRepository getDataRepository() {
return new CassandraDataRepository();
}
}
class OracleIntegrationTest extends BaseIntegrationTest {
@Override
protected DataRepository getDataRepository() {
return new OracleDataRepository();
}
}
public class OracleTest extends BaseTestCase{
@Override
protected DataRepository getDataRepository() {
return new OracleDataRepository();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment