SpringBoot中的测试

SpringBoot中的测试

SpringBoot中的Service测试

在启动类父文件夹中创建HelloService

@Service
public class HelloService {
    public String sayHello(String name){
        return "hello"+name;
    }
}

在测试类中加入测试方法

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestApplicationTests {
    @Autowired
    HelloService helloService;
    @Test
    public void contextLoads() {
        String hello = helloService.sayHello("xiaoliu");
        //Assert.assertThat(hello, Matchers.is("hello xiaoliu"));
        //Assert.assertThat(hello,Matchers.is("hello xiaoliu") );
    }
}

SpringBoot中的Controller测试

在启动类父文件夹中创建HelloController

@RestController
public class HelloController {
    @GetMapping("/hello")
    public String hello(String name){
        return "hello"+name;
    }
    @PostMapping("/book")
    public Book addBook(@RequestBody Book book){
        return book;
    }
}

并且创建Book实体类

public class Book {
    private Integer id;
    private String name;
    private String author;

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", author='" + author + '\'' +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }
}

在测试类中加入测试方法

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestApplicationTests {
    @Autowired
    WebApplicationContext wac;
    MockMvc mockMvc;
    @Before
    public void before(){
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
        构造MockMvc
    }
    @Test
    public void test1() throws Exception {
        MvcResult mvcResult = mockMvc.perform(
                MockMvcRequestBuilders.get("/hello")
                        .contentType(MediaType.APPLICATION_FORM_URLENCODED)
                        .param("name", "xiaoliu"))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andDo(MockMvcResultHandlers.print())
                .andReturn();
        System.out.println(mvcResult.getResponse().getContentAsString());

    }

    @Test
    public void test2() throws Exception {
        Book book=new Book();
        book.setId(99);
        book.setName("三国演义");
        book.setAuthor("罗贯中");
        String s = new ObjectMapper().writeValueAsString(book);
        MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/book").contentType(MediaType.APPLICATION_JSON).content(s))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        System.out.println(mvcResult.getResponse().getContentAsString());
    }
}

SpringBoot中的json测试

在test目录下添加book.json

{"id":99,"name":"红楼梦","author":"曹雪芹"}

并且添加JsonTest.java

@RunWith(SpringRunner.class)
@org.springframework.boot.test.autoconfigure.json.JsonTest
public class JsonTest {
    @Autowired
    JacksonTester<Book> jacksonTester;
    @Test
    public void contextLoads() throws Exception {
        Book book=new Book();
        book.setId(99);
        book.setName("红楼梦");
        book.setAuthor("曹雪芹");
        Assertions.assertThat(jacksonTester.write(book))
                .isEqualToJson("book.json");
        Assertions.assertThat(jacksonTester.write(book))
                .hasJsonPathStringValue("@.name");
        Assertions.assertThat(jacksonTester.write(book))
                .extractingJsonPathStringValue("@.name")
                .isEqualTo("红楼梦");
    }

    @Test
    public void test2() throws IOException {
        String content="{\"id\":99,\"name\":\"红楼梦\",\"author\":\"曹雪芹\"}";
      Assertions.assertThat(jacksonTester.parseObject(content).getName()).isEqualTo("红楼梦");
    }
}

SpringBoot中的json测试testRestTemplate

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class TestApplicationTests2 {
    @Autowired
    TestRestTemplate testRestTemplate;
    @Test
    public void contextLoads() {
        String xiaoliu = testRestTemplate.getForObject("/hello?name={1}", String.class, "xiaoliu");
        System.out.println(xiaoliu);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值