错误整理二 | assertThat, equalTo, @RunWith, @SpringBootTest标红

《深入理解Spring Cloud与微服务构建》整理:
关于 4.2.3 Spring Boot 的测试一章,
@RunWith, @SpringBootTest 报红
按 IDEA 提示操作即可
在这里插入图片描述
错误整理二 | assertThat, equalTo, @RunWith, @SpringBootTest标红_第1张图片

assertThat, equalTo 方法报红,添加对应包即可:

import static org.junit.Assert.*;
import static org.hamcrest.Matchers.equalTo;

关于本章的代码:

package com.example.demo;


import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;

import static org.junit.Assert.*;
import static org.hamcrest.Matchers.equalTo;

import java.net.MalformedURLException;
import java.net.URL;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class HelloControllerIT {
     
    @LocalServerPort
    private int port;
    private URL base;

    @Autowired
    private TestRestTemplate template;

    public void setUp() throws MalformedURLException {
     
        this.base = new URL("http://localhost:" + port + "/hello");
    }

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

你可能感兴趣的:(Spring,Cloud,JAVA)