我们这边主要数据面和控制面相结合的地去测试中间件产品,确保产品在运维的时候,服务还能按期望的质量提供服务
@RunWith(SpringRunner.class) @SpringBootTest(classes = StartUpApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class HelloControllerTest {
/** * @LocalServerPort 提供了 @Value("${local.server.port}") 的代替 */ @LocalServerPort private int port;
private URL base;
@Autowired private TestRestTemplate restTemplate;
@Before public void setUp() throws Exception { String url = String.format("http://localhost:%d/", port); System.out.println(String.format("port is : [%d]", port)); this.base = new URL(url); }
/** * 向"/test"地址发送请求,并打印返回结果 * @throws Exception */ @Test public void test1() throws Exception {
ResponseEntity<String> response = this.restTemplate.getForEntity( this.base.toString() + "/test", String.class, ""); System.out.println(String.format("测试结果为:%s", response.getBody())); }