Spring|Spring MVC Test with Mockito
MockMvc VetController
@Controller
public class VetController {private final ClinicService clinicService;
@Autowired
public VetController(ClinicService clinicService) {
this.clinicService = clinicService;
}@RequestMapping(value = https://www.it610.com/article/{"/vets.html"})
public String showVetList(Map model) {
// Here we are returning an object of type 'Vets' rather than a collection of Vet objects
// so it is simpler for Object-Xml mapping
Vets vets = new Vets();
vets.getVetList().addAll(this.clinicService.findVets());
model.put("vets", vets);
return "vets/vetList";
}
}
VetControllerTest
@ExtendWith(MockitoExtension.class)
class VetControllerTest {@Mock
ClinicService clinicService;
@Mock
Map model;
@InjectMocks
VetController controller;
List vetsList = new ArrayList<>();
MockMvc mockMvc;
@BeforeEach
void setUp() {
vetsList.add(new Vet());
given(clinicService.findVets()).willReturn(vetsList);
mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
}@Test
void testControllerShowVetList() throws Exception {
mockMvc.perform(get("/vets.html"))
.andExpect(status().isOk())
.andExpect(model().attributeExists("vets"))
.andExpect(view().name("vets/vetList"));
}
}
推荐阅读
- springboot配置druid数据源及druid的日志监控
- JAVA|SpringBoot配置druid连接池
- Springboot配置Druid连接池
- druid|druid mysql_SpringBoot配置druid连接池连接mysql数据库
- 运维|springboot 配置 druid 监控
- SSM|SpringMVC拦截器 & SpringMVC异常处理
- java中自定义拦截器_JavaEE开发之SpringMVC中的自定义拦截器及异常处理
- spring|spring boot security自定义配置授权服务器实现Oauth2授权(支持密码和授权码两种模式)
- Spring|SpringSecurity + JWT自定义授权
- springboot 集成 spring security 自定义登录