pchudzik / springmock

alternative spring mocking infrastructure
https://blog.pchudzik.com/201707/springmock-v1/
MIT License
22 stars 4 forks source link
mock mocking mockito spock spring spring-boot spring-test

springmock

Build Status Coverage Status

Introduction

Alternative spring mocking infrastructure. With pluggable mocking library support. The purpose is to allow you to easily inject mocks created by any mocking library into your spring tests. Currently, mockito and spock mocks are supported.

Why? Spring boot supports only mockito as mocks provider which is great if you write tests in java. When using spock you might want to use mocks created by spock because of syntactic sugar they offer. It is similar to @MockBean any @SpyBean from spring-boot-test, but allows you to use mocks created by a library of your choice.

Contents

Features

Requirements

Basic requirements to make it running:

Required spring modules: spring-context & spring-test

Mockito

mockito-core <= 1.10.x is required and must be provided. Samples with spring-boot

Spock

To get spock mocks running you'll need:

Samples with spring-boot

Installation

Releases

With Mockito as mocks provider

mvn

Maven Central

<dependency>
  <groupId>com.pchudzik.springmock</groupId>
  <artifactId>springmock-mockito</artifactId>
  <version>1.2.0</version>
</dependency>
gradle
testCompile('com.pchudzik.springmock:springmock-mockito:1.2.0')

With Spock as mock provider

mvn

Maven Central

<dependency>
  <groupId>com.pchudzik.springmock</groupId>
  <artifactId>springmock-spock</artifactId>
  <version>1.2.0</version>
</dependency>
gradle
testCompile('com.pchudzik.springmock:springmock-spock:1.2.0')

sample pom.xml with spring-boot

sample build.gradle

Snapshots

Repository configuration

Add sonatype snapshots repository to repositories list

Maven:

<repositories>
  <repository>
    <id>sonatype-snapshots</id>
    <url>https://oss.sonatype.org/content/repositories/snapshots</url>
  </repository>
</repositories>

Gradle:

repositories {
  maven {
    url 'https://oss.sonatype.org/content/repositories/snapshots'
  }
  mavenCentral()
}

Mockito as mocks provider

Include mvn dependency:

<dependency>
  <groupId>com.pchudzik.springmock</groupId>
  <artifactId>springmock-mockito</artifactId>
  <version>1.2.1-SNAPSHOT</version>
</dependency>

Or gradle dependency:

testCompile('com.pchudzik.springmock:springmock-mockito:1.2.1-SNAPSHOT')

Spock as mocks provider

Include mvn dependency:

<dependency>
  <groupId>com.pchudzik.springmock</groupId>
  <artifactId>springmock-spock</artifactId>
  <version>1.2.1-SNAPSHOT</version>
</dependency>

Or gradle dependency:

testCompile('com.pchudzik.springmock:springmock-spock:1.2.1-SNAPSHOT')

Usage

Mock injection infrastructure is the same and is independent to selected mock provider.

Once mock is injected you can use it accordingly to selected mocking library for mockito and for spock

Mocks

To inject mocks just annotate field you want to be initialized and injected with mock using @AutowiredMock and you are good to go

@AutowiredMock AnyService anyService;

@Test
public void should_inject_mock() {
  assertTrue(mockingDetails(anyService).isMock());
}

or in spock:

@AutowiredMock AService service

def "should inject mock"() {
  given: service.hello() >> "mock"
  when:  final result = service.hello()
  then:  result == "mock"
}

You can specify name of the registered mock using name param. Which will result in registering mock with the specific name in the spring context (like in with @Bean#name). If name is not provided bean name will be the same ase the field name on which it is declared.

You can also provide list of aliases for mock using alias attribute.

Sample test case with mockito mocks

Sample test case with spock mocks

Spies

Right now springmock can not create spies on the fly and inject them in the context. In order to spy on the bean it must already be registered in the context. It needs appropriate object to already exist in spring context.

@AutowiredSpy Service service;

@Test
public void should_inject_spy() {
  assertTrue(mockingDetails(service).isSpy());
}

or in spock:

@AutowiredSpy Service service

def "should inject spy"() {
  when: service.hello()
  then: 1 * service.hello() >> "spy!"
}

You can specify name of the bean which should be replaced by created spy using name attribute. if no name is defined then destination bean will be matched field name or by class.

Configuration

Mockito

Annotate @AutowiredMock or @AutowiredSpy field with @MockitoDouble. Examples:

Spock

Annotate @AutowiredMock or @AutowiredSpy field with @SpockDouble. Examples:

Problems

Please report any problems with the library using Github issues. I'd really appreciate failing test case included in issue description or as PR.

Development

Versions

Setting version is done using script.mvnw/update-versions.groovy. From this script versions are loaded from version.properties and applied on all required projects (root + samples).

To apply versions from property file execute:

./mvnw -P versions-update gplus:execute -N

Deployment

Private key must be imported into pgp.

In order to do the release, release versions must be set in poms and one must execute goal:

SONATYPE_USERNAME=secret_user \
SONATYPE_PASSWORD=secret_password  \
./mvnw --settings .mvn/settings.xml -P release clean deploy

Changelog

1.2.0 springmock-spock - 2018.12.03

1.2.0 springmock-mockito - 2018.12.03

1.1.2 springmock-spock - 2017.09.17

1.1.2 springmock-mockito - 2017.09.17

1.1.0 springmock-mockito - 2017.09.06

1.1.0 springmock-spock - 2017.09.06

1.0.0 - 2017.07.22