Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking the Qualified beans using mockito for a spring-boot application

consider my scenario

public class SomeClass {
  @Autowired @Qualifier("converter1") private IConverter converter1;
  @Autowired @Qualifier("converter2") private IConverter converter2;

  public void doSomeAction(String mimeType) {
    converter1.execute();
    converter2.execute();
  }
}

This is my code.

In order to test this

@RunWith(MockitoJUnitRunner.class)
public class SomeClassTest {
  @Mock(name="converter1") IConverter converter1;
  @Mock(name="converter2") IConverter converter2;
  @InjectMocks SomeClass class = new SomeClass();
  @Test
  public void testGetListOfExcelConverters() throws Exception {
    class.doSomeAction("abcd");
  }
}

Here the mocks are not getting injected, please help with the proper mechanism for mocking a qualified beans.

If this is not the right way to code using spring, please let me know the correct method for using this.

like image 935
amith Avatar asked Jun 07 '16 06:06

amith


People also ask

How do I use Mockito in Spring Boot?

The Spring Boot test support will then automatically create a Mockito mock of type SendMoneyUseCase and add it to the application context so that our controller can use it. In the test method, we can then use Mockito’s given () and when () methods just like above.

What is the best mocking framework for Spring Boot?

As a mocking framework, we’ll use Mockito, since it’s well-rounded, well-established, and well-integrated into Spring Boot. But the best kind of test doesn’t use Spring at all, so let’s first look at how to use Mockito in a plain unit test to mock away unwanted dependencies.

What is @mockbean annotation in Spring Boot?

Spring Boot's @MockBean Annotation. We can use the @MockBean to add mock objects to the Spring application context. The mock will replace any existing bean of the same type in the application context. If no bean of the same type is defined, a new one will be added. This annotation is useful in integration tests where a particular bean –...

Is it possible to mock spring beans in Java?

Learn how to mock Spring Beans in the current Java ecosystem. Join the DZone community and get the full member experience. EDIT: As of Spring Boot 1.4.0, faking of Spring Beans is supported natively via annotation @MockBean. Read Spring Boot docs for more info. About a year ago, I wrote a blog post on how to mock Spring Beans.


3 Answers

You can mock beans using a test configuration:

@Configuration
public class TestConfig {
   @Bean
   public MyService myService() {
      return Mockito.mock( MyService.class );
   }
}
like image 124
riddy Avatar answered Oct 29 '22 13:10

riddy


I've found this solution:

@RunWith(MockitoJUnitRunner.class)
public class SomeClassTest {

  @Mock()
  @Qualifier("converter1")
  IConverter converter1;

  @Mock() 
  @Qualifier("converter1")
  IConverter converter2;

  @InjectMocks SomeClassTest testObj = new SomeClassTest();

  @Test
  public void testGetListOfExcelConverters() throws Exception {
    testObj.doSomeAction("abcd");
    verify(converter1).execute();
    verify(converter2).execute();
  }
}

BTW, I haven't found this in doc.

like image 35
turulb Avatar answered Oct 29 '22 12:10

turulb


For me, both existing answers were insufficient.

@riddy 's answer did not take into account different test cases.

@jhericks ' answer did not use the Spring context, which caused other issues down the line.

Here's my solution:

@MockBean
@Qualifier("myNamedBean")
private SomeBean someBean;

As simple as that.

like image 41
Markus Appel Avatar answered Oct 29 '22 12:10

Markus Appel