Skip to content

Commit

Permalink
mockito#2921: test for multiple candidates
Browse files Browse the repository at this point in the history
  • Loading branch information
Jörg von Frantzius committed Mar 6, 2023
1 parent 93eee06 commit 58ecada
Showing 1 changed file with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -146,4 +147,49 @@ void testGenericSubclass() {
}
}

@Nested
public class MultipleCandidatesByTypeTest {
public class UnderTestWithMultipleCandidatesByType {
List<String> stringList;
}

@Mock
List<String> stringList1;

@Mock
List<String> 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<String> stringList;
}

@Mock
List<String> stringList;

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

}

0 comments on commit 58ecada

Please sign in to comment.