首页 > 解决方案 > 调用抽象超类的私有方法时 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));
  

请告诉我一种调用超抽象类方法的方法以及如何模拟这些方法。

标签: junitmockitovert.x

解决方案


推荐阅读