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
6
votes
1 answer

Android RecyclerView Adapter is giving null on unit testing

I am trying to test RecyclerView with AndroidJunit4, it is my test code: package com.kaushik.myredmart.ui; // all includes @RunWith(AndroidJUnit4.class) public class ProductListActivityTest { @Rule public…
dev_android
  • 8,698
  • 22
  • 91
  • 148
5
votes
0 answers

Changing from deprecated ActivityTestRule to ActivityScenarioRule breaks test

I recorded an Espresso test using the recorder in Android Studio 4.2.2, which included a single assertion that a text field on my MainActivity UI was showing with the correct text string. I then saved this to SplashActivityTest.java: public class…
5
votes
1 answer

MockitoJUnit tests running in Android Studio but failed when running on the command line

I'm trying to run unit tests using MockitoJUnitRunner; they run fine under Android Studio, but some (not all) tests fail when running in the command line - which is a big deal, I need to be able to run them from my continuous integration platform,…
mgcaguioa
  • 1,443
  • 16
  • 19
4
votes
2 answers

Configure testLogging for instrumented tests

I would like to configure testLogging for my instrumented tests. However, Gradle seems to ignore my configuration in android.testOptions.unitTests.all.testLogging. There, I have configured that all passed and failed tests should be logged, but…
Martin
  • 598
  • 1
  • 4
  • 27
4
votes
1 answer

Get resource from Android test: InstrumentationRegistry.getInstrumentation().getTargetContext() VS myActivityTestRule.getActivity()

I need to get resource from Android test (AndroidJUnit4ClassRunner). What is the recommended way and why? String some_res_string =…
4
votes
0 answers

Android instrumented unit testing Cannot resolve symbol 'AndroidJUnit4'

I trying to add Android Support Instrumentation test to my application, but i run this problem: Cannot resolve symbol 'AndroidJUnit4' Its seems i don't have the package android.support.test.runner my app's build.gradle: apply plugin:…
3
votes
1 answer

How to apply a Rule to all test cases in a AndroidJUnitRunner?

I'm trying to apply a TestWatcher as a rule across all my test cases run by a particular runner. MetadataCollector: class MetadataCollector : TestWatcher() { ... } TestRunner: class TestRunner : AndroidJUnitRunner() { override fun…
Dragan R.
  • 598
  • 4
  • 18
3
votes
2 answers

"Failed to resolve: org.junit:junit:4.12" with AndroidX Test JUnit

When I try to add dependency on AndroidX Test JUnit 1.0.0-beta01: androidTestImplementation androidx.test.ext:junit:1.0.0-beta01, Gradle Sync fails with the following error: Failed to resolve: org.junit:junit:4.12 All manipulations with adding…
qwertyfinger
  • 634
  • 7
  • 16
3
votes
3 answers

How to unit test AsyncTask in android

Consider below code. How can I test this without using third party libraries? The Assert line is never executed, because it is a different thread and the vm stops running. Many thanks! public class FileParserTask extends AsyncTask
Jim Clermonts
  • 1,694
  • 8
  • 39
  • 94
3
votes
2 answers

Android Espresso Test Suites

I am interested in how to run Espresso tests from command line (gradle task) individually (run group/suite tests then close app and then run another group/suite of tests). Found that it is feasible to implement JUnit Test Suites but do not really…
3
votes
2 answers

Launching Activities via package names

I have been working on testing some packages that I do not have the source code for, and one of the packages is normally launched by pressing three buttons for three seconds. When I try to launch the package using the typical method, I get a…
Tadhg
  • 474
  • 7
  • 19
3
votes
1 answer

Get Activity instance of other app using UiAutomator?

I am writing some Automated TestCase using UiAutomator across apps from my app. My aim is to find the Current Activity of all the app which i click. I have project called MyApp with package called com.example with one Activity, MainActivity I tried…
3
votes
0 answers

Where do the logs go when I try to log in an Instrumented Unit Test in Android?

I've followed this documentation to create an Instrumented Test Unit class: https://developer.android.com/training/testing/unit-testing/instrumented-unit-tests.html#run But when I try to use logs or System.out.println as part of my test method, none…
George Yang
  • 694
  • 6
  • 18
3
votes
0 answers

Perform Instrumentation Testing of SyncAdapter

How does one go about testing a SyncAdapter? I am talking about test methods that verify that onPerformSync is called with the correct parameters and extras and that it responds as expected, etc. etc. The platform seems to provide instrumentation…
Nouvel Travay
  • 6,292
  • 13
  • 40
  • 65
3
votes
1 answer

Android JUnit Parameters are String "null" instead of `null`

I'm trying to run some new parameterized Android tests with null in the parameter set. Unfortunately, the values are showing up as the string "null" instead of null. How can I keep this from happening? @RunWith(Parameterized.class) public class…
Jon
  • 9,156
  • 9
  • 56
  • 73