Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add regression test that makers sure constructor injection is used when possible Fixes #2602
- Loading branch information
Showing
3 changed files
with
99 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 0 additions & 55 deletions
55
src/test/java/org/mockito/internal/configuration/InjectingAnnotationEngineTest.java
This file was deleted.
Oops, something went wrong.
98 changes: 98 additions & 0 deletions
98
subprojects/junit-jupiter/src/test/java/org/mockitousage/InjectMocksTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Copyright (c) 2018 Mockito contributors | ||
* This program is made available under the terms of the MIT License. | ||
*/ | ||
package org.mockitousage; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class ToBeMockedInTestSuperClass{ | ||
int identifier; | ||
|
||
public ToBeMockedInTestSuperClass(int identifier) { | ||
this.identifier = identifier; | ||
} | ||
} | ||
|
||
class ToBeMocked{ | ||
int identifier; | ||
|
||
public ToBeMocked(int identifier) { | ||
this.identifier = identifier; | ||
} | ||
} | ||
|
||
class TestClassToBeInitiatedViaConstructorInSuperClass { | ||
ToBeMockedInTestSuperClass toBeMockedInTestSuperClass; | ||
|
||
public TestClassToBeInitiatedViaConstructorInSuperClass(ToBeMockedInTestSuperClass toBeMockedInTestSuperClass) { | ||
assert toBeMockedInTestSuperClass != null; | ||
this.toBeMockedInTestSuperClass = new ToBeMockedInTestSuperClass(42); | ||
} | ||
} | ||
|
||
class TestClassToBeInitiatedViaConstructor{ | ||
ToBeMockedInTestSuperClass toBeMockedInTestSuperClass; | ||
ToBeMocked toBeMocked; | ||
|
||
public TestClassToBeInitiatedViaConstructor(ToBeMocked toBeMocked, ToBeMockedInTestSuperClass toBeMockedInTestSuperClass) { | ||
assert toBeMocked != null; | ||
assert toBeMockedInTestSuperClass != null; | ||
this.toBeMocked = new ToBeMocked(42); | ||
this.toBeMockedInTestSuperClass = new ToBeMockedInTestSuperClass(42); | ||
} | ||
} | ||
|
||
class SuperTestClass { | ||
@Mock | ||
ToBeMockedInTestSuperClass toBeMockedInTestSuperClass; | ||
|
||
@InjectMocks | ||
TestClassToBeInitiatedViaConstructorInSuperClass testClassToBeInitiatedViaConstructorInSuperClass; | ||
|
||
} | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class InjectMocksTest extends SuperTestClass { | ||
|
||
@Mock | ||
ToBeMocked toBeMocked; | ||
|
||
@InjectMocks | ||
TestClassToBeInitiatedViaConstructor testClassToBeInitiatedViaConstructor; | ||
|
||
/** | ||
* Checks that {@link #testClassToBeInitiatedViaConstructor} holds instances that have identifier 42. | ||
* It being 42 is proof that constructor injection was used over field injection. | ||
*/ | ||
@Test | ||
void given_instanceToBeInitializedByMockito_when_mocksRequestedByConstructorAreInTestAndSuperClass_should_useConstructorInjection() { | ||
assertThat(testClassToBeInitiatedViaConstructor) | ||
.extracting( | ||
testInstance-> testInstance.toBeMocked.identifier, | ||
testInstance-> testInstance.toBeMockedInTestSuperClass.identifier | ||
) | ||
.containsExactly( | ||
42, | ||
42 | ||
); | ||
} | ||
|
||
/** | ||
* Checks that {@link #testClassToBeInitiatedViaConstructorInSuperClass} holds instances that have identifier 42. | ||
* It being 42 is proof that constructor injection was used over field injection. | ||
*/ | ||
@Test | ||
public void given_instanceInSuperClassToBeInitializedByMockito_when_mocksRequestedAreInSuperClass_should_useConstructorInjection(){ | ||
assertThat(testClassToBeInitiatedViaConstructorInSuperClass) | ||
.extracting(yetAnotherClas1 -> yetAnotherClas1.toBeMockedInTestSuperClass.identifier) | ||
.isEqualTo(42); | ||
} | ||
|
||
} |