gpt4 book ai didi

java - JMock:预期一次,从未调用

转载 作者:行者123 更新时间:2023-12-01 19:13:46 26 4
gpt4 key购买 nike

我试图期望我的 getFirstName() 返回“Jackson”并且 getLastName() 返回“Chin”,然后我执行 setStaffName() 以便创建并初始化 name 实例“ jackson ”和“钦”。然而,它说它从未被调用过。从不调用是什么意思?

我是 JMock 新手,谁能告诉我代码中的问题吗?有什么解决办法吗?

not all expectations were satisfied
expectations:
! expected once, never invoked: name.getFirstName(); returns "Jackson"
! expected once, never invoked: name.getLastName(); returns "Chin"
what happened before this: nothing!

上面是它显示的错误,下面是我的代码..

package userManual;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.imposters.ByteBuddyClassImposteriser;
import org.junit.Test;


public class StaffTest {
Mockery testName = new Mockery(){{
setImposteriser(ByteBuddyClassImposteriser.INSTANCE);
}};
@Test
public void testStaffNameInitialization() {
final Name name = testName.mock(Name.class);

Staff staff = new Staff();

final String firstName = "Jackson";
final String lastName = "Chin";

//expectations
testName.checking(new Expectations() {{
oneOf(name).getFirstName(); will(returnValue(firstName));
oneOf(name).getLastName(); will(returnValue(lastName));
}});

//execute
staff.setStaffName(firstName, lastName);

//verify
testName.assertIsSatisfied();

}
}

这是我的名字类

public class Name {
private String firstName;
private String lastName;
int i;

public Name() {}

public Name(String firstName, String lastName){
this.firstName = firstName;
this.lastName = lastName;
}

public String getFirstName(){
return firstName;
}

public void setFirstName(String firstName){
this.firstName = firstName;
}

public String getLastName(){
return lastName;
}

public void setLastName(String lastName){
this.lastName = lastName;
}

public String toString() {
return String.format("%s %s", firstName, lastName);
}
}

这是我的员工类(class)

package userManual;

public class Staff{
protected String staffId;
protected String staffPassword;
protected String staffPhoneNo;
protected String staffPosition;
protected Name staffName;

public Staff() {}

public Staff(String staffId, String firstName, String lastName, String staffPassword, String staffPhoneNo, String staffPosition){
this.staffName = new Name(firstName, lastName);
this.staffId = staffId;
this.staffPassword = staffPassword;
this.staffPhoneNo = staffPhoneNo;
this.staffPosition = staffPosition;
}

public Name getStaffName() {
return staffName;
}

public void setStaffName(String firstName, String lastName) {
this.staffName = new Name(firstName, lastName);
}
...
}

最佳答案

What does never invoked means?

我的意思是 getFirstName()getLastName() 方法不会被任何人执行。通过查看类 NameStaff 的源代码,这是正确的,没有 getFirstName()getLastName( ) 在任何地方调用。但是,使用您的代码

 //expectations
testName.checking(new Expectations() {{
oneOf(name).getFirstName(); will(returnValue(firstName));
oneOf(name).getLastName(); will(returnValue(lastName));
}});

您写道,您希望调用这些方法。

当您编写 testName.assertIsSatisfied(); 时,它会检查是否确实如此。在你的情况下它没有,所以你将从上面得到断言消息。

根据您想要在此处测试的内容,您需要重写 JMock 期望以实现您想要测试/模拟的内容,或者根本不使用任何模拟。请记住,“模拟”并不能替代简单的 JUnit 断言,例如 assertEquals()

关于java - JMock:预期一次,从未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59443083/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com