Close

Spring - Integration Testing with JUnit

[Last Updated: Jul 8, 2017]

As spring beans are nothing but POJOs, we can always write unit test for individual beans that simply instantiated using the new operator, without Spring or any other container. But that's not good enough if we also would like to test whether beans are wired together properly or not and whether everything works as expected when tested in Spring container.

The Spring TestContext Framework supports full integration with JUnit 4 through a custom runner.

By annotating test classes with @RunWith(SpringJUnit4ClassRunner.class) or the shorter @RunWith(SpringRunner.class) variant, we can write standard JUnit 4 unit and integration tests. That means we can have full support of spring context loading and dependency injection of the beans in our tests. Let's see how to do that with an example

Example

Creating a simple Spring application

@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class ShoppingCart {
  @Autowired
  private OrderService orderService;
  private List<Order> orders = new ArrayList<>();

  public void addItem(String name, int qty) {
      orders.add(new Order(name, qty));
  }

  public String checkout() {
      String msg = orderService.placeOrders(orders);
      orders.clear();
      return msg;
  }
}
@Service
public class OrderService {

  public String placeOrders(List<Order> orders) {
      //just a dummy service
      return orders.size() + " orders placed";
  }
}
@Configuration
@ComponentScan("com.logicbig.example")
public class AppConfig {
}

Writing JUnit test

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = AppConfig.class)
public class ShoppingCartTest {

  @Autowired
  private ShoppingCart shoppingCart;

  @Test
  public void testCheckout() {
      shoppingCart.addItem("Item1", 3);
      shoppingCart.addItem("item2", 5);
      String result = shoppingCart.checkout();
      Assert.assertEquals("2 orders placed", result);
  }
}
mvn -q test

Output

d:\example-projects\spring-core-testing\spring-testing-getting-started>mvn -q test

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.logicbig.example.ShoppingCartTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.399 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

What is @ContextConfiguration?

This class-level annotation is used to specify what application configuration to load for Spring context. In above example, we specified 'classes' element with the value of our JavaConfig class. It has other useful elements as well, which we will be exploring in coming tutorials.

Example Project

Dependencies and Technologies Used:

  • spring-context 4.3.9.RELEASE: Spring Context.
  • spring-test 4.3.9.RELEASE: Spring TestContext Framework.
  • junit 4.12: JUnit is a unit testing framework for Java, created by Erich Gamma and Kent Beck.
  • JDK 1.8
  • Maven 3.3.9

Spring Integration Test Example Select All Download
  • spring-testing-getting-started
    • src
      • main
        • java
          • com
            • logicbig
              • example
      • test
        • java
          • com
            • logicbig
              • example
                • ShoppingCartTest.java

    See Also