Questions tagged [android-junit]

Android testing is based on JUnit. In general, a JUnit test is a method whose statements test a part of the application under test.

History:

For a history of JUnit, visit this Wikipedia page.

About Androids JUnit:

Android's build and test tools assume that test projects are organized into a standard structure of tests, test case classes, test packages, and test projects.

Android testing is based on JUnit. In general, a JUnit test is a method whose statements test a part of the application under test. You organize test methods into classes called test cases (or test suites). Each test is an isolated test of an individual module in the application under test. Each class is a container for related test methods, although it often provides helper methods as well.

In JUnit, you build one or more test source files into a class file. Similarly, in Android you use the SDK's build tools to build one or more test source files into class files in an Android test package. In JUnit, you use a test runner to execute test classes. In Android, you use test tools to load the test package and the application under test, and the tools then execute an Android-specific test runner.

Reference: https://developer.android.com/tools/testing/testing_android.html

Example Questions:

Documentation:

82 questions
1
vote
1 answer

Run vanilla JUnit tests on individual classes in an Android Eclipse JUnit project

I'm pulling my hair out trying to run a plain vanilla JUnit test on my Android project. I want to write a test for a POJO class that does not use any Android objects. In fact, I can't even get a test working that tests nothing! I've read Google's…
Jodes
  • 14,118
  • 26
  • 97
  • 156
0
votes
0 answers

Use custom test runners for android Unit Test

Is it possible to use a custom test runner for android unit test? (Or junit tests)? I have tried setting a custom test runner in my default config like testInstrumentationRunner = "ca.xyz.CustomTestRunner" But it doesn't work. I checked in the unit…
0
votes
0 answers

Error not loading dependencies in Android Studio Test

I have installed android studio and all the SDK needed, also connected my phone via USB in debugging mode all is good. Now I want to run automatic tests on a app from my phone using android studio but I have many loading errors. import static…
0
votes
1 answer

How to test the ViewModel using Junit, Mockito and Kotlin flows in Android

I tried to test the ViewModel using Junit and Mockito, getting null pointer exception. It returns cannot invoke flow collector. I want to test the responses coming in MpinDataKey, check all parameters are there in MpinDataKey, the api call…
0
votes
1 answer

Android: Mocking GoogleSignIn.getClient is resulting in Null Pointer Exception

Trying to mock static object GoogleSignIn.getClient() method but getting this error from the initialize() method: getClient(context, gso) must not be null java.lang.NullPointerException: getClient(context, gso) must not be null Here is the class…
rysv
  • 2,416
  • 7
  • 30
  • 48
0
votes
0 answers

Android Test Runner can find tests in a class but not in a package

Why would the Android Instrumentation Runner fail to find tests in a package? This is an old and quite large project, and it used to work but now it does not. I have tried creating a brand new test project, and it works fine. I do not know where to…
Andrew Shepherd
  • 44,254
  • 30
  • 139
  • 205
0
votes
1 answer

Android test cases robolectric Shadowlooper idle VS idleFor

using robolectric version 4.5.1 What is the difference between shadowOf(getMainLooper()).idleFor(1, TimeUnit.MILLISECONDS); and shadowOf(getMainLooper()).idle(); shadowOf(getMainLooper()).idle(); is causing my test case to fail giving following…
amodkanthe
  • 4,345
  • 6
  • 36
  • 77
0
votes
0 answers

how to test fragment navigations which is handled by the Activity

I want to test fragment navigations, which is handled by the MainActivity. for that I have created HomeFragmentTest class @MediumTest @RunWith(AndroidJUnit4::class) class HomeFragmenTest { @Rule @JvmField val mockitoRule: MockitoRule =…
Jemo Mgebrishvili
  • 5,187
  • 7
  • 38
  • 63
0
votes
1 answer

How to unit test newly created object inside method?

I have PaymentPresenter with method payWorkOrder(). That method accepts some parameters and based on the logic created two new objects: WoPayment PaymentRequest Here is the code for that: @RunWith(PowerMockRunner.class)…
Zookey
  • 2,637
  • 13
  • 46
  • 80
0
votes
1 answer

How to skip test if a condition is failed Junit (Instrumentation) Testing Android

I am writing test cases for a device testing in which I will have to check some Wifi, Messaging, Camera and Audio Apis but only if the device is capable of that Feature Example : @Test public void testMessagingApi() throws Exception { ....some…
0
votes
2 answers

Test text submit on SearchView when using Espresso

How can I submit some text to SearchView in androidTest? onView(withId(R.id.search_src_text)).perform(typeText("text")) doesn't work for me - app crashes at this line @Test fun testSearchViewTextSubmit() { …
user924
  • 8,146
  • 7
  • 57
  • 139
0
votes
1 answer

Context getColor in activity and AndroidJunit4 return different result. Why?

I'm getting color (i.e. R.color.my_white is FFFFFFFF ) from my resource as below ContextCompat.getColor(getContext(), R.color.my_white) And when I perform AndroidJUnit4 testing, I also get the same color. ContextCompat.getColor(getContext(),…
Elye
  • 53,639
  • 54
  • 212
  • 474
0
votes
1 answer

Unit test stops after addOnCompleteListener of the Firebase Task API

I've implemented my first testcase for my App. Before every testcase, I need to login using Firebase. I've programmed everything in my Firebase class using the Tasks API. The Firebase class is asynchronuous but sends back a Task, and the result can…
0
votes
0 answers

Sqlite junit test in android

I have a class called CallWcfTask that extended from extends AsyncTask. I have declared these fields: private String _CiName; private CallWcfCallBack wcfCallBack; private SQLiteDatabase _Db; private List infoList = new…
Cyrus the Great
  • 5,145
  • 5
  • 68
  • 149
0
votes
1 answer

Firebase Test lab Instrumented test error : NoClassDefFoundError

I'm trying to use Firebase Test Lab to run Instrumented Tests. When I run local instrumented tests with android studio everything goes well. When I upload APKs (app-debug.apk and app-debug-androidTest.apk) to Firebase test lab and run the…
Jch Pal
  • 191
  • 2
  • 7