Skip to content

Commit

Permalink
Make MockitoExtension constructor public (mockito#1391)
Browse files Browse the repository at this point in the history
* Make MockitoExtension constructor public

It is possible to register an extension for automatic use using Java's `ServiceLoader` mechanism.

https://junit.org/junit5/docs/current/user-guide/#extensions-registration-automatic

Some projects may want to provide their own `META-INF` file to avoid tediously adding `MockitoExtension` to tests since it is very common. However, `ServiceLoader` requires the class to have a no-args *public* constructor, so the current extension cannot be used with the `ServiceLoader` mechanism.

* Add test
  • Loading branch information
anuraaga authored and TimvdLippe committed May 17, 2018
1 parent 758ca37 commit 3f96f3b
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 2 deletions.
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ include 'extTest'
include 'kotlinTest'
include 'android'
include 'junit-jupiter'
include 'junitJupiterExtensionTest'

rootProject.name = 'mockito'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ public class MockitoExtension implements TestInstancePostProcessor,BeforeEachCal

private final Strictness strictness;

// This constructor is invoked by JUnit Jupiter via reflection
// This constructor is invoked by JUnit Jupiter via reflection or ServiceLoader
@SuppressWarnings("unused")
private MockitoExtension() {
public MockitoExtension() {
this(Strictness.STRICT_STUBS);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apply from: "$rootDir/gradle/dependencies.gradle"

apply plugin: 'java'
description = "End-to-end tests for automatic registration of MockitoExtension."

sourceCompatibility = 1.8

dependencies {
testCompile project(":junit-jupiter")
testCompile libraries.assertj
testCompile libraries.junitPlatformLauncher
testCompile libraries.junitJupiterApi
testRuntime libraries.junitJupiterEngine
}

test {
useJUnitPlatform()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2018 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockitousage;


import java.util.List;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;

class NoExtendsTest {

@Mock
private List<String> mock;

@Test
void runs() {
when(mock.get(0)).thenReturn("foo");
assertThat(mock.get(0)).isEqualTo("foo");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.mockito.junit.jupiter.MockitoExtension
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
junit.jupiter.extensions.autodetection.enabled = true

0 comments on commit 3f96f3b

Please sign in to comment.