Android Testing of an Activity not run by JUnit - android

I'm trying to do Android unit testing for the first tme and I encounter a problem I can't seem to solve : only one of my test classes is ran, I'm not able to run test classes related to Activity testing, and even asserting true=false in them doesn't display an error.
My testing project is composed of three source files :
A test file for a class in my project (subclass of AndroidTestCase)
A test file for my first activity, LoginActivity (subclass of ActivityInstrumentationTestCase2)
A test file for another activity, EditUserActivity (once again subclass of ActivityInstrumentationTestCase2)
I used the following tutorial : http://forum.frandroid.com/topic/13831-traduc-de-tuto-les-tests-unitaires/ (in French but the code is in English)
And first read the following answer on StackOverflow : Trying to run Android JUnit tests in Eclipse fails? however it doesn't seems to be my problem
The code for the last test class is the following :
package com.imci.ica.test;
import com.imci.ica.EditUserActivity;
import android.test.ActivityInstrumentationTestCase2;
public class EditUserActivityTest extends
ActivityInstrumentationTestCase2<EditUserActivity> {
EditUserActivity mActivity;
public EditUserActivityTest() {
super("com.imci.ica", EditUserActivity.class);
}
#Override
protected void setUp() throws Exception {
super.setUp();
mActivity = this.getActivity();
}
public void testTest() {
assertEquals(true, false);
}
}
Thanks in advance for your help!

I don't understand why, but I had to move the Eclipse project's files, so I closed the project, moved them and imported the project back, and now all the tests are checked, so my problem's fixed. If it can help somebody...

For me, I found that one testing class was crashing. I forgot to added non-argument constructor. Fixing that, all tests are run.

Related

How can I mock and test this class?

The follows was the code which I want to test.
public class Demo {
private static final List<Pair<String, String>> mList;
static {
mList = new ArrayList<>();
mList.add(new Pair<>("F0", "T1"));
mList.add(new Pair<>("F1", "T2"));
mList.add(new Pair<>("F2", "T3"));
}
public String getStr(int pos) {
return mList.get(pos).first;
}
}
I was an android developer. I have get some trouble in test and mock the code.I have use mockito.
I have try some code to test it,but the result was not my expect.
1.First try
#Test
public void test(){
Demo demo=new Demo();
assertEquals(demo.getStr(0),"F0");
/**
* java.lang.AssertionError:
* Expected :null
* Actual :F0
*/
}
2.Second try
#Test
public void test() {
Demo demo = mock(Demo.class);
doCallRealMethod().when(demo).getStr(0);
assertEquals(demo.getStr(0), "F0");
/**
* java.lang.AssertionError:
* Expected :null
* Actual :F0
*/
}
Anyone tell me how can I resolve this problem to make demo.getStr(0) == "F0" by call the real method? Thanks!
===========================
Another question relate to it
I have try an another test to test android.util.Pair class, and the result is that "pair.first" was null,.(There are androidTest and test directory,I put it into test package.Did it impact the result?)
import android.util.Pair;
import org.junit.Test;
import org.mockito.Mockito;
import static org.junit.Assert.assertEquals;
public class DemoTest {
#Test
public void test1(){
Pair<String,String> pair=new Pair("First","Second");
assertEquals("First",pair.first);
//pair.first was null,why?
}
#Test
public void test2(){
Pair<String,String> pair= Mockito.spy(Pair.class);
assertEquals("First",pair.first);
//pair.first was null also,why?
}
}
Why the simple code is correct in real android environment,but failure in test?
I had the same problem too. month ago I have problem with TextUtils class too.
I report this to jUnit but they told me the problem is with android package because in unit test environment you don't have access to platform specific classes
for that pair case you can use this package. this works for me
import android.support.v4.util.Pair;
The problem in your first try is, that the public field "first" is actually null.
Is the Pair class the one from the "javafx.util" package or a custom implementation?
Did you forget "this.first = first" or something similar in the constructor of the "Pair" class?
I would also recommend to change the following line:
assertEquals(demo.getStr(0),"F0");
to
assertEquals("F0", demo.getStr(0));
so that the error is printed correctly.
Your second try does not make any sense. What is the point in mocking the class you want to test?
I think the second example has the same problem as the first one. Pair.first is never set. If you fix that, it should also work (untested).
From Google's Android tools website:
"Method ... not mocked."
The android.jar file that is used to run unit tests does not contain any actual code - that is provided by the Android system image on real devices. Instead, all methods throw exceptions (by default). This is to make sure your unit tests only test your code and do not depend on any particular behaviour of the Android platform (that you have not explicitly mocked e.g. using Mockito).
So how can we solve this?
In other words. If you need a default android class to work properly you either have to include it from a separate repository, or implement it yourself.
In the case of Android's Pair class. You can use android.support.v4.util.Pair instead.
To get access to this class, you can include com.android.support:support-compat:27.0.0 in your build.gradle or dependencies file.
If you are not using Gradle, you can copy the implementation of this file and use it in place of the official one. Or you can try and download the .jar file from this older version https://mvnrepository.com/artifact/com.google.android/support-v4/r7 (I have not tested whether it works)
Another approach (based on this) is to create the class in app/src/test/java/android/util/Pair.java and copy the code from the Android implementation.
This way you don't need extra dependencies. (There may be issues related to the implementation changing after you make the copy, but the dependencies may become stale as well.)

