如何覆盖单元测试中被调用的方法,class 被测试
How to override a method in unit tests that is called from which the class being tested
我正在测试 class A 的函数 func1。
Func1有一个局部变量ClassB,调用了B的函数func2。代码看起来像这样:
public Class A
{
public func1()
{
B object = new B();
int x = object.func2(something);
}
}
当我在单元测试中测试 func1 时,我不想调用 func2。
所以我想在测试中做这样的事情:
B textObject = new B()
{
@override
int func2(something)
{
return 5;
}
}
但它仍在调用 class B 中的 func2。请建议如何处理此问题。
如果您想覆盖 new B()
构造函数调用 - 将其放在自己的方法中。
public Class A
{
public func1()
{
B object = newB();
int x = object.func2(something);
}
protected B newB(){
return new B();
}
}
在您的测试中,您可以覆盖 B
构造函数调用。
public class APartitialMock extends A {
protected B newB(){
return new BMock();
}
}
public class BMock extends B {
int func2(something) {
return 5
}
}
然后使用 APartitialMock
来测试 func1
和你的 B
。
PS 如果您可以或想要使用框架,请查看 powermock - Mock Constructor
I can make B as a class variable in A but that doesn't seem to help
either. What would you suggest in this case?
如果把B做成class变量,那么就可以mock B,"swap"在A的被测对象中
可能不是很优雅,但是又快又简单。
一个例子:
public class B {
int func2(int something){
return 3*something;
}
}
public class A
{
// declare it as protected - a test need to have access to this field to replace it
protected B object = new B();
public int func1()
{
int x = object.func2(22);
return x;
}
}
还有一个测试:
import static org.mockito.Mockito.*;
import static org.junit.Assert.*;
import org.junit.Test;
public class ATest {
@Test
public void test() {
A myA = new A();
B myB = mock(B.class);
// dummy function
when(myB.func2(anyInt())).thenReturn(20);
// replace B with mocked B
myA.object = myB;
int actual = myA.func1();
assertEquals(20, actual);
}
}
我正在测试 class A 的函数 func1。
Func1有一个局部变量ClassB,调用了B的函数func2。代码看起来像这样:
public Class A
{
public func1()
{
B object = new B();
int x = object.func2(something);
}
}
当我在单元测试中测试 func1 时,我不想调用 func2。
所以我想在测试中做这样的事情:
B textObject = new B()
{
@override
int func2(something)
{
return 5;
}
}
但它仍在调用 class B 中的 func2。请建议如何处理此问题。
如果您想覆盖 new B()
构造函数调用 - 将其放在自己的方法中。
public Class A
{
public func1()
{
B object = newB();
int x = object.func2(something);
}
protected B newB(){
return new B();
}
}
在您的测试中,您可以覆盖 B
构造函数调用。
public class APartitialMock extends A {
protected B newB(){
return new BMock();
}
}
public class BMock extends B {
int func2(something) {
return 5
}
}
然后使用 APartitialMock
来测试 func1
和你的 B
。
PS 如果您可以或想要使用框架,请查看 powermock - Mock Constructor
I can make B as a class variable in A but that doesn't seem to help either. What would you suggest in this case?
如果把B做成class变量,那么就可以mock B,"swap"在A的被测对象中
可能不是很优雅,但是又快又简单。
一个例子:
public class B {
int func2(int something){
return 3*something;
}
}
public class A
{
// declare it as protected - a test need to have access to this field to replace it
protected B object = new B();
public int func1()
{
int x = object.func2(22);
return x;
}
}
还有一个测试:
import static org.mockito.Mockito.*;
import static org.junit.Assert.*;
import org.junit.Test;
public class ATest {
@Test
public void test() {
A myA = new A();
B myB = mock(B.class);
// dummy function
when(myB.func2(anyInt())).thenReturn(20);
// replace B with mocked B
myA.object = myB;
int actual = myA.func1();
assertEquals(20, actual);
}
}