如何使用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
设置的值。
答案 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();
}
}