我在春季引导应用程序中学习junit,我试图为帐户控制器post方法编写JUnit测试用例。我的帐户控制器依赖于帐户服务方法,所以我使用mockito进行模拟。我试图像下面这样编写测试用例,但作为回应,我得到了null。
有人能告诉我我在测试用例中做错了什么吗?
AccountController
@PutMapping("/saveAttributes")
public ResponseEntity<Object> saveData(@RequestBody AccountMaintenanceSave saveObj){
return accService.saveData(saveObj);
}AccountControllerTest
@RunWith(SpringRunner.class)
public class AccountControllerTest {
private MockMvc mockMvc;
@Mock
private AccountService accountService;
@InjectMocks
private AccountController accountController;
@Before
public void setup() {
mockMvc = MockMvcBuilders.standaloneSetup(accountController).build();
}
@Test
public void btnSaveClickTest() throws Exception {
AccountMaintenanceSave mockAccountMainSave = new AccountMaintenanceSave();
mockAccountMainSave.setnAccountId(1);
mockAccountMainSave.setsLocation("B");
mockAccountMainSave.setnAccountCPCMappingid(3);
mockAccountMainSave.setnDeptId(5);
mockAccountMainSave.setsAcctDesc("abc");
mockAccountMainSave.setsClientAcctId("2");
mockAccountMainSave.setnInvestigatorId(4);
String InputInJson = this.mapToJson(mockAccountMainSave);
Mockito.when(accountService.saveData(mockAccountMainSave))
.thenReturn(new ResponseEntity<>(mockAccountMainSave, HttpStatus.OK));
MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders
.put("/api.spacestudy.com/SpaceStudy/Admin/Account/saveAttributes")
.accept(MediaType.APPLICATION_JSON)
.content(InputInJson)
.contentType(MediaType.APPLICATION_JSON);
MvcResult result = mockMvc.perform(requestBuilder).andReturn();
MockHttpServletResponse response = result.getResponse();
String outputInJson = response.getContentAsString();
assertEquals(InputInJson, outputInJson);
Mockito.verify(accountService).saveData(mockAccountMainSave);
}
private String mapToJson(Object object) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(object);
}
}堆栈跟踪
org.junit.ComparisonFailure: expected:<[{"nAccountId":1,"sClientAcctId":"2","sAcctDesc":"abc","sLocation":"B","nDeptId":5,"nAccountCPCMappingid":3,"nInvestigatorId":4}]> but was:<[]>
at org.junit.Assert.assertEquals(Assert.java:115)
at org.junit.Assert.assertEquals(Assert.java:144)
at com.spacestudy.controller.AccountControllerTest.btnSaveClickTest(AccountControllerTest.java:78)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)问题通过重写AccountMaintenanceSave类中的equals()来解决问题
@Override
public boolean equals(Object mockAccountMainSave) {
if (this == mockAccountMainSave) return true;
if (mockAccountMainSave == null || getClass() != mockAccountMainSave.getClass()) return false;
final AccountMaintenanceSave that = (AccountMaintenanceSave)mockAccountMainSave;
if (!nAccountId.equals(that.nAccountId)) return false;
if (!nAccountCPCMappingid.equals(that.nAccountCPCMappingid)) return false;
if (nDeptId != that.nDeptId) return false;
if (nInvestigatorId != that.nInvestigatorId) return false;
if (sLocation != null ? !sLocation.equals(that.sLocation) : that.sLocation != null) return false;
if (sAcctDesc != null ? !sAcctDesc.equals(that.sAcctDesc) : that.sAcctDesc != null) return false;
return sClientAcctId != null ? sClientAcctId.equals(that.sClientAcctId) : that.sClientAcctId == null;
} 但是,我仍然不知道在重写equals()之后它是如何工作的。内部assertEquals()是如何工作的?
请给我澄清一下?
发布于 2018-06-15 11:47:36
像这样初始化您的模拟程序
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders.standaloneSetup(accountController).build();
}然后像这样修改测试代码
MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders
.put("/api.spacestudy.com/SpaceStudy/Admin/Account/saveAttributes")
.accept(MediaType.APPLICATION_JSON)
.content(InputInJson)
.contentType(MediaType.APPLICATION_JSON);
mockMvc.perform(requestBuilder)
.andExpect(status().isOk())
.andExpect(content().json(InputInJson));https://stackoverflow.com/questions/50874196
复制相似问题