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.
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.
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.
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 –...
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.
You can mock beans using a test configuration:
@Configuration
public class TestConfig {
@Bean
public MyService myService() {
return Mockito.mock( MyService.class );
}
}
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With