AlistairIsrael / junit-rules

Provides a set of rules that showcase JUnit's @Rule functionality

Home | Edit | New

Home

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 testFindAll() {
        WidgetDao widgetDao = new JpaWidgetDao(persistenceContext.getEntityManager());

        List<Widget> widgets = widgetDao.findAll();
        assertFalse(widgets.isEmpty());
    }
}

Sun HttpServer Functional Test

public class HttpTest {

    @Rule
    public final HttpServerInterceptor httpServer = new HttpServerInterceptor(8000);

    @Test
    public void testHttp() throws Exception {

        httpServer.addHandler("/1234.xml", new SimpleHttpHandler() {
            protected void onGet() throws IOException {
                PrintWriter out = getResponseWriter();
                out.println("<?xml version=\"1.0\"?>");
                out.println("<resource id=\"1234\" name=\"test\" />");
            }
        });

        URL url = new URL("http://localhost:8000/1234.xml");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        assertEquals("<?xml version=\"1.0\"?>", in.readLine());
        assertEquals("<resource id=\"1234\" name=\"test\" />", in.readLine());
        assertEquals(HTTP_OK, connection.getResponseCode());
    }

}

Get it?

Maven.

<project>

  <dependencies>
    <dependency>
      <groupId>junit-rules</groupId>
      <artifactId>junit-rules</artifactId>
      <version>0.4</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <repositories>
    <repository>
      <id>alistairisrael.github.com</id>
      <url>http://alistairisrael.github.com/</url>
    </repository>
  </repositories>

</project>
Last edited by AlistairIsrael, Fri Oct 16 17:37:01 -0700 2009
Home | Edit | New
Versions: