Created
April 26, 2017 02:40
-
-
Save kuoshenghsu/2634537ddba7f97070a4c3a6eb0033bc to your computer and use it in GitHub Desktop.
[mockito] 簡易範例
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RunWith(SpringJUnit4ClassRunner.class) | |
@ContextConfiguration(locations = {"classpath:spring-mvc.xml", "classpath:spring-service.xml", | |
"classpath:spring-mapper-test.xml"}) | |
@WebAppConfiguration | |
public class ControllerTest { | |
@Autowired | |
private WebApplicationContext wac; | |
@InjectMocks | |
@Autowired | |
Controller controller; | |
@Mock | |
Facade facade; | |
private Entity entity; | |
private List<Entity> entities; | |
private String url; | |
private MockMvc mockMvc; | |
private static final String BASE_URL = "/test" | |
@Before | |
public void setUp() throws Exception { | |
MockitoAnnotations.initMocks(this); | |
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); | |
prepareEntity(); | |
prepareEntities(); | |
} | |
@Test | |
public void testReadList() throws Exception { | |
prepareTestReadList(); | |
mockMvc.perform(MockMvcRequestBuilders | |
.get(url) | |
.accept(MediaType.APPLICATION_JSON)) | |
.andDo(this::checkListHaveData); | |
} | |
@Test | |
public void testReadListAnotherMethod() throws Exception { | |
prepareTestReadList(); | |
mockMvc.perform(MockMvcRequestBuilders | |
.get(url) | |
.accept(MediaType.APPLICATION_JSON)) | |
.andDo(mvcResult -> Assert.assertEquals(HttpStatus.OK.value(), mvcResult.getResponse().getStatus())); | |
} | |
@Test | |
public void testUpdateListAndGenXml() throws Exception { | |
prepareTestUpdateListAndGenXml(); | |
String request = JsonUtils.getJsonTextByObject(entities); | |
mockMvc.perform(MockMvcRequestBuilders.put(url) | |
.content(request) | |
.contentType(MediaType.APPLICATION_JSON) | |
.accept(MediaType.APPLICATION_JSON)) | |
.andDo(mvcResult -> Assert.assertEquals(HttpStatus.OK.value(), mvcResult.getResponse().getStatus())); | |
} | |
private void prepareTestUpdateListAndGenXml() { | |
url = BASE_URL + CoreConf.UPDATE_PATH; | |
mockFacadeUpdateListAndGenXML(); | |
} | |
private void mockFacadeUpdateListAndGenXML() { | |
Mockito.doNothing().when(facade).UpdateListAndGenXML(Mockito.any()); | |
} | |
private void prepareTestReadList() { | |
url = BASE_URL + CoreConf.READ_PATH; | |
mockFacadeReadList(); | |
} | |
private void mockFacadeReadList() { | |
Mockito.doReturn(entities).when(facade).findCenterListIsActive(); | |
} | |
private void checkListHaveData(MvcResult mvcResult) throws UnsupportedEncodingException { | |
MockHttpServletResponse response = mvcResult.getResponse(); | |
MockMvcUtils.checkResponseStatus(response); | |
Assert.assertNotNull(JsonUtils.getObjectsByJsonText(response.getContentAsString(), Entity.class)); | |
} | |
private void prepareEntity() { | |
entity = Entity.Builder.creat() | |
.setCenterId("TestCenterId") | |
.setCenterName("TestCenterName") | |
.setActiveStatus(1) | |
.setAddress("TestAddress") | |
.setAdminEmail("Test@test.com") | |
.setAdminName("TestAdmin") | |
.setFaxNumber("1234567890") | |
.setPhoneNumber("1234567890") | |
.setRedundantStatus(1) | |
.build(); | |
entity.initSave("TestUser"); | |
} | |
private void prepareEntities() { | |
entities = new ArrayList<>(); | |
entities.add(entity); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RunWith(SpringJUnit4ClassRunner.class) | |
@ContextConfiguration(locations = {"classpath:spring-service.xml", "classpath:spring-mapper-test.xml"}) | |
public class FacadeTest { | |
private Entity entity; | |
private List<Entity> entities; | |
@InjectMocks | |
@Autowired | |
Facade facade; | |
@Mock | |
Service service; | |
@Before | |
public void setUp() { | |
MockitoAnnotations.initMocks(this); | |
prepareEntity(); | |
prepareEntities(); | |
} | |
@Test | |
public void testFindIsActive() { | |
mockServiceFindIsActive(); | |
Assert.assertNotNull(facade.findIsActive()); | |
} | |
@Test | |
public void testuUpdatelListAndGenXML() { | |
mockServiceUpdatelListAndGenXML(); | |
try { | |
facade.UpdatelListAndGenXML(entities); | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
Assert.assertNotNull(ex); | |
} | |
} | |
private void mockServiceUpdatelListAndGenXML() { | |
Mockito.doNothing().when(service).updateCentralList(Mockito.any()); | |
Mockito.doNothing().when(service).genRedundantXML(Mockito.any()); | |
} | |
private void mockServiceFindIsActive() { | |
Mockito.doReturn(entities).when(service).findIsActive(); | |
} | |
private void prepareEntities() { | |
entities = new ArrayList<>(); | |
entities.add(entity); | |
} | |
private void prepareEntity() { | |
entity = Entity.Builder.creat() | |
.setCenterId("TestId") | |
.setCenterName("TestName") | |
.setActiveStatus(1) | |
.setAddress("TestAddress") | |
.setAdminEmail("Test@test.com") | |
.setAdminName("TestAdmin") | |
.setFaxNumber("1234567890") | |
.setPhoneNumber("1234567890") | |
.setRedundantStatus(1) | |
.build(); | |
entity.initSave("TestUser"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment