This repository has been archived by the owner on Jun 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Home
AlistairIsrael edited this page May 16, 2011
·
26 revisions
See JPA + EJB 3 Testing for a more in-depth tada!
@Fixtures("widgets.xml")
public class WidgetDaoTest {
@Rule
public HibernatePersistenceContext persistenceContext = new HibernatePersistenceContext();
@Test
public void testListAll() {
WidgetDao widgetDao = new JpaWidgetDao(persistenceContext.getEntityManager());
List<Widget> widgets = widgetDao.listAll();
assertFalse(widgets.isEmpty());
}
}
public class UserDaoTest {
@Rule
public DerbyDataSourceRule derby = new DerbyDataSourceRule();
@Before
public void setUpDatabase() {
derby.execute("CREATE TABLE users (id INTEGER NOT NULL"
+ "GENERATED ALWAYS AS IDENTITY, name VARCHAR(20) NOT NULL UNIQUE)");
}
@After
public void tearDownDatabase() {
derby.execute("DROP TABLE users");
}
@Test
public void testCreate() {
UserDao userDao = new UserDaoImpl(derby);
assertEquals(0, derby.count("users"));
userDao.createNewUser("user1");
assertEquals(1, derby.count("users"));
}
}
NOTE that derby.count() and derby.execute() are currently only available in 0.5.1-SNAPSHOT.
Maven.
<project>
<dependencies>
<dependency>
<groupId>junit-rules</groupId>
<artifactId>junit-rules</artifactId>
<version>0.5</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>maven2.github.com</id>
<url>http://maven2.github.com</url>
</repository>
</repositories>
</project>