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
27
votes
2 answers

What is touch mode and why is it important for the ActivityTestRule class?

The ActivityTestRule class takes in an initialTouchMode parameter in its constructor. This is not explained in the class reference (or anywhere online) except as follows: initialTouchMode - true if the Activity should be placed into "touch mode"…
26
votes
2 answers

Could not find method test() for arguments on Android studio project

In my Android application, I want exclude some test cases in a package so that I used test task in build.gradle file. for example: apply plugin: 'com.android.library' test{ exclude '**/calltest/Summary.class' } If sync the project I got…
M.A.Murali
  • 9,988
  • 36
  • 105
  • 182
21
votes
2 answers

@Config constants parameter could not be found in Robolectric

I am trying to write a Robolectric test. I was following few tutorials where they seem to be using @RunWith(RobolectricTestRunner::class) @Config(constants = BuildConfig::class) to setup the test, but in my case The parameter constants does not…
erluxman
  • 18,155
  • 20
  • 92
  • 126
15
votes
5 answers

Mockito lenient() when to use

As I understand, lenient silences the exceptions thrown by StrictStubbing. Based on this, lenient shouldn't be used, maybe only temporary while doing TDD, because strict stubbing exceptions would generally mean that your code is either wrong, test…
htafoya
  • 18,261
  • 11
  • 80
  • 104
14
votes
1 answer

How to run single test class in Android using Gradle command?

In my Android application I have several test classes. If I run following command ./gradlew connectedAndroidTest it runs all the test cases inside the android test folder and generate test report for all test classes but I need to test a specific…
14
votes
2 answers

How do I unit test (with JUnit or mockito) recyclerview item clicks

I am currently trying to unit test recyclerview addonitemclick listner, with either junit or mockito. here's my code: private void mypicadapter(TreeMap> photosMap) { List mItems = new ArrayList<>(); for…
user2386226
13
votes
1 answer

AndroidJunit4 doesn't accept space function test name?

I have the following test, where the test name is with space and backtick for my instrumental test @RunWith(AndroidJUnit4::class) class MyTestClass { @Rule @JvmField var activityRule: ActivityTestRule =…
Elye
  • 53,639
  • 54
  • 212
  • 474
10
votes
2 answers

Android RecyclerView Adapter Item count is returning 0 on unit testing

I am trying to test RecyclerView with AndroidJunit4, it is my test code: @Rule public ActivityTestRule rule = new …
dev_android
  • 8,698
  • 22
  • 91
  • 148
10
votes
3 answers

Test recreating Android Activity using instrumentation and JUnit4

I want to write test for recreating activity. Performing rotation is optional. I want the test to be written in up-to-date version of testing framework "blessed" by Google. I am new to writing tests, so I want to learn basic, main-stream, well…
MateuszL
  • 2,751
  • 25
  • 38
8
votes
0 answers

Gradle connectedCheck fails with 'No tests found' after migrating to AndroidX

I'm working on a multi-module project, and after making the transition from the support libraries to AndroidX, ./gradlew connectedCheck fails: FAILURE: Build failed with an exception. * What went wrong: Execution failed for task…
Justin Pollard
  • 6,661
  • 1
  • 18
  • 20
7
votes
0 answers

Caused by: java.lang.NoSuchMethodError: No static method listOf(Ljava/lang/Object;)Ljava/util/List; in class Lk/v/i; or its super classes

I'm using kotlin collections library (listOf, first, toList, etc,..) methods in our AndroidTest package to run UI tests using AndroidJunit4 runner and I'm coming across these type of errors wherever I refer to kotlin collections…
7
votes
0 answers

Custom Test Orchestrator or callback for start/finish

I'm using the Android Test Orchestrator to run my UI tests and i need to generate a report of the results when all the tests are finished. I need to have a callback for when the orchestrator starts running the tests and when it finishes running all…
6
votes
2 answers

Espresso test giving: No Koin Context configured. Please use startKoin or koinApplication DSL

I'm running a espresso uiautomator test which runs well when using the green run > button on android studio. (image below) Yet ./gradlew connectedAndroidTest is giving an error: No Koin Context configured. Please use startKoin or koinApplication…
ir2pid
  • 5,604
  • 12
  • 63
  • 107
6
votes
2 answers

Android FragmentScenario - Unable to find package androidx.fragment.app.testing

I'm getting this error while trying to use FragmentScenario in Android error: package androidx.fragment.app.testing does not exist import androidx.fragment.app.testing.FragmentScenario; ^ This is my simple Unit…
chrizonline
  • 4,779
  • 17
  • 62
  • 102
6
votes
1 answer

AndroidJUnitRunner unit tests are very slow to execute

I'm using the AndroidJUnitRunner in my project, but the unit tests are painfully slow to execute on a device and emulator both in Android Studio and from the command-line via gradlew. I'm using the master branch of this open-source project,…
1
2 3 4 5 6