I am creating the unit test for some legacy android codes.
Currently i need to manually create the unit test for each class/method in test/java.
Is there an automated way to auto generate all the test stubs? ideally from the Android studio IDE? (similar to how parasoft can autogenerate empty test stubs for C++ classes and methods).
I am using robolectric on Android studio 1.2.1
Like this: Right-click the class name. Go To -> Test. Select the function, Done.
Related
I am a Beginner in Android. I want to write a unit testing code in Android studio. How to run that testing in real device and also how to get the test result. I can able to see AndroidTest folder in src folder.
It is a good practice to write Unit Test code from scratch. But if you have learned a little bit of Unit testing basic you can take advantage of IntelliJ plugins for Android Studio to generate Unit Testing code very easily.
Try this:
TestMe(https://plugins.jetbrains.com/plugin/9471-testme)
Auto Generate Unit Tests for source class in Java or Groovy.
No more boilerplate!
Features:
Auto-generate Java or Groovy test code with JUnit 4/5, TestNG or Spock frameworks
Auto-generate Mockito mocks
Generate test params and assertion statements
Quick-Start:
http://weirddev.com/testme/
I have been using android studio for some time.(still learning) and till date i haven't used the java files in the test folders. i.e the one's marked here :-
Is it necessary for a project or can we just delete those files?
If it is necessary, what is its use and how can we use it ? (any reference would be great).
If anyone could rephrase the question in a better way,it would be better.
Those are for tests. The androidTest is for instrumented tests while the test folder is for unit tests. You could delete them if you do not plan on using it, but it also does not harm your project.
See here Test Your App
I want to use Junit & Mockito for android framework java files for local unit testing.
I am not using android studio and want to write these unit testcases and execute them on Linux server.
I have used libraries Junit, Mockito , hamcrest-core jar files. Do I need to include any other jar files?
I am stuck at mocking the context.
#Mock Context context - Doesn't work at all
Can anyone please guide me on this?
On the same android sdk documentation web page:
http://developer.android.com/training/testing/start/index.html#config-local-tests
We can read the following:
"In your Android Studio project,
you must store the source files for local unit tests under a specific source directory
src/test/java"
Lines later:
"In your Android Studio project,
you must place the source code for your instrumentated tests under a specific directory
src/androidTest/java."
So, where are we supposed to put our test sources?
Either or both. They are for different types of tests. The first quote is from a section entitled "Configure Your Project for Local Unit Tests". The second quote is from a section entitled "Configure Your Project for Instrumented Tests". Those are not the same thing.
In a nutshell:
"Local unit tests" means "tests that run on the JVM of your development machine, mostly for testing POJOs, other non-Android-specific code, or Android code that you mock incessantly"
"Instrumented tests" (referred to previously as "instrumentation tests") means "tests that run on Android, and therefore can test code that depends heavily on Android"
This is covered in the Testing Concepts documentation.
I am writing an Android Library Project basing on Android Bitmap class (call it AndroindLib) which contains only utility class (no activity). I tried to test it using Android JUnit, but it keeps complaining that can't find the AnroidLib.apk
What's the right way to Unit test Android Library Project?
Quoting the documentation:
"There are two recommended ways of setting up testing on code and resources in a library project:
You can set up a test project that instruments an application project that depends on the library project. You can then add tests to the project for library-specific features.
You can set up a standard application project that depends on the library and put the instrumentation in that project. This lets you create a self-contained project that contains both the tests/instrumentations and the code to test."
In your test project simply change the package name so that it's the same as your library's package.
For example, you have a library whose package is "com.example.lib". Create a test project targeting your library. In the manifest file you'll see package="com.example.lib.test", and targetPackage="com.example.lib". Just change the package from "com.example.lib.test" to "com.example.lib" (targetPackage leave as is).
Also, make sure that the library is referenced to your test project NOT in Java build path, but as a usual Android library : in Eclipse it must be shown as library in Project->Properties->Android tab, but not in Project->Properties->Java Build Path tab.
Then run you tests.
Per the documentation:
Testing a library module is the same as testing an app.
The main difference is that the library and its dependencies are automatically included as dependencies of the test APK. This means that the test APK includes not only its own code, but also the library's AAR and all its dependencies. Because there is no separate "app under test," the androidTest task installs (and uninstalls) only the test APK.
When merging multiple manifest files, Gradle follows the default priority order and merges the library's manifest into the test APK's main manifest.
NOTE: This solution is based on using Eclipse Indigo (3.8.2) and might have to be implemented slightly differently for another IDE although the basic principles will be the same.
I had similar issues and I found that do the following always works:
(NOTE: These instructions are for building a new project group from scratch. If you have already built parts of the project group, then you may have to modify your projects so that they connect in the same way.)
Create a new Android Library project by checking the "Is Library" checkbox during creation. (for example an Android project named "RemingtonAndroidTools").
Build the Android Library project and verify that it created a jar file in the bin folder. (for example a jar file named "RemingtonAndroidTools.jar".)
Create an empty Android Project for testing the Android app that will serve as an Android Test App. (For example an Android project named "RemingtonAndroidToolsTestApp"). You will not need to modify the source code or resources of the Android Test App project unless you have something that must be added for testing. Many things can be tested without any modifications to the Android Test App Project. The Android Test App project is a bridge between your Android Library project and the Android Junit project that makes testing of the Android Library project via Android Junit possible.
Go the Library tab of Java Build Path for the Android Test App project ("RemingtonAndroidToolsTestApp" in this example).
Add the jar file ("RemingtonAndroidTools.jar" in this example) of the Android Library Project ("RemingtonAndroidTools" in this example) via the "Add Jars..." button.
Create a new Android Test project (for example "RemingtonAndroidToolsTester") that will serve as an Android Library Tester and select the Android Test App project ("RemingtonAndroidToolsTestApp" in this example) as the target.
Go the Library tab of Java Build Path for the Android Library Tester project ("RemingtonAndroidToolsTester" in this example).
Add the jar file ("RemingtonAndroidTools.jar" in this example) of the Android Library Project ("RemingtonAndroidTools" in this example) via the "Add Jars..." button.
Find the last folder of your Android package in the Android Library Tester project ("danny.remington.remington_android_tools_test_app.test" for example) and add a test class ("MainActivityTest" for example) that inherits from ActivityInstrumentationTestCase2.
Edit the test class ("TestActivityTest" in this example) to use the activity (for example "TestActivity") of the Android Test App ("RemingtonAndroidToolsTestApp" in this example) as the parameter for ActivityInstrumentationTestCase2.
Edit the test class ("TestActivityTest" in this example) and create a default constructor that makes a call to super(Class) and passing in the class of the Android Test App ("TestActivity.class" for example).
You should end up with three projects (Android Library, Android Test App, Android Library Tester) that look similar to this:
You should end up with a class for testing your Android Library that looks similar to this:
package danny.remington.remington_android_tools_test_app.test;
import android.test.ActivityInstrumentationTestCase2;
import danny.remington.remington_android_tools_test_app.TestActivity;
/**
*
*/
public class TestActivityTest extends
ActivityInstrumentationTestCase2<TestActivity> {
public TestActivityTest() {
super(TestActivity.class);
}
}
You can then add any test that you want. You will not need to reference the Android Test App ("RemingtonAndroidToolsTestApp" in this example) further to run your tests unless they require access to an Android specific component (like the Assets folder, for example). If you need to access any Android specific components you can do so by modifying the Android Test App ("RemingtonAndroidToolsTestApp" in this example) and then referencing it via the instrumentation provided by the standard Android Junit API. (You can read more about that here: http://developer.android.com/tools/testing/testing_android.html)
If your ulitiy classes do not depend on any android specific code, you can just use standard JUnit unit tests. No need to use the Android versions.