| Howto test a service with JUnit [message #1112976] |
Fri, 20 September 2013 09:09  |
 |
Thomas Wilhelm Messages: 13 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 09:15] Report message to a moderator
|
|
|
|
|
|
|
|
|
| Re: Howto test a service with JUnit [message #1115511 is a reply to message #1114720] |
Tue, 24 September 2013 03:45  |
Erdal Karaca Messages: 662 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:40Ok 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
|
|
|
Powered by
FUDForum. Page generated in 0.02153 seconds