Can we use JUNIT for Automated Integration Testing?

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?

Asked by: Max360 | Posted: 23-01-2022

Answer 1

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:

  1. Use Hypersonic or H2 in in-memory mode as a replacement for the database (this works best for ORMs)
  2. Initialize the database in @BeforeSuite or equivalent (again: easiest with ORMs)
  3. Use Jetty to start an in-process web server.
  4. @Before each test, clear the database and initialize with the necessary data
  5. Use JWebUnit to execute HTTP requests towards Jetty

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.

Here some of the code I use:

public static void startServer() throws Exception {
    System.setProperty("", "create");
    System.setProperty("hibernate.dialect", "...");
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
             "jdbc/primaryDs", dataSource);

    Server server = new Server(0);
    WebAppContext webAppContext = new WebAppContext("src/main/webapp", "/");
    webServerPort = server.getConnectors()[0].getLocalPort();

// From JWebUnit
private WebTestCase tester = new WebTestCase();

public void createTestContext() {
    tester.getTestContext().setBaseUrl("http://localhost:" + webServerPort + "/");

public void createNewProduct() throws Exception {
    String productName = uniqueName("product");
    int price = 54222;

    tester.setTextField("productName", productName);
    tester.setTextField("price", Integer.toString(price));

    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

Answered by: Daisy317 | Posted: 24-02-2022

Answer 2

JUnit works. There are no limitations that restrict it to being unit tests only. We use JUnit, Maven and CruiseControl to do CI.

There may be tools that are specific for integration testing, but I would think their usefulness is dependent on what type of system components you are integrating. JUnit will work fine for non UI type testing.

Answered by: Audrey636 | Posted: 24-02-2022

Answer 3

When using Maven to build a project, I've had a little more luck with TestNG because it has @BeforeSuite and @AfterSuite operations. Which are useful because Maven will not execute the 'post-integration-test` if any of the integration tests fail. Not a problem with Ant, so I just use jUnit out of preference with it.

In either case, segmenting out the tests as both TestNG and jUnit do is helpful with integration tests too.

Answered by: John838 | Posted: 24-02-2022

Answer 4

In our work here, our integration testing solution has three major parts:

  1. CruiseControl is the foundation of our continuous integration methodology.
  2. Our CruiseControl configuration kicks off a quick-test build within 3 minutes of anyone's checkin to Subversion. The tests that happen here are "does everything still compile?" and "do the unit tests all still pass?". JUnit is obviously the major facilitator in answering the second questions.
  3. Every hour, it kicks off a larger build that constructs the online help and installers that we use on our various deployment platforms. This step verifies the bigger questions of "do we still have a deployable product for each of our target platforms?"

The end result is that most people here never worry about integration testing: it just happens. Unit testing, on the other hand, is everyone's priority. JUnit makes it easy to construct tests, though good tests will always require thought and development time.

Answered by: Catherine542 | Posted: 24-02-2022

Answer 5

Yes, you may use junit for integration tests, but it depends on the type of integration test you need.

Testing a servlet:

  • setup the servlet context and config
  • do the tests using mock servlet requests (Spring has support for this, but you may also use EasyMock or your own mocks)

Testing a spring application:

  • use AbstractDependencyInjectionSpringContextTests to setup the context
  • test the wired beans
  • there are also subclasses of AbstractDependencyInjectionSpringContextTests supporting transaction handling when testing with a database.

But pure Junit has its limit. Testing user interfaces is a typical case. You may use selenium for web applications, soapui for webservices or other appropriate tools.

But whatever you use, it should be possible to integrate it in your continious build (cruise control, team city or whatever).

Answered by: Roland251 | Posted: 24-02-2022

Answer 6

Definitely! We use a combination of JUnit, ANT tasks to run them, and Hudson for continues integration tests. Works like a charm.

Answered by: Wilson559 | Posted: 24-02-2022

Answer 7

The suggestion depends on your application and your objective.

I've written integration tests in JUnit, but I've also seen people use HtmlUnit (JUnit extension), Selenium, Watir, Fit/Fitness, and even commercial tools like WinRunner and Silk.

So tell us a bit more about your domain and the objectives of your tests and you can probably get a better answer.

Answered by: Audrey453 | Posted: 24-02-2022

Answer 8

There is a very good extension for JUnit called Jitr.

Jitr is a JUnit Integration Test Runner and it allows your web application integration tests to easily run against a lightweight web container in the same JVM as your tests.

See their site for details:

Answered by: Emma668 | Posted: 24-02-2022

Answer 9

Update for 2012: Whilst JUnit can be used (and benefits from CI support) JWebUnit and Selenium appear to be eating up the mindshare for Integration Testing.

Answered by: Brianna531 | Posted: 24-02-2022

Answer 10

I think automation and integration tests do not play well together. The very basic problem is environment setup before every test. The more integration-type test bigger setup is needed.

My thoughts on test automation on integration layer:

Answered by: Stella619 | Posted: 24-02-2022

Similar questions

java - Automated integration tests as part of Azure DevOps CD/CI

Basic Java CRUD application. Nothing crazy. We would want to test the data layer accordingly (so we have dependencies on a database). The project is maven based, so the pipeline runs a build/runs unit tests then drops the artifact accordingly. My question is this: what's the best practice to handle this with Azure Pipelines? Should we run an in-memory database that we can create and destroy once tests are c...

java - Integration of apache common vfs and mercurial

Can Apache common vfs api retrieve/update/add files from/to the Mercurial version control system? Common vfs does have an interface api for accessing vcs. Is there a provider for Mercurial? hbagchi

java - Unit tests vs integration tests with Spring

I'm working on a Spring MVC project, and I have unit tests for all of the various components in the source tree. For example, if I have a controller HomeController, which needs to have a LoginService injected into it, then in my unit test HomeControllerTest I simply instantiate the object as normal (outside of Spring) and inject the property: protected void set...

java - Spring Integration 1.0 RC2: Streaming file content?

I've been trying to find information on this, but due to the immaturity of the Spring Integration framework I haven't had much luck. Here is my desired work flow: New files are placed in an 'Incoming' directory Files are picked up using a file:inbound-channel-adapter The file content is streamed, N lines at a time, to a 'Stage 1' channel, which parses the line into a...

java - Integration testing an external library?

I am using an external library in a java project but I am not sure how can I integration test it with my code. For example: Let us say I am using a string encryption library to encrypt passwords. Please enlighten. Thanks

c# - Consuming a .Net library via COM or direct integration in Java

I have to admit the last time I programmed in Java was a data structures class in high school. So please be kind to this Java noob. I have spent a good deal of time putting together a C# COM library at work. This library relies heavily on some new .Net technologies (WPF being the big one) so translating it into another language is not really an option. I have tested consuming this library from C++ through the CO...

java - Is it currently possible to build Eclipse Plugins by Maven AND have nice IDE Integration?

I'm currently evaluating maven to improve our build process. The building and creating of normal jar files works so far, although I'm not entirely happy with the Maven IDE. I'm now at that point, where all libs I need for our project are built, and I'm moving on to the Eclipse RCP projects. And now I'm not sure how to go on. There are some plugins I need to build first, before moving on to the actual RCP pa...

java - Putting Spring integration tests in different classes and packages

I am using AbstractTransactionalSpringContextTests to run spring integrations tests. The spring context is loaded just once and then all the tests are run. How do I do the same if I want my tests to be in many classes and packages. Of course, the spring context should be loaded just once for all my tests (in all classes and packages), and not once per class or package.

Build and Integration Environment for Java/Java EE

is there such a thing in a standard manner? including Java Source Code - Test Code - Ant or Maven JUnit Continuous Integration (possibly Cruise Control) ClearCase Versioning Tool Deploy to Application Server in the end I 'd like to have an automatic Build and Integration Environment.

integration - Calling a java program from another

How do i call a Java command from a stand alone java program. I understand that Runtime.getRuntime().exec("cmd c/ javac &lt;>.java"); would work. However, this would be platform specific. Any other APIs available that could make it work in j2sdk1.4 ?

java - Spring Security integration into active directory

I want to authenticate my web service in Spring with an Active Directory lookup at both the producer and the consumer - under the Principal that that each are executing under (ie Service Accounts). I'm assuming I have to use JaasPlainTextPasswordValidationCallbackHandler and JaasCertificateValidationCallbackHandler and set up my j...

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)