Skip to content

Instantly share code, notes, and snippets.

@yanhua365
Created December 4, 2013 03:42
Show Gist options
  • Select an option

  • Save yanhua365/7781983 to your computer and use it in GitHub Desktop.

Select an option

Save yanhua365/7781983 to your computer and use it in GitHub Desktop.
Spring MVC测试返回XML结果的控制器
package com.kingsoft.wpsess.update.controller;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import com.kingsoft.wpsess.update.service.UpdateService;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.when;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
/**
* 测试更新服务的控制器
*/
@RunWith(MockitoJUnitRunner.class)
public class UpdateServerControllerTest {
private MockMvc mockMvc;
@Mock
private UpdateService updateServiceMock;
private String bodyXml;
private String v;
private String responseXml;
@Before
public void setUp() {
mockMvc = MockMvcBuilders.standaloneSetup(new UpdateServerController(updateServiceMock)).build();
v = "%2FdKY16wvvbiqanDn4jvPWrL3ue2h8koxEILg%2B%2B6d%2FcUp7sfz6gHTv%2F2dT5WFMou3bNjaDZpcjl1wviBMWPHuYA==";
bodyXml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" +
"<update protocol=\"1.0\">\n" +
" <office productid=\"PG01-WPS-2052-156-X-ProEx\" version=\"9.1.0.4167\" />\n" +
"</update>";
responseXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<update protocol=\"1.0\">\n" +
" <status version=\"6.6.0.954\" code=\"getinfo\" info=\"first to getinfo:allinfo\"/>\n" +
"</update>";
}
@Test
public void thatUpdateRequestReturnErrorXmlResponse() throws Exception{
String errorXmlResponse = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<update protocol=\"1.0\">\n" +
" <status version=\"6.6.0.954\" code=\"error\" info=\"XmlCrc verify error!\"/>\n" +
"</update>";
when(updateServiceMock.doRequest(v, bodyXml)).thenThrow(new BadUpdateRequestException.CrcXmlError("[Bad parameter] - the parameter 'v' must not be null or empty."));
mockMvc.perform(post("/updateserver/update?v={v}",
v)
.contentType(MediaType.APPLICATION_OCTET_STREAM).accept(MediaType.ALL)
.header("Content-Length","160")
.header("User-Agent","Update.WPS")
.header("Cache-Control","no-cache")
.content(bodyXml)).andDo(print())
.andExpect(status().isOk())
.andExpect(content().xml(errorXmlResponse));
}
@Test
public void thatFirstUpdateRequestReturnCorrectXmlResponse() throws Exception{
when(updateServiceMock.doRequest(v, bodyXml)).thenReturn(responseXml);
mockMvc.perform(post("/updateserver/update?v={v}",
v)
.contentType(MediaType.APPLICATION_OCTET_STREAM).accept(MediaType.ALL)
.header("Content-Length","160")
.header("User-Agent","Update.WPS")
.header("Cache-Control","no-cache")
.content(bodyXml)).andDo(print())
.andExpect(status().isOk())
.andExpect(content().string(responseXml));
verify(updateServiceMock, times(1)).doRequest(v, bodyXml);
verifyNoMoreInteractions(updateServiceMock);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment