Spring-Automocker
Automatic detection and mocking of Spring IO components.
Motivation
Writing integration tests for Spring application is often writing the same glue code over and over again. Spring-Automocker was created to avoid re-writing the same boilerplate code and focus on test added value.
Mocking strategies
Property sources
The extension @MockPropertySources adds a ProtocolResolver to Spring context resolving properties file as empty ones.
MVC controllers
The extension @MockWebMvc sets up a MockMvc. This MockMvc instance is either wired on :
- the 
org.springframework.web.context.WebApplicationContextif the current context is of such type - the 
@Controllerannotated beans otherwise 
JDBC Data Sources
The extension @MockJdbc
- modifies 
javax.sql.DataSourcebeans by making them point to a dedicated H2 in-memory database. - adds a 
DataSourceResetterto truncate all tables after each test 
JMS Connection Factories
The extension @MockJms
- replace all 
javax.jms.ConnectionFactorybeans by mockrunner-jmsMockConnectionFactoryones - for each 
javax.jms.ConnectionFactorybeans adds aJmsMockwith the same qualifiers for simplified JMS operations usage - adds a 
DestinationManagerResetterto remove messages from all queues after each test - if available, wraps the 
ErrorHandlerofJmsListenerContainerFactoryto access errors from matchingJmsMock 
Micrometer Graphite Meter Registry
The extension @MockMicrometerGraphite replaces the default GraphiteReporter by one baked by GraphiteMock which can be injected like any other bean.
RabbitMQ Connection Factories
The extension @MockAmqp replaces all org.springframework.amqp.rabbit.connection.ConnectionFactory beans by rabbitmq-mock ones.
Utilities
The extension @RegisterTools registers a BeanLocator to easily access beans by partial name.
Example Use
As Spring-Automocker uses spring-test org.springframework.test.context.ContextCustomizerFactory extension mechanism, it is compatible with Spring >= 4.3 (so spring-boot >= 1.4).
Using JUnit 4
Use SpringJUnit4ClassRunner in conjuction with @Automocker
@Automocker
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MyApplication.class)
public class MyApplicationTest {
    @Autowired
    private MyService service;
	@Test
	public void my_test() {
		// test injected service
	}
} 
Using JUnit 5
Use @ExtendWith(SpringExtension.class) in conjuction with @Automocker
@Automocker
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = MyApplication.class)
public class MyApplicationTest {
    @Autowired
    private MyService service;
	@Test
	public void my_test() {
		// test injected service
	}
} 
Getting Started
Maven
Add the following dependency to your pom.xml
<dependency>
    <groupId>com.github.fridujo</groupId>
    <artifactId>spring-automocker</artifactId>
    <version>1.1.0</version>
    <scope>test</scope>
</dependency> 
Gradle
Add the following dependency to your build.gradle
repositories {
	mavenCentral()
}
// ...
dependencies {
	// ...
	testCompile('com.github.fridujo:spring-automocker:1.1.0')
	// ...
} 
Building from Source
You need JDK-8 to build Spring-Automocker. Core and samples can be built with Maven using the following command.
mvn clean package
 
All features can be tested through samples with Maven using the following command.
mvn clean test
 
Since Maven has incremental build support, you can usually omit executing the clean goal.
Installing in the Local Maven Repository
Core and samples can be installed in a local Maven Repository for usage in other projects via the following command.
mvn clean install