How can I import Configuration class approriately? - android

When I try to use Configuration class android studio says "Cannot resolve symbol 'Configuration'". How can I use Configuration class appropriately?
Here is the class:
package android.example.com;
import android.content.Context;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Before;
import org.junit.runner.RunWith;
#RunWith(AndroidJUnit4.class)
public class ActivityInputOutputTest {
#Before
public void setup() {
Configuration config;
}
}
And Here's the build.gradle (Module: app):
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "android.example.com"
minSdkVersion 15
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation "androidx.work:work-testing:2.2.0"
// Required -- JUnit 4 framework
testImplementation 'junit:junit:4.12'
// Optional -- Robolectric environment
testImplementation 'androidx.test:core:1.0.0'
// Optional -- Mockito framework
testImplementation 'org.mockito:mockito-core:1.10.19'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
compileOnly 'com.jakewharton.espresso:espresso:1.1-r2'
androidTestCompile('com.jakewharton.espresso:espresso:1.1-r2') {
exclude group: 'com.squareup.dagger'
}
}

Add the following in your build.gradle
implementation "androidx.work:work-runtime:2.2.0"

Related

Android Unit Testing dependencies not found

I am trying to write the following unit test for a function in my project
import android.content.Context
import org.junit.Test
import androidx.test.core.app.ApplicationProvider
import com.adi_random.tracky.api.searchBook
import com.adi_random.tracky.models.GoodreadsBook
import com.google.gson.Gson
import okhttp3.Call
import okhttp3.Callback
import okhttp3.Response
import java.io.IOException
import com.google.common.truth.Truth.assertThat
/**
* Created by meadi on 6/27/2020.
*/
class BookFetchTest {
/**
* Test if the Tracy API searchBook endpoint returns the expected result and gets parsed correctly
*/
val context = ApplicationProvider.getApplicationContext<Context>()
#Test
fun bookFetchResultValidation() {
val query = "Dune";
searchBook(query, context, object : Callback {
override fun onFailure(call: Call, e: IOException) {
TODO("Not yet implemented")
}
override fun onResponse(call: Call, response: Response) {
val gson = Gson();
val res = gson.fromJson<Array<GoodreadsBook>>(
response.body?.charStream(),
Array<GoodreadsBook>
::class.java
)
assertThat(res).hasLength(20);
}
})
}
}
When hitting run, I am getting Unresolved reference in :app:compileDebugUnitTestKotlin gradle task for the following dependencies: test (double clicking the error highlights androidx.test.core.app.ApplicationProvider), common ( in com.google.common.truth.Truth.assertThat), ApplicationProvider ( in ApplicationProvider.getApplicationContext()) and assertThat (in the assertThat call at the end of the test).
Here is my module build.gradle file:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
buildFeatures {
viewBinding true
}
testOptions {
unitTests.includeAndroidResources = true
}
defaultConfig {
applicationId "com.adi_random.tracky"
minSdkVersion 23
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
useLibrary 'android.test.runner'
useLibrary 'android.test.base'
useLibrary 'android.test.mock'
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.0'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.squareup.okhttp3:okhttp:4.1.0'
implementation 'com.google.code.gson:gson:2.8.6'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.navigation:navigation-fragment-ktx:2.1.0'
implementation 'androidx.navigation:navigation-ui-ktx:2.1.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.ext:truth:1.2.0'
androidTestImplementation 'com.google.truth:truth:0.42'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation 'androidx.test:core:1.2.0'
// AndroidJUnitRunner and JUnit Rules
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test:rules:1.2.0'
}
As you can see I have those dependencies added to my gradle build file with the androidTestImplementation directive, so I don't understand why those errors get thrown. Does anyone have any idea? Thanks in advance!
Edit:Here is a screenshot of the problem:
Add this dependency:
def androidx_test_core = "1.2.0"
androidTestImplementation "androidx.test:core-ktx:$androidx_test_core"
def androidx_test_ext = "1.1.1"
androidTestImplementation "androidx.test.ext:junit-ktx:$androidx_test_ext"
def hamcrestVersion = '2.2'
testImplementation "org.hamcrest:hamcrest:$hamcrestVersion"

java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null)