Eclipse JUnit works not properly

So i have a an android application project and unit test project to it. I am a beginner in Eclipse and maybe i don't understand properly how to work with JUnit.
I have written two simple test methods:
#Test
public void searchTest() {
fail("test is failed");
}
#Test
public void getTest() {
assertTrue(10 > 1);
}
So my problems are
Firstly: It is obvoius that searchTest must fail but when i run it doesn't happend :
Secondly: in some reason i can not debug my test methods. I click right button on serachTest in JUnit dialogue and select "Debug" but debugging is not starting.
Thirdly: Only one test method is showing in in JUnit dialogue however i have two methods with #Test annotation in my class. Why does it happend?
Could anyone help me? Thanks in advance
Think about what you had selected when you said to Run it as a Test. You need to select the class itself rather than a method within it. The JUnit View will only show you the tests that were run.
And you need to set a Breakpoint in your test code to have any observable difference from just Running the test.

Android UiAutomator Testing

I have created a simple activity in which I have 2 buttons for submitting and cancel. I just want to test this project by using UI Automator testing. So I create an android test project and make a class. I make this test class extend UiAutomatorTestCase. I have also added uiautomator.jar, android.jar as well as the junit3 library. But when I run the test case, it gives me an error about
TestSuiteConstruction failed and java.lang.RuntimeException.
However I add constructor but as it does not take any parameter, so I am unable to add any paramater. Here is my test case code. Please solve this error as soon as possible. Can you please tell me anything that I have not added in my project?
package com.example.automatorapp.test;
import android.test.suitebuilder.TestSuiteBuilder;
import android.util.Log;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;
import com.example.automatorapp.MainActivity;
public class testDemo1 extends UiAutomatorTestCase
{
public testDemo1()
{
}
public void testdemo() throws UiObjectNotFoundException
{
getUiDevice().pressHome();
Log.e("how r u","hello");
}
}
Try changing the name of your class so that is does not start with 'test' and remove the constructor. By convention, the name of a class starts with a Capital letter, yours starts with the lowercase 't'. Also, again by convention, JUnit3 test classes put the word 'Test' at the end. http://junit.sourceforge.net/junit3.8.1/javadoc/junit/framework/TestCase.html So in your example a class name of testdemo1 would be something line Demo1Test (remember the filename needs to match the classname).
However, and to my surprise, a slightly modified version of your code ran on my machine. Here's the code that ran OK.
package com.example.automatorapp.test;
import android.util.Log;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;
public class testDemo1 extends UiAutomatorTestCase
{
public testDemo1()
{
}
public void testdemo() throws UiObjectNotFoundException
{
getUiDevice().pressHome();
Log.e("how r u","hello");
}
}
I got the Log message in the Android log too.
E/how r u (24667): hello
Therefore, you may have some issues with your project setup or build environment. As far as I know this project (for testing) should be independent of the code or project for the app you want to test. However you have an import to the app you want to test.
import com.example.automatorapp.MainActivity;
As you've now posted quite a few questions related to UI Automator perhaps you could summarise your progress so far. For example, have you ever got a UI Automator test to run successfully?
PS: I hoped to see some feedback to answers to your earlier questions. Without your feedback it's hard to know which way you're heading and whether the answers were relevant or useful to you.

Possible to identify in setup which JUnit test case is being executed?

I have writing an Android JUnit test for an activity in my application. The Activity is modal, and can be configured via the launching intent.
I would like to write test methods to test the different modes. This would involve a setActivityIntent call in the setup method, configuring the Activity based on the test case we are running.
My question is, how can I determine which test case is about to be run from the setup method?
It turns out the answer to this question is very easy. junit.framework.TestCase has a getName method that returns the name of the current test case. Perfect.
In Junit 4.X, you can do the samething with TestName class:
public class MyTest {
#Rule public TestName name = new TestName();
#Before
public void before() {
System.out.println("running...", name.getMethodName());
}
}

android instrumentation testsuite

I have written two test cases in a package com.app.myapp.test
When I try to run them both of them are not getting executed, only one test case gets executed and stops.
I have written the following testsuite in the same package
AllTests.java
public class AllTests extends TestSuite {
public static Test suite() {
return new TestSuiteBuilder(AllTests.class).includePackages("./src/com.ni.mypaint.test","./src/com.ni.mpaint.test").build();
/* .includeAllPackagesUnderHere()
.build();*/
}
Is the code and location for this testsuite is correct?
Well, certainly leave off the '/src/' portion of the package listing for that invocation. Either way, the easiest and most flexible way to run your tests this is to make sure all your tests are in a subpackage of where AllTests is (e.g. com.app.myapp.test.tests) and use this for the suite:
public static Test suite() {
return new TestSuiteBuilder(AllTests.class)
.includeAllPackagesUnderHere().build();
}
Make sure your tests run individually, too, without the suite runner -- the suite won't pick up your tests if they're set up wrong to begin with.
(This is better than explicitly listing the package name since it's more portable -- you can rename your test package without breaking it, for example.)

Categories

Resources