我想模拟一个正在测试的类的私有方法但是方法在调用方法之后首先返回false两次,之后它应该返回false.这是我试过的代码.这是正在测试的类
public class ClassToTest
{
public void methodToTest()
{
Integer integerInstance = new Integer(0);
boolean returnValue= methodToMock(integerInstance);
if(returnValue)
{
System.out.println("methodToMock returned true");
}
else
{
System.out.println("methodToMock returned true");
}
System.out.println();
}
private boolean methodToMock(int value)
{
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
考试班
import org.junit.Test;
import static mockit.Deencapsulation.*;
import mockit.*;
public class TestAClass
{
@Tested ClassToTest classToTestInstance;
@Test
public void test1()
{
new NonStrictExpectations(classToTestInstance)
{
{
invoke(classToTestInstance, "methodToMock", anyInt);
returns(false);
times = 2;
invoke(classToTestInstance, "methodToMock", anyInt);
returns(true);
times = 1;
}
}; …Run Code Online (Sandbox Code Playgroud) 将int传递给以Integer作为参数的方法是否可以.这是代码
public class PassingInt
{
public static void main(String args[])
{
int a = -1;
passIntToInteger(a);//Is this Ok?
}
private static void passIntToInteger(Integer a)
{
System.out.println(a);
}
}
Run Code Online (Sandbox Code Playgroud)