SpringBoot单元测试
程序员文章站
2022-04-26 09:17:27
...
快捷方式,IDEA中在需要测试的方法上右键go to–>test,即可快速创建测试方法
测试Service
package com.imooc.service;
import com.imooc.domain.Girl;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import static org.junit.Assert.*;
/**
* Created by Administrator on 2018/12/1.
*/
public class GirlServiceTest {
@Autowired
private GirlService grilSevice;
@Test
public void findOne() throws Exception {
Girl girl =grilSevice.findOne(6);
Assert.assertEquals(new Integer(15),girl.getAge());
}
}
测试Controller
package com.imooc.controller;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
/**
* Created by Administrator on 2018/12/1.
*/
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class GirlControllerTest {
@Autowired
private MockMvc mvc;
@Test
public void girlList() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/girls"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().string("abc"));
}
}
上一篇: Spring boot 单元测试
下一篇: 单元测试实战