It automatically injects mockito mocks into your spring services for dependencies that are not found on the classpath (i.e. that are not registered in Spring).
Add the test dependency to your project:
<dependency>
<groupId>com.github.rinoto.spring</groupId>
<artifactId>spring-auto-mock</artifactId>
<version>0.3</version>
<scope>test</scope>
</dependency>
To use it, just register the AutoMockRegistryPostProcessor
in your test. E.g.:
@ContextConfiguration(classes = { AutoMockRegistryPostProcessor.class, RestOfClasses.class, ... })
@RunWith(SpringJUnit4ClassRunner.class)
public class YourTest {
...
}
The mocked service can also be injected in your tests. See a usage example in https://github.com/rinoto/spring-auto-mock/blob/master/src/test/java/sbg/rinoto/spring/mock/AutoMockRegistryProcessorTest.java
You need spring-auto-mock only if your tests need to run with SpringJUnit4ClassRunner, and you have a lot of dependencies that you do not want to mock explicitly. Otherwise, please use @InjectMock and @Mock, or just mock your dependencies yourself.
No.
Before using spring-auto-mock, take two minutes to think about your application's architecture: your design is probably not good if you really need to use spring-auto-mock.
Initially based on the work from Justin Ryan published DZone