Automatic detection and mocking of Spring IO components.
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.
The extension @MockPropertySources
adds a ProtocolResolver
to Spring context resolving properties file as empty ones.
The extension @MockWebMvc
sets up a MockMvc
.
This MockMvc
instance is either wired on :
- the
org.springframework.web.context.WebApplicationContext
if the current context is of such type - the
@Controller
annotated beans otherwise
The extension @MockJdbc
- modifies
javax.sql.DataSource
beans by making them point to a dedicated H2 in-memory database. - adds a
DataSourceResetter
to truncate all tables after each test
The extension @MockJms
- replace all
javax.jms.ConnectionFactory
beans by mockrunner-jmsMockConnectionFactory
ones - for each
javax.jms.ConnectionFactory
beans adds aJmsMock
with the same qualifiers for simplified JMS operations usage - adds a
DestinationManagerResetter
to remove messages from all queues after each test - if available, wraps the
ErrorHandler
ofJmsListenerContainerFactory
to access errors from matchingJmsMock
The extension @MockMicrometerGraphite
replaces the default GraphiteReporter
by one baked by GraphiteMock
which can be injected like any other bean.
The extension @MockAmqp
replaces all org.springframework.amqp.rabbit.connection.ConnectionFactory
beans by rabbitmq-mock ones.
The extension @RegisterTools
registers a BeanLocator
to easily access beans by partial name.
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).
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
}
}
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
}
}
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>
Add the following dependency to your build.gradle
repositories {
mavenCentral()
}
// ...
dependencies {
// ...
testCompile('com.github.fridujo:spring-automocker:1.1.0')
// ...
}
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.
Core and samples can be installed in a local Maven Repository for usage in other projects via the following command.
mvn clean install