JMock:预期一次,从未调用

JMock: expected once, never invoked

我试图期望我的 getFirstName() 到 return "Jackson" 和 getLastName() 到 return "Chin",然后我执行 setStaffName() 以便name 实例是用 "Jackson" 和 "Chin" 创建和初始化的。但是,它说它从未被调用过。 never invoked 是什么意思?

我是 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();

    }
}

这是我的名字Class

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 对您想要的内容的期望 test/mock 或者根本不使用任何模拟。请记住,"mocking" 不能替代简单的 JUnit 断言,例如 assertEquals().