JUnit

From Resin 3.0

Revision as of 03:52, 28 January 2008 by Ferg (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Running Resin inside jUnit

[Verified with Resin 3.1.5 and jUnit-4.4]

The new Resin embedding features of Resin 3.1.5 simplify and speed-up unit testing. Since the embedded Resin can be tested without using an external HTTP/TCP connection, the setup and overhead is low.

The test will point to an expanded web-app directory, e.g. in the build area. It's also possible to expand from a .war file to a temp directory.

Suppose there's a single Quercus/PHP page, test.php we want to test, like:

<?php 
$a = $_GET["a"];
$b = $_GET["b"];

echo "$a + $b = " . ($a + $b); ?>

The jUnit test class might look like:

package qa;

import org.junit.*;
import static org.junit.Assert.*;
import com.caucho.resin.*;

public class MyTest {
  private static ResinEmbed _resin;

  @BeforeClass
  public static void setup()
  {
    _resin = new ResinEmbed();
    WebAppEmbed webApp = new WebAppEmbed("/", "file:/tmp/caucho/qa/test");
    _resin.addWebApp(webApp);
    _resin.start();
  }

  @Test 
  public void test1plus1()
    throws java.io.IOException
  {
    assertEquals(_resin.request("GET /test.php?a=1&b=1"), "1 + 1 = 2");
  }

  @Test 
  public void test1plus2()
    throws java.io.IOException
  {
    assertEquals(_resin.request("GET /test.php?a=1&b=2"), "1 + 2 = 3");
  }

  @AfterClass
  public static void shutdown()
  {
    if (_resin != null)
      _resin.destroy();
  }
}
Personal tools