Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Junit5 Mockito for Acitvate method

baw_gov
Level 2
Level 2

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.

1 Accepted Solution
koenve
Correct answer by
Level 3
Level 3

As pointed out by @Jörg_Hoh , 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());
}

 

 

View solution in original post

0 Replies
koenve
Correct answer by
Level 3
Level 3

As pointed out by @Jörg_Hoh , 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());
}

 

 

View solution in original post

baw_gov
Level 2
Level 2
Thank you for your prompt reply @koenve..I will try this method. Can you please also suggest me how to write setup method with the example you gave above?
kautuk_sahni
Community Manager
Community Manager
@koenve, good to see you assisting AEM community. Keep the great work going. Looking forward to AEM SME's like you more in the community. Thank you.
Jörg_Hoh
Employee
Employee

You can get that done much easier if you are using SlingMocks. I have written a blog post about exactly the case you have: https://cqdump.joerghoh.de/2019/01/09/writing-unit-tests-for-aem-using-slingmocks/

koenve
Level 3
Level 3
Nice, that's amazing. I'll check it out for myself too. Thanks!