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
BedrockMission!

Learn More

View all

Sign in to view all badges

Junit5 Mockito for Acitvate method

Avatar

Avatar
Ignite 1
Level 1
baw_gov
Level 1

Likes

0 likes

Total Posts

11 posts

Correct Reply

0 solutions
Top badges earned
Ignite 1
Give Back
View profile

Avatar
Ignite 1
Level 1
baw_gov
Level 1

Likes

0 likes

Total Posts

11 posts

Correct Reply

0 solutions
Top badges earned
Ignite 1
Give Back
View profile
baw_gov
Level 1

23-03-2021

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.

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Affirm 3
Level 2
koenve
Level 2

Likes

13 likes

Total Posts

15 posts

Correct Reply

3 solutions
Top badges earned
Affirm 3
Establish
Validate 1
Give Back
Boost 5
View profile

Avatar
Affirm 3
Level 2
koenve
Level 2

Likes

13 likes

Total Posts

15 posts

Correct Reply

3 solutions
Top badges earned
Affirm 3
Establish
Validate 1
Give Back
Boost 5
View profile
koenve
Level 2

23-03-2021

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());
}

 

 

Answers (1)

Answers (1)

Avatar

Avatar
Coach
Employee
Jörg_Hoh
Employee

Likes

1,112 likes

Total Posts

3,145 posts

Correct Reply

1,072 solutions
Top badges earned
Coach
Give back 600
Ignite 5
Ignite 3
Ignite 1
View profile

Avatar
Coach
Employee
Jörg_Hoh
Employee

Likes

1,112 likes

Total Posts

3,145 posts

Correct Reply

1,072 solutions
Top badges earned
Coach
Give back 600
Ignite 5
Ignite 3
Ignite 1
View profile
Jörg_Hoh
Employee

25-03-2021

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/