Here is my test code
package com.tasks;
@ContextConfiguration(classes = ITest.class)
@ActiveProfiles(profiles = "test")
@RunWith(SpringJUnit4ClassRunner.class)
public class ITest {
@Inject
IAccessor iAccessor1;
@Test
public void testRun() {
}
@Configuration
@Profile("test")
@ComponentScan(basePackageClasses{com.tasks.ITest.class})
public static class ITestConfiguration{
@Bean
@Primary
public IAccessor iDataAccessor(){
IAccessor iAccessor = mock(IAccessor.class);
return iAccessor;
}
}
}
I tried @Autowired instead of Inject, but got the same error.
In my class under test I have
@Component
public class ISync {
@Inject
private IAccessor iAccessor;
public int someMethod(){
iAccessor.someOtherMethod(); //want to mock out
}
}
So I want to inject a mocked up value. However I get a depenedency error
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.database.iAccessor1' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.inject.Inject()}
It works generally for me, but I do not know what is the problem here in this particular case. I want the answer to what is the problem with my solution.
I already tried : Spring JUnit: How to Mock autowired component in autowired component 1) The accepted answer is using testContext.xml I do not use any xml. 2) I used @MockBean but I started getting other error.
java.lang.NoSuchMethodError: org.mockito.Mockito.mockingDetails(Ljava/lang/Object;)Lorg/mockito/MockingDetails;
TO rectify it I bumped the version of mockito-all 1.9.0 to 1.9.5 (though I don't know why is it trying to find it).
java.lang.NoSuchMethodError: org.mockito.internal.util.MockUtil.getMockSettings(Ljava/lang/Object;)Lorg/mockito/mock/MockCreationSettings;
I got it working by bumping the version to 1.0.19. But the value in the class under test was still null. So this didn't work for me.
I want to know what is the problem with my original solution?