last day (15 days later) » 

7:13 PM
2
A: Getting context in AndroidTestCase or InstrumentationTestCase in Android Studio's Unit Test feature

Jared BurrowsUpdated - Newer Examples: I got these working without deploying to a device. Put the tests in the /src/main/test/ folder. Here are newer examples, I took your examples and tested them in my own temporary test project. I ran the tests via command line: ./gradlew clean test. Please read more here...

 
Thanks for the answer, unfortunately all the linked questions refer to the 'old' way of testing when tests ran on a device on not the new unit test feature of Android Studio where tests are solely run within the IDE. My tests all worked fine (could access Context) before the update to 1.1.0 so these posts are outdated.
 
You didnt even try them?
@Ascorbin I have updated my answer using your examples in your question and I go them to work.
 
First of all, thanks for all your effort. You got those tests that require your Resources running with the Unit Test feature, without deploying to a device? Do you have them in the test folder or the androidTest folder?
 
Yes I got these working without deploying to a device. Put the tests in the /src/main/test/ folder.
@Ascorbin Can you please try my testing setup?
 
I have followed and tried them all in all kinds of combinations, to no avail. Will try again on my own pc later.
 
7:13 PM
@Ascorbin you need to use the information in my build.gradle. Can I see yours?
 
I can post it tomorrow, but I used the same target sdk, same gradle plugin version, same testCompile of Junit etc. and used the recommended fix for the 'method not mocked' error.
 
@Ascorbin You do not need to upgrade everything. You need to add in the "mocked" unit test: testOptions { unitTests { returnDefaultValues = true } }. Also the bounty runs out in 2 days.
 
Yeah, I did that. As I said, Ill try on another pc & AS installation.
 
@Ascorbin Alright, let me know and post your build.gradle files.
 
Edited in my build files, still no luck with a fresh project at another pc.
 
7:13 PM
@Ascorbin Which tests are not working? Are you running via IDE or the command line?
 
Tried all of your examples, from the IDE & command line, I couldn't get my resources with any. This is driving me nuts ...
 
@Ascorbin What about the other tests? What is the error you are seeing now? I having this working for me and published my exact source code.
 
That baffles me. Tests that do not need a Context all work fine. The error is that every method to retrieve a Context return null, except for new MockContext() which of course provides a Context but no resources. Even InstrumentationTestCase getInstumentation() returns null (de.tinypic.com/r/11vilox/8).
 
I feel we are close to solving this. Only 17 hours left.
@Ascorbin Could you post that photo in your question? I cannot view it directly.
 
Done, two pics in question.
 
7:13 PM
@Ascorbin Thanks for the fast reply. Nice screenshots. Have you read the "assertion error" in the HTML? Also, why are not you using MockContext?
 
Oh lord, I failed at blackening the project name. Hope no ones offended ;) The assertion error can be seen in the screenshots via the line number. MockContext gives me a context, but no Resources.
 
@Ascorbin That's fine. It's just a test project. Did you see my previous comment?
 
Yes I did, edited my comment.
If you have a test project where you successfully accessed the recourses, perhaps you can zip it and upload it somewhere so I can see if it also works for me?
 
Ah,
Give me a second haha
 
7:30 PM
This time I am on a different computer lol
I have the project and I am building soon, I need the sdks again, mine is out dated
Ok you there?
I just re-wrote the project
@Ascorbin hey
@Ascorbin wikisend.com/download/284136/…. griddle clean test or in Android Studio, File -> "Import Project" -> Click on the build.gradle.
 
7:55 PM
I have to go now.
I really want that Bounty.
 
AndroidTestCaseTest: assertNotNull(context) working, assertNotNull(context.getResources()) not working :(
ActivityTestCaseTest: not run at all, doesn't even show up in the html test report, doesn't fail with assertEquals(false, true)
InstrumentationTestCaseTest: no resources, fails at assertNotNull(resources);
I even reinstalled AS & the Android SDK, no effect.
 

  last day (15 days later) »