1 package dst.ass1.jpa.examples;
3 import static org.hamcrest.CoreMatchers.is;
4 import static org.junit.Assert.assertThat;
8 import javax.persistence.EntityManager;
10 import org.junit.Rule;
11 import org.junit.Test;
13 import dst.ass1.jpa.ITestData;
14 import dst.ass1.jpa.ORMService;
15 import dst.ass1.jpa.dao.ICoursePlatformDAO;
16 import dst.ass1.jpa.model.ICoursePlatform;
17 import dst.ass1.jpa.model.IModelFactory;
20 * This examples shows how you can write your own tests with custom fixtures that are injected it into the
21 * {@link ORMService} rule. Using rules instead of abstract tests is considered good practice as it follows the
22 * principle of composition over inheritance.
24 public class ExampleTestWithCustomTestData {
27 public ORMService orm = new ORMService(new MyTestData());
30 public void coursePlatformDAO_findAll_returnsCorrectValues() throws Exception {
31 ICoursePlatformDAO coursePlatformDAO = orm.getDaoFactory().createCoursePlatformDAO();
33 List<ICoursePlatform> all = coursePlatformDAO.findAll();
35 assertThat(all.isEmpty(), is(false));
36 assertThat(all.size(), is(2));
38 System.out.println(all);
42 public void coursePlatformDAO_findById_returnsCorrectValue() throws Exception {
43 ICoursePlatformDAO coursePlatformDAO = orm.getDaoFactory().createCoursePlatformDAO();
45 ICoursePlatform actual = coursePlatformDAO.findById(1L);
47 assertThat(actual.getUrl(), is("http://cp1.example"));
50 public static class MyTestData implements ITestData {
53 public void insert(IModelFactory modelFactory, EntityManager em) {
54 ICoursePlatform cp1 = modelFactory.createCoursePlatform();
56 cp1.setUrl("http://cp1.example");
58 ICoursePlatform cp2 = modelFactory.createCoursePlatform();
60 cp2.setUrl("http://cp1.example");
65 em.flush(); // transaction is done through the ORMTestFixture