Skip to content
This repository has been archived by the owner on May 11, 2021. It is now read-only.

How to test it

mchmielarz edited this page Feb 3, 2015 · 15 revisions

How to test it

If your application starts using our libs then for sure you will ask a question - how on earth can I test it? Don't worry - we come to the rescue.

Micro Infra Spring Test module

The idea of the libraries that we create is simplicity. We want you to do the minimal work possible. That's why we provide micro-infra-spring-test module that relies on micro-deps-spring-test-config module which is part of the micro-deps-spring-config project.

It provides base classes that you can extend both for your JUnit and Spock tests. It sets up basic Spring stuff with proper profiles (we assume that when doing integration tests we use the 'test' profile) and configurations with WireMock as a bean. It also gives you an easy way to stub interactions of that WireMock instance.

In case you'd like to verify interactions with your collaborators you can use one of the base classes: MvcWiremockIntegrationSpec for tests in Spock and MvcWiremockIntegrationTest for tests in JUnit. Using stubOf(collaboratorName) it is possible to retrieve a stub of collaborator with provided name. Names have to be the same as provided in microservice configuration otherwise an exception is thrown. With stub in a hand you can verify what interactions took place in your tests using verifyThat() methods. Check 4th step in Examples section below.

Consumer Driven Contracts

First thing to remember is that micro-infra-spring follows the Consumer Driven Contracts approach. Meaning that it will search for stubs in the provided repository (for more information on the properties check out Stub Runner Spring wiki.

Scared already? Don't be! It's all totally transparent for you! Just provide a couple of properties and you're ready to go.

Examples

It takes 4 simple steps to make your life better:

1. Add dependencies

Add micro-infra-spring to your classpath with micro-infra-spring-test module too:

compile 'com.ofg:micro-infra-spring:0.8.6'

testCompile 'com.ofg:micro-infra-spring-test:0.8.6'

Of course you need to have your microservice descriptor present on the classpath (e.g. microservice.json).

2. Set up properties

Set up properties that will define where your stub definitions lay in a JAR containing repository (e.g. Nexus):

# stubRepositoryRoot root URL from where the JAR with stub mappings will be downloaded
stubrunner.stubs.repository.root=http://nexus.4finance.net/content/repositories/Pipeline

# stubsGroup group name of the dependency containing stub mappings
stubrunner.stubs.group=com.ofg

# stubsModule module name of the dependency containing stub mappings
stubrunner.stubs.module=stub-definitions

3. Write your tests - enjoy!

It's enough to just write your test and extend our base test classes:

Spock with Spring Boot based application:

package com.ofg.base

import com.ofg.infrastructure.base.MvcWiremockIntegrationSpec
// Some @EnableAutoConfiguration annotated class
import com.ofg.microservice.Application
import org.springframework.boot.test.SpringApplicationContextLoader
import org.springframework.test.context.ContextConfiguration

@ContextConfiguration(classes = [Application], loader = SpringApplicationContextLoader)
class AcceptanceSpec extends MvcWiremockIntegrationSpec {
   // Your stuff
}

JUnit example of Spring Boot based application:

package com.ofg.base

import com.ofg.infrastructure.base.MvcWiremockIntegrationTest
// Some @EnableAutoConfiguration annotated class
import com.ofg.microservice.Application
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner
import org.springframework.boot.test.SpringApplicationConfiguration
import org.springframework.boot.test.IntegrationTest

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = { Application })
@IntegrationTest
class AcceptanceTest extends MvcWiremockIntegrationTest {
   // Your stuff
}

4. Stubs verification

@Test
private void should_test_behaviour_of_my_service() {
   ...

   stubOf("my/ping/collaborator").verifyThat(expectedRequest())
}

private RequestPatternBuilder expectedRequest() {
   return ...
}