admin管理员组

文章数量:1391964

I have a class Foo with generics. Inside class Foo, I have an interface declared Bar.

public class Foo<T,U> {
    @Autowired
    private Bar<T, U> bar;

    public Mono<F> calculate(Supplier<Mono<Req<T, U>>> supplier) {
        // Trying to invoke a method on bar but bar is coming as null
    }
}
@RunWith(.mockito.junit.MockitoJUnitRunner.class)
class FooTest {
    @InjectMocks
    Foo<classA, classB> foo;

    @Mock
    private Bar<ClassC, ClassD> bar;

    // Method under test is calculate
}

I have tried adding mock name, in setup. I am also initializing mocks, but when I invoke that method.

MockitoAnnotations.initMocks(this);

I am getting the following error:

java.lang.AssertionError: expectation "expectComplete" failed (expected: onComplete(); actual: onError(java.lang.NullPointerException: Cannot invoke "foo.bar(Object, boolean)" because "parameter1" is null))

本文标签: junit5Junit issue mocking an implemetation classStack Overflow