我在spring boot application中学习junit
。我正在尝试为Account controller 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()
如何运作?
请给我一些澄清?
答案 0 :(得分:0)
像这样初始化你的模拟
@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));