Skip to content

Commit

Permalink
junit demo remove import
Browse files Browse the repository at this point in the history
  • Loading branch information
edf91 committed Jan 1, 2018
1 parent eb9ee6c commit bd74fb7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class OtherInterfaceTest {
private String id = "id";

@Before
public void setUp(){
public void setUp() {
/**
* 如果otherInterface 本地开发未完成,或者网络不通等,则可以通过mock打桩
*/
Expand All @@ -38,19 +38,19 @@ public void setUp(){

// 测试otherInterface
@Test
public void assertOfId(){
public void assertOfId() {
String qId = otherInterface.getOf(id);
Assert.assertEquals(qId,id);
Assert.assertEquals(qId, id);
// 校验 getOf(id) 是否被调用1次
verify(otherInterface,times(1)).getOf(id);
verify(otherInterface, times(1)).getOf(id);
}

// 测试通过service 调用 otherService
@Test
public void assertCallOfId(){
public void assertCallOfId() {
String qId = service.of(id);
Assert.assertEquals(qId,id);
verify(otherInterface,times(1)).getOf(id);
Assert.assertEquals(qId, id);
verify(otherInterface, times(1)).getOf(id);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
Expand All @@ -30,32 +29,32 @@ public class SimpleControllerDemoTest {
public void setUp() throws Exception {
mvc = MockMvcBuilders.webAppContextSetup(context).build();

mvc.perform(MockMvcRequestBuilders.post("/simple/add").param("id",id))
mvc.perform(MockMvcRequestBuilders.post("/simple/add").param("id", id))
.andExpect(MockMvcResultMatchers.status().isOk());
}

@Test
public void assertOfId() throws Exception {
String qId = mvc.perform(MockMvcRequestBuilders.get(String.format("/simple/%s",id)))
String qId = mvc.perform(MockMvcRequestBuilders.get(String.format("/simple/%s", id)))
.andExpect(MockMvcResultMatchers.status().isOk())
.andReturn().getResponse().getContentAsString();
Assert.assertEquals(id,qId);
.andReturn().getResponse().getContentAsString();
Assert.assertEquals(id, qId);
}

@Test
public void assertDel() throws Exception {
remove();
String qId = mvc.perform(MockMvcRequestBuilders.get(String.format("/simple/%s",id)))
String qId = mvc.perform(MockMvcRequestBuilders.get(String.format("/simple/%s", id)))
.andExpect(MockMvcResultMatchers.status().isOk())
.andReturn().getResponse().getContentAsString();

Assert.assertEquals("",qId);
Assert.assertEquals("", qId);
}


private void remove() throws Exception {
mvc.perform(
MockMvcRequestBuilders.delete(String.format("/simple/%s/remove",id))
MockMvcRequestBuilders.delete(String.format("/simple/%s/remove", id))
).andExpect(MockMvcResultMatchers.status().isOk());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ public class SimpleServiceDemoTest {
private static String id = "id";

@Before
public void setUp(){
public void setUp() {
service.save(id);
}


@Test
public void assertOfId(){
public void assertOfId() {
String qId = service.of(id);
Assert.assertEquals(id,qId);
Is.is(Objects.equals(id,qId));
Assert.assertEquals(id, qId);
Is.is(Objects.equals(id, qId));
}

@Test
public void assertDelOfId(){
public void assertDelOfId() {
service.delOf(id);
String qId = service.of(id);

Expand All @@ -47,7 +47,7 @@ public void assertDelOfId(){
}

@After
public void clearUp(){
public void clearUp() {
service.delOf(id);
}

Expand Down

0 comments on commit bd74fb7

Please sign in to comment.