AlistairIsrael edited this page May 16, 2011 · 26 revisions

JPA

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());
    }
}

Database Testing Using DerbyDataSourceRule

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.

Writing Functional Tests for HTTP clients

See Functional HTTP Testing

Get it?

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>
Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.