我有一個(gè)使用 JMockit 進(jìn)行單元測(cè)試的簡(jiǎn)單實(shí)現(xiàn)。實(shí)現(xiàn)如下Team班級(jí)public class Team { private TeamLeader teamLeader; public Team(TeamLeader teamleader) { this.teamLeader = teamleader; } public boolean hasDiscussion() { System.out.println("Discussion is hold by " + teamLeader.getName()); Discussion discussion = new Discussion(); return discussion.start(); }}TeamLeader班級(jí)public class TeamLeader { private String name; public TeamLeader(String name) { this.name = name; } public String getName() { return name; }}Discussion班級(jí)public class Discussion { public boolean start() { return false; }}然后,我有一個(gè)單元測(cè)試來(lái)測(cè)試功能hasDiscussion如下@RunWith(JMockit.class)public class TeamTest { private Team team; @Test public void hasDiscussion(@Mocked TeamLeader teamLeader, @Mocked Discussion discussion) { new Expectations() {{ teamLeader.getName(); result = "teamleader"; discussion.start(); result = true; }}; team = new Team(teamLeader); boolean actualResult = team.hasDiscussion(); new Verifications() {{ assertThat(actualResult, is(true)); }}; }}執(zhí)行測(cè)試時(shí),出現(xiàn)以下錯(cuò)誤Unexpected invocation of:entity.Discussion#Discussion() on instance: entity.Discussion@7b70deb2when was expecting an invocation of:entity.Discussion#start() on mock instance: entity.Discussion@6f93ad02mockit.internal.UnexpectedInvocation: Unexpected invocation of:entity.Discussion#Discussion() on instance: entity.Discussion@7b70deb2when was expecting an invocation of:對(duì)這類(lèi)問(wèn)題有什么想法嗎?
JMockit - 意外調(diào)用
www說(shuō)
2023-04-26 16:49:41