Howto test a service with JUnit [message #1112976] |
Fri, 20 September 2013 13: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
[Updated on: Fri, 20 September 2013 13:15] Report message to a moderator
|
|
|
|
Re: Howto test a service with JUnit [message #1112994 is a reply to message #1112989] |
Fri, 20 September 2013 13:42 |
|
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 #1114720 is a reply to message #1113138] |
Mon, 23 September 2013 05: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
|
|
|
Re: Howto test a service with JUnit [message #1114762 is a reply to message #1112976] |
Mon, 23 September 2013 07:02 |
|
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 Shame on me
Thanks anyway
Thomas
|
|
|
|
Powered by
FUDForum. Page generated in 0.02301 seconds