我的情景如下
class SuperClass{
public void run(){
System.out.println("I am running in Super class");
}
}
class ChildClass extends SuperClass{
public void childRunner(){
System.out.println("Step 1");
System.out.println("Step 2");
**run();**
System.out.println("Last Step");
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想模拟childRunner()方法,ChildClass因为这个方法在内部调用超类方法,我需要一些关于如何模拟run()方法中存在的childRunner()方法的帮助/代码片段ChildClass.
我需要模拟对 GenericService 的 findById 方法的调用。
我有这个:
public class UserServiceImpl extends GenericServiceImpl<User Integer> implements UserService, Serializable {
....
// This call i want mock
user = findById(user.getId());
.....
// For example this one calls mockeo well. Why is not it a call to the generic service?
book = bookService.findById(id);
Run Code Online (Sandbox Code Playgroud)
问题出在第一个模拟中,因为它是对通用服务的调用。
第二个模拟也很好用
when(bookService.findById(anyInt())).thenReturn(mockedBook);
Run Code Online (Sandbox Code Playgroud) 这是我的代码 -
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.core.classloader.annotations.*;
import static org.powermock.api.support.SuppressCode.*;
class BaseService {
public int save() {
validate();
return 2;
}
public static int save2() {
return 5;
}
public void validate() {
System.out.println("base service save executing...");
}
}
class ChildService extends BaseService {
public int save() {
System.out.println("child service save executing...");
int x = super.save2();
int y = super.save();
System.out.println("super.save returned " + y);
load();
return 1 + x;
}
public void load() {
System.out.println("child service …Run Code Online (Sandbox Code Playgroud) 有时,当我编写单元测试时,我应该模拟对超类的引用。
我已经阅读了这个问题: 问题
如果超类方法足够大,这个答案另一个答案不合适。就我而言,我有非常大的代码。是的,我知道这违反了 SOLID OOD 原则,但我应该编写测试。我没有足够的时间进行重构。
这个问题是 4 年前提出的!
目前 Mockito 或 Powermock 可以解决这个问题吗?
代码示例:
class BaseService {
public void save() {
// a lot of code here! I cannot change this code.
}
}
public Childservice extends BaseService {
public void save(){
//logic for testing
super.save();
//logic for testing
}
}
Run Code Online (Sandbox Code Playgroud)
public class Parent {
public int save() {
return 99;
}
}
public class Child extends Parent …Run Code Online (Sandbox Code Playgroud) 我遇到了 Mockito 试图在存根时调用真实方法的问题。我认为这可能与该方法被继承有关。这是我的系统外部的组件,对此无能为力。但是让我们来看看代码
AbstractRpcClient abstractRpcClient = mock(AbstractRpcClient.class);
doNothing().when(abstractRpcClient).callOnce(anyString(), anyVararg());
Run Code Online (Sandbox Code Playgroud)
由于callOnce在其他一些对象上调用方法,我在第二行得到了 NPE。
AbstractRpcClient从另一个抽象类继承,但是这个类是本地包,所以我什至不能在我的存根中转换它。
有什么我可以做的吗?我怎样才能存根这个方法什么都不做或抛出异常而不调用真正的方法。
我是否必须在测试和覆盖方法中扩展这个类callOnce?这当然有效,但还有其他解决方案吗?
最小的例子:
package external.component;
public abstract class ClassToMock extends SuperClass {
}
abstract class SuperClass {
public void callOnce(String method, Object... params) {
throw new RuntimeException("We shouldn't be here");
}
}
Run Code Online (Sandbox Code Playgroud)
测试班
package mypackage
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.anyVararg;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
public class Testclass {
@Test
public void test() {
external.component.ClassToMock classToMock = mock(external.component.ClassToMock.class);
doNothing().when(classToMock).callOnce(anyString(), anyVararg());
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试为类的特定方法编写单元测试 - foo.这个类扩展了另一个类 - bar它在一个外部jar中.
问题是这个基础bar有一些与数据库交互的方法,我不想实际调用它.
我尝试创建这个基类foo的模拟,但这不起作用.它实际上尝试连接到数据库而不是模拟.
@Test
public void testSomeMethod(){
bar b= mock(bar.class);
when(b.calldatabase()).thenReturn(resultset); //calldatabse is in base class bar
//create expected object, and set properties here
Results expected = new Results();
expectedResult = foo.MethodUnderTest(); // this has call to calldatabase and then uses resultset mocked above
assert()...
}
Run Code Online (Sandbox Code Playgroud)
我正在使用JUnit4和Mockito.
它是否真的可以像这样 - 在基类中模拟方法但实际上测试派生类?如果没有,我该如何测试呢?
如果需要,我可以更改基类,并根据需要使用任何其他工具/库.