集成测试

集成测试

As well as mocking the HTTP request cycle we can also use Spring Boot to write a very simple full-stack integration test. For example, instead of (or as well as) the mock test above we could do this:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@IntegrationTest({ "server.port=0" })
public class HelloControllerIT {
  @Value("${local.server.port}")
  private int port;

  private URL base;
  private RestTemplate template;

  @Before
  public void setUp() throws Exception {
    this.base = new URL("http://localhost:" + port + "/");
    template = new TestRestTemplate();
  }

  @Test
  public void getHello() throws Exception {
    ResponseEntity<String> response = template.getForEntity(
      base.toString(),
      String.class
    );
    assertThat(response.getBody(), equalTo("Greetings from Spring Boot!"));
  }
}

The embedded server is started up on a random port by virtue of the@IntegrationTest("${server.port=0}") and the actual port is discovered at runtime with the @Value("${local.server.port}").

Actuator

If you are building a web site for your business, you probably need to add some management services. Spring Boot provides several out of the box with its actuator module, such as health, audits, beans, and more.

Add this to your build file’s list of dependencies:

compile("org.springframework.boot:spring-boot-starter-actuator")

If you are using Maven, add this to your list of dependencies:

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

Then restart the app:

./gradlew build && java -jar build/libs/gs-spring-boot-0.1.0.jar

If you are using Maven, execute:

mvn package && java -jar target/gs-spring-boot-0.1.0.jar

You will see a new set of RESTful end points added to the application. These are management services provided by Spring Boot.

2014-06-03 13:23:28.119... : Mapped "{[/error],methods=[],params=[],headers=[],consumes...
2014-06-03 13:23:28.119... : Mapped "{[/error],methods=[],params=[],headers=[],consumes...
2014-06-03 13:23:28.136... : Mapped URL path [/**] onto handler of type [class org.spri...
2014-06-03 13:23:28.136... : Mapped URL path [/webjars/**] onto handler of type [class ...
2014-06-03 13:23:28.440... : Mapped "{[/info],methods=[GET],params=[],headers=[],consum...
2014-06-03 13:23:28.441... : Mapped "{[/autoconfig],methods=[GET],params=[],headers=[],...
2014-06-03 13:23:28.441... : Mapped "{[/mappings],methods=[GET],params=[],headers=[],co...
2014-06-03 13:23:28.442... : Mapped "{[/trace],methods=[GET],params=[],headers=[],consu...
2014-06-03 13:23:28.442... : Mapped "{[/env/{name:.*}],methods=[GET],params=[],headers=...
2014-06-03 13:23:28.442... : Mapped "{[/env],methods=[GET],params=[],headers=[],consume...
2014-06-03 13:23:28.443... : Mapped "{[/configprops],methods=[GET],params=[],headers=[]...
2014-06-03 13:23:28.443... : Mapped "{[/metrics/{name:.*}],methods=[GET],params=[],head...
2014-06-03 13:23:28.443... : Mapped "{[/metrics],methods=[GET],params=[],headers=[],con...
2014-06-03 13:23:28.444... : Mapped "{[/health],methods=[GET],params=[],headers=[],cons...
2014-06-03 13:23:28.444... : Mapped "{[/dump],methods=[GET],params=[],headers=[],consum...
2014-06-03 13:23:28.445... : Mapped "{[/beans],methods=[GET],params=[],headers=[],consu...

They include: errors, environment, health, beans, info, metrics, trace, configprops, and dump.

** There is also a /shutdown endpoint, but it’s only visible by default via JMX. To enable it as an HTTP endpoint, add endpoints.shutdown.enabled=true to yourapplication.properties file.

It’s easy to check the health of the app.

$ curl localhost:8080/health
{"status":"UP"}

You can try to invoke shutdown through curl.

$ curl -X POST localhost:8080/shutdown
{"timestamp":1401820343710,"error":"Method Not Allowed","status":405,"message":"Request method 'POST' not supported"}

Because we didn’t enable it, the request is blocked by the virtue of not existing.

For more details about each of these REST points and how you can tune their settings with anapplication.properties file, you can read detailed docs about the endpoints.

上一页