Hi Andrew,
Is it good idea to create constructor of Android Component classes ?
I have read somewhere that we should never create constructors for Android
Component classes.
Regards,
Yuvi
On Wednesday, March 26, 2014 11:48:31 PM UTC+5:30, Andrew Mackenzie wrote:
>
> I think with your current code, getInstance() will return null in your
> test.
> You might want to check that....
>
> Well, the quickest hackyest thing that works fine and will get you running
> is:
> - add a setter to your service class that overrides the assignment to mBar
> that is done on construction.
> Depending on how you structure you packages for test, you can leave it
> package default scope and not publicly expose it beyond the test code.
>
> If you want to spend more time, then you might want to look at Dagger and
> have it @Inject the Bar into the service when the system Constructs it for
> you, and then you override the Injection during the test with a Test Dagger
> Module that injects the Mock.
>
> Andrew
>
> On Monday, 24 March 2014 05:37:13 UTC+1, 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.