How do you automate integration testing? I use JUnit for some of these tests. This is one of the solutions or is totally wrong? What do you suggest?
Besides manual testing, JUnit is preferred equally for automation testing. It can also be used along with the Selenium WebDriver to automate tests for web applications. It provides a unique way to write structured, short, and better test cases.
Just like unit tests, integration tests are mostly written by developers and can be automated, but they test completely different features. Integration tests are about observing how code sections behave together and “making sure that they speak the same language,” Schneider said.
JUnit is an open source unit testing tool that helps to test units of code. It is mainly used for unit testing Java project, however, it can be used with Selenium Webdriver to automate testing of Web applications. So you can even perform automation testing of a web application with JUnit.
Unit testing APIs is an important part of API testing. Unit testing ensures that API components will function properly. In this article we will learn how to cover JUnit REST API testing with Spring Boot.
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.
Here's an approach that works well for me for applications that use http and databases and I want to verify the whole stack:
Hypersonic or H2
in in-memory mode as a replacement for the database (this works best for ORMs)@BeforeSuite
or equivalent (again: easiest with ORMs)@Before
each test, clear the database and initialize with the necessary dataJWebUnit
to execute HTTP requests towards JettyThis 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.
Here some of the code I use:
@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<Product> products = dao.find(Product.class); assertEquals(1, products.size()); Product product = products.iterator().next(); assertEquals(productName, product.getProductName()); assertEquals(price, product.getPrice()); }
For those who'd like to know more, I've written an article about Embedded Integration Tests with Jetty and JWebUnit on Java.net.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With