junit - 调用抽象超类的私有方法时 Mockito 抛出 NPE
问题描述
我目前遇到了 vertx 和 junit 集成的问题
public class A extends B implements C {
@Override
public Maybe<Test> methodToTest(String test) throws IOException {
Tuple tuple = Tuple.tuple();
tuple.addString(test);
return getOne(TEST_QUERY, tuple).map(this::getFromRow).defaultIfEmpty(Test.builder().build()); //getFromRow just convert to object Test
}
}
public abstract class B {
@Inject
public MySqlPool mySQLPool;
public B() {
}
public Maybe<Row> getOne(String getOneQuery, Tuple tuple) {
return this.rxGetOne(getOneQuery, tuple, 300000L);
}
private Maybe<Row> rxGetOne(String getOneQuery, Tuple tuple, long timeout) {
return this.mySQLPool.preparedQuery(getOneQuery).rxExecute(tuple).timeout(timeout, TimeUnit.MILLISECONDS).flatMapPublisher(Flowable::fromIterable).firstElement().switchIfEmpty(Maybe.error(new NotFoundException("Record not found")));
}
}
创建了一个测试方法:尝试了 2 件事但获得了 NPE
A gD=new A();
Method method = B.class.getDeclaredMethod("getOne", String.class, Tuple.class);
method.setAccessible(true);
Tuple t=Tuple.tuple();
method.invoke(gD,anyString(),t);
B cut = Mockito.mock(B.class,Mockito.CALLS_REAL_METHODS);
Mockito.when(cut.getOne(anyString(),any())).thenAnswer(I->Maybe.just(r));
请告诉我一种调用超抽象类方法的方法以及如何模拟这些方法。
解决方案
推荐阅读
- firebase - 无法安装 nodered-contrib-firebase-storage
- javascript - 如何在 React 中更改状态对象内的键状态?
- .net - 是否可以通过 .net core 3.0 在 Linux 中使用 p4bridge.dll?
- python - 'int' 对象不可调用堆排序函数
- ios - Flutter ios 构建失败 - Distill 因未知原因而失败
- jenkins - Jenkinsfile 不能在 Windows (bat) 中 CD
- javascript - 如何正确等待 javascript 中异步函数的返回?
- java - 为什么 Gradle 找不到现有的工件?
- java - java - 如何在Java中另一个模拟对象的模拟中注入一个模拟对象?
- python - 通过 Flask 和 BytesIO 从 S3 显示文件(图像)