I am making some unit testing. but when i want to run that unit test it gave me error like this
"java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null)"
what i am doing wrong?
this is my gradle. i wonder if i add wrong dependency into it, please help me :'(
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
multiDexEnabled true
applicationId "com.example.androidjetpacksubmission1"
minSdkVersion 15
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
debug{
applicationIdSuffix ".debug"
debuggable true
}
}
testOptions {
unitTests.returnDefaultValues = true
}
androidExtensions {
experimental = true
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'com.github.bumptech.glide:glide:4.10.0'
implementation "androidx.lifecycle:lifecycle-extensions:2.1.0"
implementation 'androidx.test.espresso:espresso-idling-resource:3.2.0'
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'com.squareup.okhttp3:okhttp:3.10.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0'
implementation 'com.facebook.shimmer:shimmer:0.5.0'
implementation "androidx.arch.core:core-testing:2.1.0"
implementation "org.mockito:mockito-android:3.1.0"
implementation 'androidx.multidex:multidex:2.0.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.10.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:core:1.2.0'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test:rules:1.2.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.ext:truth:1.2.0'
androidTestImplementation 'androidx.arch.core:core-testing:2.1.0'
androidTestImplementation 'com.google.truth:truth:0.42'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-accessibility:3.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-web:3.2.0'
androidTestImplementation 'androidx.test.espresso.idling:idling-concurrent:3.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-idling-resource:3.2.0'
androidTestImplementation 'com.android.support.test:runner'
androidTestImplementation group: 'org.mockito', name: 'mockito-core', version: '3.1.0'
androidTestImplementation group: 'org.mockito', name: 'mockito-inline', version: '3.1.0'
testImplementation "androidx.arch.core:core-testing:2.1.0"
androidTestImplementation "org.mockito:mockito-android:3.1.0"
androidTestImplementation "androidx.arch.core:core-testing:2.1.0"
}
and this is my unit test code
package com.example.androidjetpacksubmission1.ui.detail
import androidx.lifecycle.Observer
import com.example.androidjetpacksubmission1.data.entity.TvShowEntity
import com.example.androidjetpacksubmission1.data.repository.RemoteRepository
import com.example.androidjetpacksubmission1.network.Network
import com.example.androidjetpacksubmission1.network.NetworkListener
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito.mock
import org.mockito.Mockito.verify
class DetailTvShowViewModelTest : NetworkListener {
override fun onSuccess(msg: String) {
}
override fun onFailure(msg: String?) {
}
#get:Rule
var executorRule = androidx.arch.core.executor.testing.InstantTaskExecutorRule()
private lateinit var detailTvShowViewModel: DetailTvShowViewModel
private lateinit var remoteRepository: RemoteRepository
private lateinit var observer: Observer<*>
private val tvShowTitleExample = "Jumanji: The Next Level"
#Before
fun setup(){
observer = mock(Observer::class.java)
remoteRepository = RemoteRepository(Network.routes())
detailTvShowViewModel = DetailTvShowViewModel(remoteRepository,tvShowTitleExample)
detailTvShowViewModel.networkListener = this
}
#Test
fun getDetailMovie() {
detailTvShowViewModel.getDetailTvShow().observeForever(observer as Observer<in TvShowEntity>)
detailTvShowViewModel.fetchDetailTvShow()
verify(observer).onChanged()
}
}
private fun <T> Observer<T>.onChanged() {
}

Gradle failed to sync issue iin IntelliJ

I am sure there has been many posts about Gradle sync issue in INtelliJ, but usually with various types of errors.
I am trying to run a test automation on mobile app, but keep encountering the issues as follow, despite upgrading all possible libraries base on advices given by fellow StackOverflower.
Main code:
import io.appium.java_client.android.AndroidDriver;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.net.MalformedURLException;
import java.net.URL;
/**
* Instrumented test, which will execute on an Android device.
*
* #see Testing documentation
*/
//#RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
private AndroidDriver driver;
#Before
public void setup() throws MalformedURLException {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "emulator-5444");
capabilities.setCapability("platFormName","Android");
capabilities.setCapability("appPackage","com.mol.molwallet.uat");
capabilities.setCapability("appActivity","com.mol.molwallet.start.SplashActivity");
capabilities.setCapability("noReset","true");
driver = new AndroidDriver(new URL("http://127.0.0.1:4725/wd/hub"),capabilities);
}
#Test
public void myFirstTest() {
// Context of the app under test.
driver.findElement(By.xpath("//*[#text='LOG IN']")).click();
//assertEquals("com.example.test", appContext.getPackageName());
}
}
App/build.gradle
android {
compileSdkVersion 29
buildToolsVersion "29.0.1"
defaultConfig {
applicationId "com.example.test"
minSdkVersion 15
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
noinspection GradleCompatible
//implementation 'com.android.support:appcompat-v7:29+'
implementation 'androidx.appcompat.appcompat:1.0.0'
//implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
implementation 'androidx.annotation:annotation:1.1.0'
//androidTestImplementation 'com.android.support.test:runner:1.0.2'
//androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
//androidTestImplementation 'com.android.support:support-annotations:24.0.0'
//implementation("com.android.support:support-v4:27.1.1")
implementation project(':react-native-fast-image')
}
if(hasProperty('buildScan')){
buildScan {
termsOfServiceUrl = 'https://gradle.com/terms-of-service';
termsOfServiceAgree = 'yes'
}
}
And the error message is as follow when I try to run the main code
Hope to have advice on this issue which has been bugging me.
try this
//noinspection GradleCompatible

Dagger2 - ApplicationComponent not generated

I followed vogella article: http://www.vogella.com/tutorials/Dagger/article.html
I did everything step by step as they suggested. My build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "test.daggertest"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:design:28.0.0'
implementation 'com.google.dagger:dagger:2.15'
implementation 'com.google.dagger:dagger-android:2.15'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.15'
annotationProcessor 'com.google.dagger:dagger-compiler:2.15'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
MyApplication class:
public class MyApplication extends Application implements HasActivityInjector
{
#Inject
DispatchingAndroidInjector<Activity> dispatchingAndroidInjector;
#Override
public void onCreate()
{
super.onCreate();
DaggerMyApplicationComponent.create().inject(this);
}
#Override
public DispatchingAndroidInjector<Activity> activityInjector()
{
return dispatchingAndroidInjector;
}
}
Then I did Rebuild project, and still it throws error DaggerMyApplicationComponent is unresolved.
Am I missing something or their article is outdated? Any ideas?

Cannot resolve symbol MathUtils

I started a new project on Android Studio, targeting API 26, and did nothing but to add the following line:
import android.util.MathUtils;
and got the error: Cannot resolve symbol MathUtils
Here's my MainActivity.java:
package com.example.HeyJude.test2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.MathUtils;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
And my build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.example.HeyJude.test2"
minSdkVersion 26
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
I tried using File > Invalidate Caches (with restart), but it didn't help. Any other ideas?
If you search for it, did you mean this?
android.support.v4.math.MathUtils

Categories

Resources