我是靠谱客的博主 靓丽柚子,最近开发中收集的这篇文章主要介绍spring mvc @controller unit test,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1. spring mvc controller implementation

@Controller
public class Alarm {
@Resource(name = "db.service", shareable = true)
private IDatabaseService dbService;
@RequestMapping(value = "/query_alarmlogs", produces = "application/json;charset=UTF-8")
public @ResponseBody
AlarmLogList queryAlarmLogs(@RequestParam("queryType") String queryType, @RequestParam("lastId") String lastId) {
AlarmQueryType alarmType = AlarmQueryType.fromValue(queryType);
AlarmLogList list = findAlarmLogs(alarmType.getAlarmDate(), Integer.valueOf(lastId));
return list;
}
private AlarmLogList findAlarmLogs(Date date, int lastId) {
List<AlarmLogs> alarmList = dbService.findAlarmLogs(date, lastId);
AlarmLogList alarms = new AlarmLogList(alarmList);
return alarms;
}
private final class AlarmLogList extends ArrayList<AlarmLogs> {
private static final long serialVersionUID = 1L;
public AlarmLogList(List<AlarmLogs> list) {
super(list);
}
}
public void setDbService(IDatabaseService dbService) {
this.dbService = dbService;
}
}

 

2. test it.

package com.eifesun.mornitor.web;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Date;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import com.eifesun.monitor.DeviceType;
import com.eifesun.monitor.service.IDatabaseService;
import com.eifesun.monitor.service.MongoDBServiceImpl;
import com.eifesun.monitor.web.Alarm;
import com.eifesun.monitor.web.command.AlarmQueryType;
import com.eifesun.monitor.web.model.AlarmLogs;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/spring/mvc-core-config.xml", "/spring/business-config.xml"})
@WebAppConfiguration
public class AlarmControllerTest {
private MockMvc mockMvc;
@Autowired
public RequestMappingHandlerAdapter handlerAdapter;
@Mock
private IDatabaseService mockDbService;
@Autowired
private Alarm alarm;
@Before
public void init() {
mockDbService = Mockito.mock(MongoDBServiceImpl.class);
alarm.setDbService(mockDbService);
this.mockMvc = MockMvcBuilders.standaloneSetup(alarm).build();
}
@Test
public void testQueryAlarmLogs() throws Exception {
AlarmLogs log1 = new AlarmLogs(DeviceType.INVERTER, "inverter1", "panel 1 over volage");
AlarmLogs log2 = new AlarmLogs(DeviceType.INVERTER, "inverter1", "panel 2 over volage");
int lastId = 0;
String queryType = "alarm_auto";
Date date = AlarmQueryType.fromValue(queryType).getAlarmDate();
Mockito.when(mockDbService.findAlarmLogs(date, lastId)).thenReturn(Arrays.asList(log1, log2));
//@formatter:off
mockMvc.perform(get("/query_alarmlogs?queryType=alarm_auto&lastId=0"))
.andExpect(status().isOk())
.andExpect(content().contentType(APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$", hasSize(2)))
.andExpect(jsonPath("$[0].clientId", equalTo("inverter1")))
.andExpect(jsonPath("$[0].deviceType", equalTo("INVERTER")))
.andExpect(jsonPath("$[0].description", equalTo("panel 1 over volage")))
.andExpect(jsonPath("$[1].clientId", equalTo("inverter1")))
.andExpect(jsonPath("$[1].deviceType", equalTo("INVERTER")))
.andExpect(jsonPath("$[1].description", equalTo("panel 2 over volage")));
//@formatter:on
verify(mockDbService, times(1)).findAlarmLogs(date, lastId);
verifyNoMoreInteractions(mockDbService);
}
public static final MediaType APPLICATION_JSON_UTF8 = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(),
Charset.forName("utf8"));
}

 

 

 

最后

以上就是靓丽柚子为你收集整理的spring mvc @controller unit test的全部内容,希望文章能够帮你解决spring mvc @controller unit test所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(55)

评论列表共有 0 条评论

立即
投稿
返回
顶部