If you just want to mock your map and inject it in your class under test, you should just create a mock map and inject it via ReflectionTestUtils
:
Class under test:
@Component
public class MyService {
@Value("#{${patientTypes}}")
private Map<String, Integer> patientTypes;
public Integer getPatientTypeByKey(String key) {
return patientTypes.get(key);
}
}
For Mockito-test you can use just InjectMocks
:
@ExtendWith(MockitoExtension.class)
public class SimpleTest {
@InjectMocks
private MyService underTest;
@Mock
private Map<String, Integer> mockMap;
@Test
public void test() {
when(mockMap.get(anyString())).thenReturn(15);
Integer result = underTest.getPatientTypeByKey("some key");
assertEquals(15, result);
}
}
For SpringBootTest ReflectionTestUtils
can be used:
@SpringBootTest
public class SBTest {
@Autowired
private MyService underTest;
@Mock
private Map<String, Integer> mockMap;
@Test
public void test() {
ReflectionTestUtils.setField(underTest, "patientTypes", mockMap);
when(mockMap.get(anyString())).thenReturn(15);
Integer result = underTest.getPatientTypeByKey("some key");
assertEquals(15, result);
}
}