我正在尝试使用POST
在控制器级别为mockito
请求编写Mockito.verify
测试用例。当我在做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;
}
}
时,我收到测试用例失败
AccountMaintenanceSave
@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
}
的AccountController
@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();
}
}
AccountControllerTest
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)
堆栈跟踪
// Set up max queued messages
var maxMessages = float64(100000)
// Set up max redis senders
var maxSender = float64(5)
// Set up message channel
var messages = make(chan Message, int(maxMessages))
// Set up messages per sender count
var senderRatio = maxSender / maxMessages
type Message struct {
ChatId int `json:"chatId"`
UserId int `json:"userId"`
Message string `json:"message"`
Date int `json:"date"`
}
func RedisWriteHandler(messageChannel chan Message) {
senderCount := 0
killswitch := make(chan string)
for {
length := float64(len(messageChannel))
neededSenders := int(math.Ceil(length * senderRatio))
if senderCount < neededSenders || senderCount < 1 {
log.Printf("Increasing sender count to %d, need %d", senderCount+1, neededSenders)
go addRedisSender(messageChannel, killswitch)
senderCount++
} else if senderCount > neededSenders && senderCount > 1 {
log.Printf("Decreasing sender count to %d, need %d", senderCount-1, neededSenders)
killMessage := fmt.Sprintf("only need %d senders", neededSenders)
killswitch <- killMessage
senderCount--
}
}
log.Fatal("The redis handler unexpectedly went away")
}
func addRedisSender(messageChannel chan Message, killswitch chan string) {
c, err := redis.Dial("tcp", "localhost:6379")
if err != nil {
log.Println(err)
return
}
defer c.Close()
for {
select {
case msg := <-messageChannel:
redisChannel := strconv.Itoa(msg.ChatId)
messageBlob, err := json.Marshal(msg)
if err != nil {
log.Println(err)
}
_, err = c.Do("PUBLISH", redisChannel, messageBlob)
if err != nil {
log.Println(err)
}
case kill := <-killswitch:
log.Printf("Sender killed: %s", kill)
return
}
}
log.Println("Closing redis sender")
}
答案 0 :(得分:1)
根据您的equals
方法,您似乎正在根据参照平等比较对象(盒装基元)。无论您是否以及如何使用Mockito,这都是一个错误。
尽管可以使用int
安全地比较像==
这样的原语,但是像Integer这样的盒装基元不应该与==
进行比较:您可能有两个单独的Integer实例,每个实例都代表整数3
,使firstInt != secondInt
但firstInt.equals(secondInt)
为真。这可能导致行为不一致:Java机会性地尝试对文字字符串和小整数使用相同的实例,但您不能依赖此行为。这也会导致您的toString
输出与您所看到的相同。
这里最好的答案是使用像int
这样的原语而不是像整数这样的盒装原语:只要结果总是非空,你就不会通过存储盒装版本获得任何东西。如果你这样做,那么你当前的equals
方法(首先在删除的答案中通过拼写发布)将会正常工作。
如果您因任何原因需要保留盒装基元,那么您应该将它们与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
,或者在Java 8中使用新的Objects.equals方法:
if (!Objects.equals(this.nAccountId, that.nAccountId)) return false;
if (!Objects.equals(this.nAccountCPCMappingid, that.nAccountCPCMappingid)) return false;