Close

Spring - Integration testing, using SpringJUnit4ClassRunner

This example uses SpringJUnit4ClassRunner instead of SpringRunner.

Both runners are exactly the same

SpringJUnit4ClassRunner is older annotation, SpringRunner was introduced in Spring 4.3.0 as a shorter name.

Example

We are going to create a simple Spring application and write a test for it.

pom.xml

<project .....>
<modelVersion>4.0.0</modelVersion>
<groupId>com.logicbig.example</groupId>
<artifactId>spring-testing-getting-started</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.14.1</version>
<configuration>
<source>8</source>
<target>8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>

Order, a pojo

package com.logicbig.example;

public class Order {
  private String item;
  private int qty;

  public Order(String item, int qty) {
      this.item = item;
      this.qty = qty;
  }

  public String getItem() {
      return item;
  }

  public int getQty() {
      return qty;
  }
}

OrderService

package com.logicbig.example;

import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderService {

  public String placeOrders(List<Order> orders) {
      //just a dummy service
      return orders.size() + " orders placed";
  }
}

ShoppingCart

package com.logicbig.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@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;
  }
}

Configuration class

package com.logicbig.example;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.logicbig.example")
public class AppConfig {
}

The test class

package com.logicbig.example;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.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 test -Dtest=ShoppingCartTest

Output

D:\example-projects-versioned\spring-framework\3.2.9.RELEASE\spring-core-testing\spring-testing-getting-started>mvn test -Dtest=ShoppingCartTest
[INFO] Scanning for projects...
[INFO]
[INFO] --------< com.logicbig.example:spring-testing-getting-started >---------
[INFO] Building spring-testing-getting-started 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- resources:3.3.1:resources (default-resources) @ spring-testing-getting-started ---
[INFO] skip non existing resourceDirectory D:\example-projects-versioned\spring-framework\3.2.9.RELEASE\spring-core-testing\spring-testing-getting-started\src\main\resources
[INFO]
[INFO] --- compiler:3.14.1:compile (default-compile) @ spring-testing-getting-started ---
[INFO] Nothing to compile - all classes are up to date.
[INFO]
[INFO] --- resources:3.3.1:testResources (default-testResources) @ spring-testing-getting-started ---
[INFO] skip non existing resourceDirectory D:\example-projects-versioned\spring-framework\3.2.9.RELEASE\spring-core-testing\spring-testing-getting-started\src\test\resources
[INFO]
[INFO] --- compiler:3.14.1:testCompile (default-testCompile) @ spring-testing-getting-started ---
[INFO] Nothing to compile - all classes are up to date.
[INFO]
[INFO] --- surefire:3.2.5:test (default-test) @ spring-testing-getting-started ---
[INFO] Using auto detected provider org.apache.maven.surefire.junit4.JUnit4Provider
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.logicbig.example.ShoppingCartTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.508 s -- in com.logicbig.example.ShoppingCartTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.365 s
[INFO] Finished at: 2025-11-25T14:31:17+08:00
[INFO] ------------------------------------------------------------------------

Example Project

Dependencies and Technologies Used:

  • spring-context 3.2.9.RELEASE (Spring Context)
  • spring-test 3.2.9.RELEASE (Spring TestContext Framework)
  • junit 4.12 (JUnit is a unit testing framework for Java, created by Erich Gamma and Kent Beck)
  • JDK 8
  • Maven 3.9.11

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

    See Also