Java and android testing methodology - java

I have a question about technology or methodology out there that I can use to test my code fast & simply. Recently I came across the difficulty & frustration regarding to testing my code when I was working on an android project. Everytime when i wanted to test my code, i had to re-compile whole project again and wait for emulator to re-install application which at least takes 40~50 seconds just to check a bit of code working fine. Are there any way that I can compile or test just a small portion of code / 1 ~ 2 methods working without having to re-compile whole project every time? Also which one is the latest and most widely used among the industries?
=====================================
Additional question. I've done some research on JUnit testing of java but is JUnit what i'm looking for? or is it different kind of testing technology

you can make a search about Robotium. it provides you to do blackbox testing.
http://testdroid.com/tech/54/automated-ui-testing-android-applications-robotium

Maybe Robolectric is what You are looking for. You can use JUnit to test only java code that doesn't use methods from android sdk.

have you considered using AndroidTestCase? JUnit can only be used to non-android specific function, but this does the job for your Android-specific code.

There is a very detailed account for android testing available at:
http://developer.android.com/tools/testing/testing_android.html
This includes basic as well as activity testing and is built on top of JUnit.

For people that don't want to use Roboticum and such, but just stick with Eclipse built-in JUnit testing, this is what I would recommend:
Have three projects:
AndroidProject
AndroidProject.test
AndroidProject.test.android
In your AndroidProject.test project you only test Models, Controllers and such which exclusively uses Java-libraries. So no Android Logcats, Toasts, or things like Patterns.WEB_URL.matcher(url).matches()) [android.util.Patterns] (which validates a String URL).
In your AndroidProject.test.android project you test the Activities, Services, Model-classes that use Android libraries like the Pattern-validation mentioned above, etc.
To be able to use Eclipse Run As -> JUnit Test for your JUnit test project you have to do some set-up however, like removing the Android API from each individual Test class, using the junit.framework.Assert and org.junit.Test imports, etc. For a full step-by-step guide to reproduce what I have done to make it work, I refer to my own Stackoverflow-post that I've made:
JUnit Test Android classes without being forced to start the (slow) Emulator.

Related

Why is Espresso's registerIdlingResources deprecated, and what replaces it?

I'm working on UI tests with Espresso for Android, and following the Google's code sample it is used, even though it's deprecated. So my questions are:
why is it deprecated?
what replaces it?
Since the example has not been updated, if you are using Espresso 3.0< instead of registerIdlingResources
Espresso.registerIdlingResources(mIdlingResource);
you should use IdlingRegistry:
IdlingRegistry.getInstance().register(mIdlingResource);
I'm working on unit tests with Espresso
I hope you mean UI tests ;)
why is it deprecated?
Some apps use build flavors in Gradle or a dependency injection framework, like Dagger, to generate test build configurations that register idling resources. Others simply expose the idling resource through their activities. The problem with all these approaches is that they add complexity to your development workflow, and some of them even break encapsulation.
what replaces it?
Now you need to use IdlingRegistry API
Source: Android Testing Support Library 1.0

Java Application Debugging Using Eclipse Scrapbook

I have a Java application with several (10) packages and almost 50 classes. My task is to understand the application (with poor documentation and no test cases). The application seem to have several bugs that I need to correct. So, I have started reverse engineering it and developing test cases based on the understanding. At times, I am using the test cases to understand the application itself. In this process , I am using JUnit test suit. I am also using the Eclipse Scrapbook, which to me is a very handy tool. I am testing small snippets of code using the scrapbook. As there are several packages and classes, I want to preserve all the small tests that I am doing using the scrapbook. My question is regarding the organization of the scrapbook(s) for large applications and the common practice. Is it a common practice to create separate scrapbooks (like Junit test case for each class and methods) for each package ? I am looking for guidelines that is followed in industry.

How to run play framework unit test in eclipse

Currently i am tring to develop web based application using play framework in java. And i would like to write unit test and run it through Elcipse. I tried but class not found exception appeared. How can i achive that?Could anyone help please? i am using play framework 2.0
If you want to unit test your code that calls out to the framework then you can do so by using an object mocking library such as Mockito. You should refactor your code to isolate the touch points with the framework as much as possible into separate classes. Then you can mock the framework objects and test your code.
You can do this with the ScalaTest plug-in for ecplise. You can find it here along with the update links for your version of Eclipse. They also mention it on scalatest.org but none of their update links are still valid.

Within Eclipse, access Android Project classes from a Java Project

How can I execute a block of code in an android project without having to launch the emulator.
I don't want to deal with the android lifcycle -> I want to test the logic of my app ONLY.
What I have in mind is to create another java project and define dependencies so that I can access the classes that are in my android project.
I would like to launch a kind of Main() method to bypass the emulator... Is it possible?
Thanks!
Look into unit testing. Fits your problem almost perfectly.
Not exactly for a block of code but for methods.
There are special launchers such as JUnit that makes the test-running smother and much more fun. You get green and red lights for every test, great satisfaction to get all green.
JUnit is fairly easy to get started with and is well integrated into eclipse.

Speed up compilation with mockito on Android

I am currently developing an android app in eclipse using:
One project for the app
One project for the tests (Instrumentation and Pojo tests)
In the test project, I am importing the mockito library for standard POJO testing.
However, when I import the library, the compilation time skyrockets from 1 second to about 30 seconds in eclipse. The cause seems to be that the whole library is converted each time. So basically, each time a make a modification that I want to test, I have to wait 30 seconds.
The only workarounds that I have found so far would be:
Disable "Build Automatically"
Create a project that includes only pojo tests and put mockito only there.
Use another library that compiles faster (e.g. easymock)
Any other suggestion?
Do you need the test project to be an android project?
If can get aware with creating a Java project and mocking out any of the android specific classes for the tests that would be my suggestion.
Have a look at this article:
https://sites.google.com/site/androiddevtesting/

Categories