Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I've used JUnit for doing a lot of integration testing. Integration testing can, of course, mean many different things. For more system level integration tests, I prefer to let scripts drive my testing process from outside.</p> <p>Here's an approach that works well for me for applications that use http and databases and I want to verify the whole stack:</p> <ol> <li>Use <code>Hypersonic or H2</code> in in-memory mode as a replacement for the database (this works best for ORMs)</li> <li>Initialize the database in <code>@BeforeSuite</code> or equivalent (again: easiest with ORMs)</li> <li>Use Jetty to start an in-process web server.</li> <li><code>@Before</code> each test, clear the database and initialize with the necessary data</li> <li>Use <code>JWebUnit</code> to execute HTTP requests towards Jetty</li> </ol> <p>This gives you integration tests that can run without any setup of database or application server and that exercises the stack from http down. Since it has no dependencies on external resources, this test runs fine on the build server.</p> <p>Here some of the code I use:</p> <pre><code>@BeforeClass public static void startServer() throws Exception { System.setProperty("hibernate.hbm2ddl.auto", "create"); System.setProperty("hibernate.dialect", "..."); DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setJdbcUrl("jdbc:hsqldb:mem:mytest"); new org.mortbay.jetty.plus.naming.Resource( "jdbc/primaryDs", dataSource); Server server = new Server(0); WebAppContext webAppContext = new WebAppContext("src/main/webapp", "/"); server.addHandler(webAppContext); server.start(); webServerPort = server.getConnectors()[0].getLocalPort(); } // From JWebUnit private WebTestCase tester = new WebTestCase(); @Before public void createTestContext() { tester.getTestContext().setBaseUrl("http://localhost:" + webServerPort + "/"); dao.deleteAll(dao.find(Product.class)); dao.flushChanges(); } @Test public void createNewProduct() throws Exception { String productName = uniqueName("product"); int price = 54222; tester.beginAt("/products/new.html"); tester.setTextField("productName", productName); tester.setTextField("price", Integer.toString(price)); tester.submit("Create"); Collection&lt;Product&gt; products = dao.find(Product.class); assertEquals(1, products.size()); Product product = products.iterator().next(); assertEquals(productName, product.getProductName()); assertEquals(price, product.getPrice()); } </code></pre> <p>For those who'd like to know more, I've written an <a href="http://today.java.net/pub/a/today/2007/04/12/embedded-integration-testing-of-web-applications.html" rel="noreferrer">article about Embedded Integration Tests with Jetty and JWebUnit</a> on Java.net.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload