Hello, Technically the Android Service should support injection by giving you some "Dependency Resolver/Container" so you can pass the resolver/container from the framework you are working with. But this is not the case, in fact, the Android Developer Training don't recommend the use of Dependency Injections frameworks ( http://developer.android.com/training/articles/memory.html#DependencyInjection ).
Here we are facing the same problem. I think you could use the Android Annotations injection ( https://github.com/excilys/androidannotations/wiki/Enhance-custom-classes), but I don't know how you could use it on unit testing because android annotations resolve the dependencies in compile time. Another suggestion is the old fashion "unit test constructor". I don't know if this works, but you could give a try: create a constructor that receives all your dependencies (but you still need the default without parameters, so that Android could start your service) and instead of the traditional ServiceTestCase, just do a new FooService with your dependencies and call your doSomeThing method. Hope this help, Regards On Monday, March 24, 2014 1:37:13 AM UTC-3, Yuvi wrote: > > > Hi, > > I am facing issue while unit testing : > > // Service class that have to be tested. > > class FooService extends Service{ > > public static FooService sFooService; > > private Bar mBar = new Bar(); > //Other private objects > > @Override > protected void onCreate() > { > sFooService = this; > } > > public static FooService getInstance() > { > return sFooService; > } > > @Override > protected void onDestroy() > { > sFooService = null; > } > > public void doSomething() > { > //do Some stuff here > if(done) > { > mBar.perfomAction(true); > // Now this performAction method doing many stuffs using some > other classes > // that may have dependency and initialized from some else. Hence > throwing exceptions. > // Therefore need to mock Bar class. but how ?? > } > else > { > mBar.perfomAction(false); > } > }} > > > // Test Class > > class FooTest extends ServiceTestCase<FooService>{ > > protected void setUp() throws Exception > { > super.setUp(); > MockitoAnnotations.initMocks(this); > startService(new Intent(getContext(), FooService.class)); > > } > > protected void tearDown() throws Exception > { > super.tearDown(); > } > > public void testdoSomething() > { > Bar bar = mock(bar.class); > doThrow(new RuntimeException re).when(bar).performAction(true); > > //How to inject bar mocked object? > > assertNotNull(FooService.getInstance()); > > try > { > FooService.getInstance().doSomeThing(); > Assert.Fail("Runtime exception should be thrown"); > } > catch (RuntimeException re) > { > > } > }} > > > Now, here how can I inject bar mocked object which is created using > Mockito ? > > I have googled this, and found that some guys suggested to create getter > and setter for Bar class. Which I don't think is a valid solution, because > there could be number of private object, that will be visible to outside > FooService class. > > Regards, > > Yuvi > > -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en --- You received this message because you are subscribed to the Google Groups "Android Developers" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.

