首页 > 解决方案 > JUnit 测试用例:参数不同!通缉:

问题描述

我正在尝试使用在控制器级别JUnit为请求编写测试用例。当我这样做时,我遇到了测试用例失败POSTmockitoMockito.verify

AccountMaintenanceSave

public class AccountMaintenanceSave {   

    public Integer nAccountId;
    public String sClientAcctId;
    public String sAcctDesc;
    public String sLocation;
    public Integer nDeptId; 
    public Integer nAccountCPCMappingid;    
    public Integer nInvestigatorId;

    //Getter and Setter

    @Override
    public String toString() {
        return "AccountMaintenanceSave [nAccountId=" + nAccountId + ", sClientAcctId=" + sClientAcctId + ", sAcctDesc="
                + sAcctDesc + ", sLocation=" + sLocation + ", nDeptId=" + nDeptId + ", nAccountCPCMappingid="
                + nAccountCPCMappingid + ", nInvestigatorId=" + nInvestigatorId + "]";
    }       
    @Override
    public boolean equals(Object accountMain) {
        if (this == accountMain) return true;
        if (accountMain == null || getClass() != accountMain.getClass()) return false;

        final AccountMaintenanceSave that = (AccountMaintenanceSave) accountMain;

        if (nAccountId != that.nAccountId) return false;
        if (nAccountCPCMappingid != 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;
    }

    @Override
    public int hashCode() {
        int result = nAccountId;
        result = 31 * result + (sLocation != null ? sLocation.hashCode() : 0);
        result = 31 * result + nAccountCPCMappingid;
        result = 31 * result + (sAcctDesc != null ? sAcctDesc.hashCode() : 0);
        result = 31 * result + nDeptId;
        result = 31 * result + (sClientAcctId != null ? sClientAcctId.hashCode() : 0);
        result = 31 * result + nInvestigatorId;
        return result;
    }
}

帐户控制器

@RestController
@RequestMapping("/api.spacestudy.com/SpaceStudy/Admin")
public class AccountController {

    @Autowired
    AccountService accService;

    @CrossOrigin(origins = "http://localhost:4200")
    @PutMapping("/Account/saveAttributes")
    public  ResponseEntity<Object> btnSaveClick(@RequestBody AccountMaintenanceSave saveObj) {
        return accService.btnSaveClick(saveObj);// line no 42

    }   

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 {

        String json = "{\n" + " \"nAccountId\":65175, \"sLocation\": \"B\",\n"
              + " \"nAccountCPCMappingid\":8 ,\"sAcctDesc\":\"FY07 GEN GIFTS\" ,\n"
              + " \"nDeptId\":21728, \"sClientAcctId\":\"2100012\",\n" 
              + " \"nInvestigatorId\":65760 }";

        AccountMaintenanceSave accountMain = new AccountMaintenanceSave();
    accountMain.setnAccountId(65175);
    accountMain.setsLocation("B");
    accountMain.setnAccountCPCMappingid(8);
    accountMain.setsAcctDesc("FY07 GEN GIFTS");
    accountMain.setnDeptId(21728);
    accountMain.setsClientAcctId("2100012");
    accountMain.setnInvestigatorId(65760);

    Mockito.when(accountService.btnSaveClick(accountMain))
            .thenReturn(new ResponseEntity<>(accountMain.toString(), HttpStatus.OK));

    RequestBuilder requestBuilder = MockMvcRequestBuilders
            .put("/api.spacestudy.com/SpaceStudy/Admin/Account/saveAttributes").accept(MediaType.APPLICATION_JSON)
            .contentType(MediaType.APPLICATION_JSON).content(json);

    MvcResult result = mockMvc.perform(requestBuilder).andReturn();

    MockHttpServletResponse response = result.getResponse();

    assertEquals(HttpStatus.OK.value(), response.getStatus());

    Mockito.verify(accountService).btnSaveClick(accountMain).toString();
    }
}

堆栈跟踪

   Argument(s) are different! Wanted:
accountService.btnSaveClick(
    AccountMaintenanceSave [nAccountId=65175, sClientAcctId=2100012, sAcctDesc=FY07 GEN GIFTS, sLocation=B, nDeptId=21728, nAccountCPCMappingid=8, nInvestigatorId=65760]
);
-> at com.spacestudy.AccountControllerTest.btnSaveClickTest(AccountControllerTest.java:71)
Actual invocation has different arguments:
accountService.btnSaveClick(
    AccountMaintenanceSave [nAccountId=65175, sClientAcctId=2100012, sAcctDesc=FY07 GEN GIFTS, sLocation=B, nDeptId=21728, nAccountCPCMappingid=8, nInvestigatorId=65760]
);
-> at com.spacestudy.controller.AccountController.btnSaveClick(AccountController.java:42)

    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.spacestudy.AccountControllerTest.btnSaveClickTest(AccountControllerTest.java:71)
    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)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)

标签: javaspringunit-testingmockitojunit4

解决方案


根据您的equals方法,您似乎正在根据引用相等比较 Objects (boxed primitives) 。无论您是否以及如何使用 Mockito,这都是一个错误。

尽管int可以使用 比较==像这样的原语,但不应将像 Integer 这样的盒装原语与 进行比较==:您可能有两个单独的 Integer 实例,每个实例都表示 integer 3,这样firstInt != secondInt但是firstInt.equals(secondInt)是正确的。这可能导致不一致的行为:Java 机会主义地尝试对文字字符串和小整数使用相同的实例,但您不能依赖这种行为。toString如您所见,这也会导致您的输出相同。

这里最好的答案是使用像 Integer 这样的原语int而不是像 Integer 这样的盒装原语:只要结果总是非空的,你就不会通过存储盒装版本获得任何东西。如果您这样做,那么您当前的equals方法(首先在已删除的答案中通过 glytching 发布)将可以正常工作。


如果您出于某种原因需要使用盒装原语,您应该将它们与它们的equals方法进行比较。

因此,而不是像这样的检查:

    if (nAccountId != that.nAccountId) return false;
    if (nAccountCPCMappingid != that.nAccountCPCMappingid) return false;

像这样进行检查:

    if (!nAccountId.equals(that.nAccountId)) return false;
    if (!nAccountCPCMappingid.equals(that.nAccountCPCMappingid)) return false;

请注意,整数字段,如字符串字段,可以是null. 为了防止这种情况发生,您应该null像使用 Strings 一样进行显式检查,或者使用Java 8 中的新Objects.equals方法:

    if (!Objects.equals(this.nAccountId, that.nAccountId)) return false;
    if (!Objects.equals(this.nAccountCPCMappingid, that.nAccountCPCMappingid)) return false;

推荐阅读