Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Howto test a service with JUnit
Howto test a service with JUnit [message #1112976] Fri, 20 September 2013 13:09 Go to next message
Thomas Wilhelm is currently offline Thomas WilhelmFriend
Messages: 28
Registered: September 2013
Junior Member
Hey all,

i have two plugins, plugin A containing the interface MyService.java and plugin B containing MyServiceImpl.java. Now i want to write a JUnit test which tests a method of MyService (The testclass is within a fragment). How can i get an instance of MyService? Because i do not exporte the package of MyServiceImpl i cannot call "new MyServiceImpl()". I tried to use DI (@Inject) but it doesn´t work... How can i get my service to test it?

Thanks all.
Thomas

[Updated on: Fri, 20 September 2013 13:15]

Report message to a moderator

Re: Howto test a service with JUnit [message #1112989 is a reply to message #1112976] Fri, 20 September 2013 13:33 Go to previous messageGo to next message
Phill Perryman is currently offline Phill PerrymanFriend
Messages: 214
Registered: July 2009
Senior Member
You can create a Factory class in plugin B which contains a static method returning a MyService by creating a MyServiceImp within the factory. You can then expose the factory class. If this factory retains a static reference you can also add proxy methods to the factory to test non public api within MyServiceImpl if needed. If you call the class something like JUnitMyServiceFactory it should be pretty obvious what it is used for.
Re: Howto test a service with JUnit [message #1112994 is a reply to message #1112989] Fri, 20 September 2013 13:42 Go to previous messageGo to next message
Thomas Wilhelm is currently offline Thomas WilhelmFriend
Messages: 28
Registered: September 2013
Junior Member
Hey Phill,

thanks for you reply, but doesnt this approach break the rule "Never mix Test and Working code" ? If i get you, i have to create a factory in my application code only for test purposes.
I think there must be a simple way to retrieve my registered service in my test class without having extra code.

Anyway thanks alot for your idea.

Thomas
Re: Howto test a service with JUnit [message #1113050 is a reply to message #1112994] Fri, 20 September 2013 15:13 Go to previous messageGo to next message
Phill Perryman is currently offline Phill PerrymanFriend
Messages: 214
Registered: July 2009
Senior Member
You don't have to mix the code. In the project properties source tab you can add say a test folder. If you create the same package name in the test folder then the class test folder will have access to "default" methods in the src folder. I think it is referred to as parallel source trees.
Re: Howto test a service with JUnit [message #1113138 is a reply to message #1112976] Fri, 20 September 2013 17:47 Go to previous messageGo to next message
Erdal Karaca is currently offline Erdal KaracaFriend
Messages: 854
Registered: July 2009
Senior Member
Thomas Wilhelm wrote on Fri, 20 September 2013 15:09
Hey all,

i have two plugins, plugin A containing the interface MyService.java and plugin B containing MyServiceImpl.java. Now i want to write a JUnit test which tests a method of MyService (The testclass is within a fragment). How can i get an instance of MyService? Because i do not exporte the package of MyServiceImpl i cannot call "new MyServiceImpl()". I tried to use DI (@Inject) but it doesn´t work... How can i get my service to test it?

Thanks all.
Thomas


You can start a JUnit Plug-in Test and use the OSGi service registry to obtain an/the instance of your service.
Re: Howto test a service with JUnit [message #1114720 is a reply to message #1113138] Mon, 23 September 2013 05:40 Go to previous messageGo to next message
Thomas Wilhelm is currently offline Thomas WilhelmFriend
Messages: 28
Registered: September 2013
Junior Member
Ok here is my Code which returns null for the serviceReference and so throws an exception when calling bundleContext.getService(null). Can anybode post a piece of code which should work?

@Test
    public void testMyService() throws Exception
    {
        BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass())
                .getBundleContext();

        ServiceReference<?> serviceReference = bundleContext
                .getServiceReference(MyService.class);

        MyService service = (MyService) bundleContext
                .getService(serviceReference);
    }


Thanks a lot
Thomas
Re: Howto test a service with JUnit [message #1114762 is a reply to message #1112976] Mon, 23 September 2013 07:02 Go to previous messageGo to next message
Thomas Wilhelm is currently offline Thomas WilhelmFriend
Messages: 28
Registered: September 2013
Junior Member
Ok i´ve got it, i´ve made a newbie mistake... fragments can access classes contained in non exported packages of their host plugin. I just had the wrong host plugin because of some experiments before Sad Shame on me Wink
Thanks anyway
Thomas
Re: Howto test a service with JUnit [message #1115511 is a reply to message #1114720] Tue, 24 September 2013 07:45 Go to previous message
Erdal Karaca is currently offline Erdal KaracaFriend
Messages: 854
Registered: July 2009
Senior Member
Event if you managed to solve your problem: To use a service you have to make it available, first. I.e., you are missing the part that registers the service.
You can use DS (OSGi declarative services) or you can register the service in the startup of your bundle activator (of Plugin B as B provides the MyServiceImpl).

Thomas Wilhelm wrote on Mon, 23 September 2013 07:40
Ok here is my Code which returns null for the serviceReference and so throws an exception when calling bundleContext.getService(null). Can anybode post a piece of code which should work?

@Test
    public void testMyService() throws Exception
    {
        BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass())
                .getBundleContext();

        ServiceReference<?> serviceReference = bundleContext
                .getServiceReference(MyService.class);

        MyService service = (MyService) bundleContext
                .getService(serviceReference);
    }


Thanks a lot
Thomas

Previous Topic:How to keep track of toggle state of a Command
Next Topic:Right aligned toolbar in an e4 application
Goto Forum:
  


Current Time: Thu Apr 18 03:56:00 GMT 2024

Powered by FUDForum. Page generated in 0.02301 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top