import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.context.WebApplicationContext;
····
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
@WebAppConfiguration
@ActiveProfiles("dev")
public class ControllerTest {
private static final String URL = "/demo";
@Autowired
private WebApplicationContext context;
private MockMvc mockMvc;
@Before
public void setUp() throws Exception {
mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
}
@Test
public void importAll() throws Exception {
//传文件夹路径
String content = mockMvc
.perform(MockMvcRequestBuilders.post(URL + "/importAll").param("file","D://测试文件夹").contentType(MediaType.APPLICATION_JSON_UTF8))
.andDo(MockMvcResultHandlers.print())
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.success").value(true))
.andReturn().getResponse().getContentAsString();
Assert.assertNotNull(content);
}
//上传文件
@Test(expected = FileNotFoundException.class)
public void readExcel() throws Exception {
String uploadFilePath = "D://测试.xlsx";
File uploadFile = new File(uploadFilePath);
String fileName = uploadFile.getName();
MockMultipartFile file = new MockMultipartFile("uploadFile", fileName, MediaType.TEXT_PLAIN_VALUE, new FileInputStream(uploadFile));
String content = mockMvc.perform(MockMvcRequestBuilders.fileUpload(URL + "/readExcel").file(file))
.andDo(print())
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.failed").value(false))
.andReturn().getResponse().getContentAsString();
Assert.assertNotNull(content);
}
}
@Test
public void find() throws Exception {
ArrayList<Long> longs = new ArrayList<>();
longs.add(1L);
longs.add(2L);
HashMap<Object, Object> map = Maps.newHashMap();
map.put("idList",longs);
String json = JsonUtils.toJsonString(map);
String content = mockMvc
.perform(MockMvcRequestBuilders.post(URL + "/find").content(json).contentType(MediaType.APPLICATION_JSON_UTF8))
.andDo(MockMvcResultHandlers.print())
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.success").value(true))
.andReturn().getResponse().getContentAsString();
Assert.assertNotNull(content);
}
//下载文件
@Test(expected = Exception.class)
public void download() throws Exception {
Long projectId = 1L;
String excelSheetName = "Sheet1";
String filePath = "D://测试.xlsx"; //下载文件路径
mockMvc.perform(MockMvcRequestBuilders.get(URL+"/download/"+projectId))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(result -> {
result.getResponse().setCharacterEncoding("UTF-8");
MockHttpServletResponse contentResponse = result.getResponse();
InputStream contentInStream = new ByteArrayInputStream(
contentResponse.getContentAsByteArray());
XSSFWorkbook resultExcel = new XSSFWorkbook(contentInStream);
//Assert.assertEquals("multipart/form-data", contentResponse.getContentType());
XSSFSheet sheet = resultExcel.getSheet(excelSheetName);
Assert.assertNotNull(sheet);
File file = new File(filePath);
OutputStream out = new FileOutputStream(file);
resultExcel.write(out);
resultExcel.close();
Assert.assertTrue(file.exists());
});
}
@Test
public void userAreaList() throws Exception{
Cookie cookie = new Cookie("www.baidu.com","xxxxxxxxxxxxxxxxxxxxx");
cookie.setPath("/");
cookie.setMaxAge(7);
String content = mockMvc
.perform(MockMvcRequestBuilders.get(URL + "/areaList").cookie(cookie))
.andDo(MockMvcResultHandlers.print())
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.failed").value(true))
.andReturn().getResponse().getContentAsString();
Assert.assertNotNull(content);
}
}
|