Skip to content

Commit

Permalink
Only run injection once
Browse files Browse the repository at this point in the history
Add regression test that makers sure constructor injection is used when possible
Fixes #2602
  • Loading branch information
1-alex98 committed Jun 12, 2023
1 parent 5cccfda commit b4d89f9
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 67 deletions.
Expand Up @@ -45,25 +45,14 @@ public class InjectingAnnotationEngine implements AnnotationEngine {
public AutoCloseable process(Class<?> clazz, Object testInstance) {
List<AutoCloseable> closeables = new ArrayList<>();
closeables.addAll(processIndependentAnnotations(testInstance.getClass(), testInstance));
closeables.addAll(processInjectMocks(testInstance.getClass(), testInstance));
closeables.add(injectCloseableMocks(testInstance));
return () -> {
for (AutoCloseable closeable : closeables) {
closeable.close();
}
};
}

private List<AutoCloseable> processInjectMocks(
final Class<?> clazz, final Object testInstance) {
List<AutoCloseable> closeables = new ArrayList<>();
Class<?> classContext = clazz;
while (classContext != Object.class) {
closeables.add(injectCloseableMocks(testInstance));
classContext = classContext.getSuperclass();
}
return closeables;
}

private List<AutoCloseable> processIndependentAnnotations(
final Class<?> clazz, final Object testInstance) {
List<AutoCloseable> closeables = new ArrayList<>();
Expand Down

This file was deleted.

@@ -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);
}

}

0 comments on commit b4d89f9

Please sign in to comment.