如何使用JMockit或其他机制检查方法中的局部变量的值

时间:2015-02-06 06:58:47

标签: java junit jmockit

如何使用JMockit或其他机制检查方法中局部变量的值?例如

public class ClassToTest
{
    DatabaseOperations databaseOperations;
    public void someMethod()
    {
        SomeClass someClass = new SomeClass();
        databaseOperations = new DatabaseOperations();
        someClass.setValue1(2);
        someClass.setValue2("some String value 3");
        databaseOperations.addObjectToDatabase(someClass);//I have mocked the class DatabaseOperations.
    }
}

public class SomeClass
{
    int value1;
    String value2;
    public int getValue1()
    {
        return value1;
    }
    public void setValue1(int value1)
    {
        this.value1 = value1;
    }
    public String getValue2()
    {
        return value2;
    }
    public void setValue2(String value2)
    {
        this.value2 = value2;
    }
}

如何检查someMethod()为本地实例someClass设置的值。

1 个答案:

答案 0 :(得分:0)

这个片段是否适合您?

public class MyTest {

    @Test
    public void classToTest_someMethod(@Mocked final SomeClass someClassMock) {
        new Expectations() {
            {
                new SomeClass();
                result = someClassMock;

                someClassMock.setValue1(2);
                someClassMock.setValue2("some String value 3");
            }
        };
        new ClassToTest().someMethod();
    }
}