Port of schuchert.wikispaces.com


hades.BaseEnvironment

hades.BaseEnvironment

These instructions summarize/make heavy use of http://www.vogella.de/articles/JavaPersistenceAPI/article.html.

(These next two steps are here for convenience, they are taken directly from http://www.vogella.de/articles/JavaPersistenceAPI/article.html

Follow the instructions in the tutorial as is with the following three changes.

@SuppressWarnings("unused")
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
package shoe;

import static org.junit.Assert.*;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import de.vogella.jpa.simple.model.Todo;

public class SmokeTest {
	private EntityManagerFactory factory;
	private EntityManager em;
	private Query findAll;

	@Before
	public void setupDb() {
		factory = Persistence.createEntityManagerFactory("todos");
		em = factory.createEntityManager();
		findAll = em.createQuery("select t from Todo t");
		em.getTransaction().begin();
	}

	@After
	public void wipeTheSlateClean() {
		em.getTransaction().rollback();
		em.close();
		factory.close();
	}

	@Test
	public void initiallyEmpty() {
		assertEquals(0, getAllTodos().size());
	}

	@Test
	public void oneAddedShouldIncreaseCount() {
		Todo todo = new Todo();
		em.persist(todo);
		assertEquals(1, getAllTodos().size());
	}

	private List<Todo> getAllTodos() {
		@SuppressWarnings("unchecked")
		List<Todo> resultList = findAll.getResultList();
		return resultList;
	}
}

Comments

" Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.