From 58ecada97ad5625035aa6ddf78f54e6a5ea01f28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20von=20Frantzius?= Date: Mon, 6 Mar 2023 13:46:40 +0100 Subject: [PATCH] #2921: test for multiple candidates --- .../org/mockitousage/GenericTypeMockTest.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/subprojects/junit-jupiter/src/test/java/org/mockitousage/GenericTypeMockTest.java b/subprojects/junit-jupiter/src/test/java/org/mockitousage/GenericTypeMockTest.java index f8ce5c01f3..8af851ff8c 100644 --- a/subprojects/junit-jupiter/src/test/java/org/mockitousage/GenericTypeMockTest.java +++ b/subprojects/junit-jupiter/src/test/java/org/mockitousage/GenericTypeMockTest.java @@ -7,6 +7,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; import java.sql.Time; import java.util.Collection; @@ -146,4 +147,49 @@ void testGenericSubclass() { } } + @Nested + public class MultipleCandidatesByTypeTest { + public class UnderTestWithMultipleCandidatesByType { + List stringList; + } + + @Mock + List stringList1; + + @Mock + List stringList2; + + @InjectMocks + UnderTestWithMultipleCandidatesByType underTestWithMultipleCandidates = new UnderTestWithMultipleCandidatesByType(); + + @Test + void testMultipleCandidatesByTypes() { + // verify that when mutiple mock candidates exist with same type (but not matching by field names), none will be injected + assertNull(underTestWithMultipleCandidates.stringList); + } + } + + @Nested + public class MultipleCandidatesOneByNameTest { + public class UnderTestWithMultipleCandidatesOneByName { + List stringList; + } + + @Mock + List stringList; + + @Mock + List stringListMock; + + @InjectMocks + UnderTestWithMultipleCandidatesOneByName underTestWithMultipleCandidatesOneByName = new UnderTestWithMultipleCandidatesOneByName(); + + @Test + void testMultipleCandidatesOneByName() { + // verify that when multiple mock candidates exist by type, and one of them matches by field name, that one is injected + assertNotNull(underTestWithMultipleCandidatesOneByName.stringList); + assertEquals(stringList, underTestWithMultipleCandidatesOneByName.stringList); + } + } + }