本文用于收集springboot的各种测试单元类的写法与配置。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package cn.hnjz;

import com.jayway.restassured.RestAssured;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import static com.jayway.restassured.RestAssured.given;
import static com.jayway.restassured.RestAssured.when;
import static org.hamcrest.CoreMatchers.is;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = App.class)
@WebAppConfiguration
@IntegrationTest({"server.port:0",
"spring.datasource.url:jdbc:h2:mem:mydemo;DB_CLOSE_ON_EXIT=FALSE"})
public class HelloControllerTest {
@Value("${local.server.port}")
int port;

@Autowired
private HelloController helloController;
@Before
public void setUp() throws Exception {
RestAssured.port = port;
}

@Test
public void testHello() throws Exception {
when().get("/").then()
.body(is("Hello World!"));
System.out.println( helloController.hello(""));
}

@Test
public void testCalc() throws Exception {
given().param("left", 100)
.param("right", 200)
.get("/calc")
.then()
.body("left", is(100))
.body("right", is(200))
.body("answer", is(300));
}
}