jeudi 7 décembre 2017

Spring Boot test mock bean

How mock 2 beans when one is into second ?

public class A {
...
}

public class B {
private A a;
}

I tried:

@MockBean 
private A a;

@InjectMocks 
private B b;

@Before
public void executedBeforeEach() {
MockitoAnnotations.initMocks(this);
}

but had exception:

org.mockito.exceptions.base.MockitoException: 
Cannot instantiate @InjectMocks field named 'B'.
You haven't provided the instance at field declaration so I tried to     construct the instance.
However, I failed because: the type 'B' is an interface.

spring version:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.8.RELEASE</version>
    <relativePath/>
</parent>

test dependency:

<dependencies>
...
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    </dependency>
 </dependencies>

how to do it right ?where did I make a mistake ?

Aucun commentaire:

Enregistrer un commentaire