Junit5 Mockito for Acitvate method
I have an activate method in service implementation class that is using OSGI configuration. I am looking for how to write Junit5 Test Cases for Activate method.
I have an activate method in service implementation class that is using OSGI configuration. I am looking for how to write Junit5 Test Cases for Activate method.
As pointed out by @joerghoh , there's an even better way of doing it!
Create a map with the properties you want to pass to your service
Map<String,Object> parameters = new HashMap<>();
parameters.put("my_property","my property");You can then pass this to your context with the registerInjectActivateService method:
context.registerInjectActivateService(myService, parameters);
_____________________________________
Original answer:
What you can do is create a mock-implementation of your OSGI configuration interface.
private static class MockConfig implements MyService.Configuration {
public String my_property() {
return "my property";
}
public Class<? extends Annotation> annotationType() {
throw new NotImplementedException();
}
}
You can then pass this to your unit test, so you can test that whatever needs to happen, happens:
void test_activate() throws Exception{
myService.activate(new MockConfig());
assertEquals("my property", myService.getMyProperty());
}
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.