I have configuration class:
@ConfigurationProperties(prefix = "myConfig")
public class MyConfig {
protected String config;
}
My service uses this config class (That gets the value from application.yml
):
@Service
public class myService {
@Inject
private MyConfig myConfig;
Public String getInfo (String param) {
if (isEmpty(param)) { throw new InvalidParameterException; }
return myConfig.getConfig();
}
}
I'm trying to test it with Mockito:
@RunWith(MockitoJUnitRunner.class)
public class myTest {
@InjectMocks
private MyService myService;
@Mock
private MyConfig myConfig;
@Test
public void myTest1() {
myService.getInfo("a");
}
@Test
public void myTest2() {
assertThrows(InvalidParameterException.class, ()->myService.getInfo(null));
}
}
myTest
fails since the config class is mocked, thus has null values.
What is the right way to test configuration class with Mockito?
Edit:
I have a number of config classes like the one above that are being used in myService
.