【问题标题】:JMock: expected once, never invokedJMock:预期一次,从不调用
【发布时间】:2020-04-14 00:28:51
【问题描述】:

我试图期望我的 getFirstName() 返回“Jackson”和 getLastName() 返回“Chin”,然后我执行 setStaffName() 以便创建 name 实例并使用“杰克逊”和“下巴”。但是,它说它从未被调用过。从不调用是什么意思?

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

这是我的教职员工课

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);
    }
 ...
}

【问题讨论】:

    标签: java junit jmock


    【解决方案1】:

    从不调用是什么意思?

    我的意思是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()

    【讨论】:

    • 哦...好吧,明白谢谢~我将检查更改为 assertEquals() 并且它有效:) 非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-15
    • 2021-08-15
    相关资源
    最近更新 更多