使用Mock测试函数



我想测试一个函数,该函数内部有对另一个函数的调用(它接受两个参数(我想嘲笑一下内部函数。但我很难通过什么作为参数

void func(classA a,classB b)
{
List<ClassC> list= Objd.func2(a.getSome(),b)
///some other things
}

现在我想模拟这个函数2,我已经有了obj C.类的接口

我试过使用

InterfaceClassD objD=Mockito.mock(InterfaceClassD.class)
Mockito.when(obj.func2(a.getsome(),b)).thenreturn(null);

但它不起作用,func2执行

我甚至试过

InterfaceClassD objD=Mockito.mock(InterfaceClassD.class)
Mockito.when(obj.func2(anyList(),any(classB.class)).thenreturn(null);

注意-a.getSome((返回列表但它也不起作用,func2执行

如果没有更多的上下文,很难回答这个问题。

在函数"中;func";,对";Objd";来自它在哪里申报?

在测试中创建和使用模拟有两个步骤:

  1. 创建mock
  2. 注入mock

";注入模拟";指的是(以某种方式(将函数将使用的对真实对象的引用替换为您使用Mockito创建的mock版本。

示例:

// Class under test
public class Foo {
// this is the object we need to mock
private InterfaceClassD objD;
void func(classA a,classB b) {
// here is where we use "objD"
List<ClassC> list= objD.func2(a.getSome(),b)
///some other things
}
}

为了用mock objD编写测试,您必须将";真实的";在调用要测试的方法(函数(之前,使用mock的objD。

通常,它使用构造函数注入、setter注入或Mockito注释来完成。

构造函数注入示例:

// Class under test
public class Foo {
// this is the object we need to mock
private InterfaceClassD objD;
// constructor injection
public Foo(InterfaceClassD objD) {
this.objD = objD;
}
void func(classA a,classB b) {
// here is where we use "objD"
List<ClassC> list= objD.func2(a.getSome(),b);
///some other things
}
}
// Unit test
public class FooTest {
@Test
public void someTest() {
// create the mock:
InterfaceClassD mock = Mockito.mock(InterfaceClassD.class);
// inject the mock:
Foo foo = new Foo(objD);
// test: this should call method func but use the mock
foo.func(dummyA, dummyB);
// verify/assert something...
}
}

使用setter注入的示例:

// Class under test
public class Foo {
// this is the object we need to mock
private InterfaceClassD objD;
// setter injection
public void setInterfaceClassD(InterfaceClassD objD) {
this.objD = objD;
}
void func(classA a,classB b) {
// here is where we use "objD"
List<ClassC> list= objD.func2(a.getSome(),b);
///some other things
}
}
// Unit test
public class FooTest {
@Test
public void someTest() {
// create the mock:
InterfaceClassD mock = Mockito.mock(InterfaceClassD.class);
Foo foo = new Foo();
// inject the mock:
foo.setInterfaceClassD(mock);
// test: this should call method func but use the mock
foo.func(dummyA, dummyB);
// verify/assert something...
}
}

使用Mockito注释和JUnit5:的示例

// Class under test
public class Foo {
// this is the object we need to mock
private InterfaceClassD objD;
void func(classA a,classB b) {
// here is where we use "objD"
List<ClassC> list= objD.func2(a.getSome(),b);
///some other things
}
}
// Unit test
// Note the Mockito extension annotation.
@ExtendWith(MockitoExtension.class)
public class FooTest {
// the Mockito JUnit extension will instantiate this automagically.
// The name of the mock must exactly match the name of the field in
// containing class.
private @Mock InterfaceClassD objD;
// Mockito annotation processing will create an instance of Foo, and
// replace any of its fields with any mocks that have the same name
// and type. In this case, objD.
private @InjectMocks Foo foo;
@Test
public void someTest() {
// Just use it.
// test: this should call method func but use the mock
foo.func(dummyA, dummyB);
// verify/assert something...
}
}

最新更新