Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.weib.spittr.web; import com.weib.spittr.repostory.data.Spittle; import com.weib.spittr.repostory.SpittleRepository; import java.util.ArrayList; import java.util.Date; import java.util.List; import static org.hamcrest.CoreMatchers.hasItems; import org.junit.Test; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import org.springframework.test.web.servlet.MockMvc; import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; import org.springframework.web.servlet.view.InternalResourceView; /** * * @author zhangjingwei */ public class SpittleControllerTest { private List<Spittle> createSpittleList(int count) { List<Spittle> spittleList = new ArrayList(); for (int i = 0; i < count; i++) { spittleList.add(new Spittle("test---" + (i + 1), new Date())); } return spittleList; } @Test public void getSpittleTest() throws Exception { //?? List<Spittle> spittleList = createSpittleList(20); //?20?? SpittleRepository mockRepository = mock(SpittleRepository.class); //??Mock when(mockRepository.findSpittles(Long.MAX_VALUE, 20)) //?? .thenReturn(spittleList); //controller SpittleController controller = new SpittleController(mockRepository); //controller MockMvc mockMvc = standaloneSetup(controller) .setSingleView(new InternalResourceView("/WEB-INF/views/spittles.jsp") //????setSingleView ).build(); //?GET/spittles mockMvc.perform(get("/spittles")).andExpect(view().name("spittles")) .andExpect(model().attributeExists("spittleList11")) .andExpect(model().attribute("spittleList11", hasItems(spittleList.toArray()))); } @Test public void postSpittleTest() throws Exception { //?? List<Spittle> spittleList = createSpittleList(20); //?20?? SpittleRepository mockRepository = mock(SpittleRepository.class); //??Mock when(mockRepository.findSpittles(Long.MAX_VALUE, 20)) //?? .thenReturn(spittleList); //controller SpittleController controller = new SpittleController(mockRepository); //controller MockMvc mockMvc = standaloneSetup(controller) .setSingleView(new InternalResourceView("/WEB-INF/views/spittle_list.jsp") //????setSingleView ).build(); //?GET/spittles mockMvc.perform(get("/spittle_list")).andExpect(view().name("spittle_list")) .andExpect(model().attributeExists("spittleList")) .andExpect(model().attribute("spittleList", hasItems(spittleList.toArray()))); } @Test public void pagedSpittleTest() throws Exception { List<Spittle> spittles = createSpittleList(50); SpittleRepository mockRepository = mock(SpittleRepository.class); when(mockRepository.findSpittles(100, 50)).thenReturn(spittles); SpittleController controller = new SpittleController(mockRepository); MockMvc mockMvc = standaloneSetup(controller) .setSingleView(new InternalResourceView("/WEB-INF/views/spittle_page.jsp")).build(); mockMvc.perform(get("/spittle_page?max=100&count=50")).andExpect(view().name("spittle_page")) .andExpect(model().attributeExists("spittleList")) .andExpect(model().attribute("spittleList", hasItems(spittles.toArray()))); } @Test public void showSpittleTest() throws Exception { Spittle spittle = new Spittle("hello!!", new Date()); SpittleRepository repository = mock(SpittleRepository.class); when(repository.findOne(12345)).thenReturn(spittle); SpittleController controller = new SpittleController(repository); MockMvc mockMvc = standaloneSetup(controller).build(); mockMvc.perform(get("/spittle/12345")).andExpect(view().name("spittle_show")) .andExpect(model().attributeExists("spittle")).andExpect(model().attribute("spittle", spittle)); } }