How to run all AndroidTests with Gradle? - android

I use Android Studio 1.1.0 and created a new Android project. AS creates this new project with one ApplicationTest. When I call:
$ ./gradlew clean createDebugCoverageReport
this ApplicationTest is executed. Now I added a new test:
public class ActivityTest extends ActivityInstrumentationTestCase2<MainActivity>
But this new test isn't executed. Whats going wrong? Both test classes are in the same folder:
app\src\androidTest\java\eu.the4thfloor.testapp\
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "eu.the4thfloor.testapp"
minSdkVersion 10
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
jacoco {
version = '0.7.3.201502191951'
}
buildTypes {
debug {
testCoverageEnabled true
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
}

The problem was the wrong constructor.
AS suggested this one:
public ActivityTest(Class<MainActivity> activityClass) {
super(activityClass);
}
But with this constructor the test isn't executed. You have to use this constructor:
public ActivityTest() {
super(MainActivity.class);
}
The complete test class looks like this:
import android.test.ActivityInstrumentationTestCase2;
public class ActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
public ActivityTest() {
super(MainActivity.class);
}
public void test2() {
assertTrue(true);
}
}

Give only
./gradlew test
a try.

Related

Android instrumentation testing with mockito

My question is similar to this one. I am implementing instrumentation testing for my Android project. I want to test the integration by mocking my delegate, so that I can test if delegate methods are called as expected.
My gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.mytest"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
repositories {
maven { url 'http://repo1.maven.org/maven2' }
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.4.0';
compile 'com.android.support:design:23.4.0';
androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.2';
androidTestCompile 'com.google.dexmaker:dexmaker:1.2';
androidTestCompile 'org.mockito:mockito-core:1.10.19'
androidTestCompile 'com.android.support:support-annotations:23.4.0';
androidTestCompile 'com.android.support.test:runner:0.5';
androidTestCompile 'com.android.support.test:rules:0.5';
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2';
}
My Test file:
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
MainActivity mainActivity;
#Mock
MyDelegate delegate;
public MainActivityTest() {
super(MainActivity.class);
}
#Before
public void setUp() throws Exception {
mainActivity = getActivity();
MockitoAnnotations.initMocks(this);
mainActivity.setDelegate(delegate);
}
#Test
public void testDelegateMethod1() throws Exception {
mainActivity.doSomething();
//delegate.method1 should be called
}
}
I got the error:
java.lang.NullPointerException
at com.google.dexmaker.mockito.DexmakerMockMaker.getInvocationHandlerAdapter(DexmakerMockMaker.java:80)
at com.google.dexmaker.mockito.DexmakerMockMaker.getHandler(DexmakerMockMaker.java:75)
at org.mockito.internal.util.MockUtil.isMockitoMock(MockUtil.java:74)
at org.mockito.internal.util.MockUtil.isMock(MockUtil.java:66)
at org.mockito.internal.configuration.injection.scanner.MockScanner.isMockOrSpy(MockScanner.java:86)
at org.mockito.internal.configuration.injection.scanner.MockScanner.preparedMock(MockScanner.java:72)
at org.mockito.internal.configuration.injection.scanner.MockScanner.scan(MockScanner.java:61)
at org.mockito.internal.configuration.injection.scanner.MockScanner.addPreparedMocks(MockScanner.java:47)
at org.mockito.internal.configuration.InjectingAnnotationEngine.injectMocks(InjectingAnnotationEngine.java:96)
at org.mockito.internal.configuration.InjectingAnnotationEngine.processInjectMocks(InjectingAnnotationEngine.java:62)
at org.mockito.internal.configuration.InjectingAnnotationEngine.process(InjectingAnnotationEngine.java:56)
at org.mockito.MockitoAnnotations.initMocks(MockitoAnnotations.java:108)
Could anyone help with this issue?
Not sure why
MockitoAnnotations.initMocks(this);
cause the crash, I end up with using
System.setProperty("dexmaker.dexcache", InstrumentationRegistry.getTargetContext().getCacheDir().getPath());
instead.
#Captor
private ArgumentCaptor<Callback> CallbackArgumentCaptor;
And then in your test method you will verify that it is called by doing the following:
verify(repository).retrieveSomething(callbackArgumentCaptor.capture());
callbackArgumentCaptor.getValue().successful();
or call failure depends on what you are testing.
Don't forget to add this as well to the #Before
#Before
public void setup() {
MockitoAnnotations.initMocks(this);
}

can't call kotlin module from java module

I want to have an android app in java, but one library module in kotlin. However when I try to run the app in my phone, there's an error saying that it can't find my Kotlin class. This is my kotlin class:
package com.example.mylibrary
import android.util.Log
class A {
fun helloWorld(){
Log.d("Kotlin", "Hello World!")
}
}
and gradle file for my kotlin module:
apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
minSdkVersion 11
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.1'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
buildscript {
ext.kotlin_version = '0.6.+'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
repositories {
mavenCentral()
}
Here's my main activity:
package com.example.uisample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.example.mylibrary.A;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
A obj = new A();
obj.helloWorld();
}
}
Notice how android studio imports com.example.mylibrary.A instead of com.example.mylibrar.Akt as the reference says. Android studio reports no errors before compiling.
Gradle file for app:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "com.example.uisample"
minSdkVersion 11
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.1'
compile project(':mylibrary')
}
After running the project, gradle throws this error: "cannot find symbol class A". What am I doing wrong?
A few things:
Apply the kotlin-android plugin to your library module build.gradle (see the docs):
apply plugin: 'kotlin-android'
As it stands now, you're not even telling gradle to compile Kotlin files.
Your buildscript block should probably be at the top-level build.gradle rather than the module build.gradle.
Consider updating the fully released version of Kotlin 1.0.1-2
For that, you need to add
buildscript {
ext.kotlin_version = '1.4.30' <-- This line
repositories {
google()
jcenter()
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" <-- This line
}
}
In the gradle.build (project)
and then add the
apply plugin: 'kotlin-android'
In the gradle.build (App)

BR cannot be resolved

I am not able to add notifyPropertyChanged(int itemId) because of certain issue in gradle, i guess!
any help would be well appreciated
public class RegisterForm extends BaseObservable {
#Bindable
public String firstName;
#Bindable
public int getFirstNameLabelVisibility(){
return TextUtils.isEmpty(firstName) ? View.INVISIBLE : View.VISIBLE;
}
public void setFirstNameFromView(String firstNameFromView) {
firstName = firstNameFromView;
// notifyPropertyChanged(BR.firstName); this line is giving error!
}
}
Build.gradle // for App
apply plugin: 'com.android.application'
apply plugin: 'com.android.databinding'
apply plugin: 'android-apt'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.example.jivrajsingh.databindingimp"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}
configurations {
apt
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
apt 'com.android.databinding:compiler:1.0-rc0'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.squareup.okhttp:okhttp:2.4.0'
compile 'com.google.android.gms:play-services-auth:8.3.0'
compile 'com.google.android.gms:play-services-gcm:8.3.0'
}
Build.gradle // for project
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0-alpha1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
// classpath 'com.android.tools.build:gradle:1.3.0'
classpath 'com.android.databinding:dataBinder:1.0-rc2'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.+'
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Thanks for your precious time :)
You have the old plugin setup with the new gradle version.
Remove the data binding plugin & its dependency from classpath.
Now it is integrated and all you need is
android {
dataBinding { enabled = true }
}
http://developer.android.com/tools/data-binding/guide.html

error: cannot find symbol in Android gradle library module unit test

I have a test project in IntelliJ (using the Android Studio plugin) and I'm trying to set up unit tests as described here.
When running a junit test the project can't seem to find my sources under src > main > java. The following test
package io.adaptiv.plzwrk.lib;
import junit.framework.TestCase;
import org.junit.Test;
public class AdaptivTest extends TestCase {
#Test
public void testDoStuff() throws Exception {
assertEquals(42, Adaptiv.doStuff());
}
}
gives the error
error: cannot find symbol
assertEquals(42, Adaptiv.doStuff());
^
symbol: variable Adaptiv
location: class AdaptivTest
My 'test' class
package io.adaptiv.plzwrk.lib;
public class Adaptiv {
public static int doStuff() {
return 42;
}
}
The layout of my project looks like this
With settings.gradle
include ':lib'
The project build.gradle file
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
The build.gradle under the lib dir
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.1'
}
}
apply plugin: 'com.android.library'
repositories {
jcenter()
}
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
minSdkVersion 9
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_6
targetCompatibility JavaVersion.VERSION_1_6
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
testCompile 'junit:junit:4.12'
testCompile "org.mockito:mockito-core:1.9.5"
}

Android Gradle: How to access java class from module in build.gradle script file?

I want to develop a small android library and share it later with the community. I want to use javassist, especially the project morpheus to manipulate the bytecode of annotated classes during the build time.
Currently im stuck while i want to use my TestTransformer class from the Library within the TestApplications build.gradle script. The TestTransformer.java is defined as usual within the library project.
library build.gradle
apply plugin: 'com.android.library'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
minSdkVersion 10
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.github.stephanenicolas.javassist:javassist-build-plugin-api:1.0.0'
}
application build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.0'
classpath 'com.github.stephanenicolas.morpheus:morpheus-plugin:1.0.8'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
apply plugin: 'com.android.application'
apply plugin: 'morpheus'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "example.com.testapplication"
minSdkVersion 10
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:22.0.0'
//compile 'com.github.stephanenicolas.morpheus:morpheus-plugin:1.0.8'
compile project(':testlibrary')
}
morpheus {
//transformers = new example.com.testlibrary.TestTransformer()
transformers = new com.example.testlibrary.TestTransformer()
}
TestTransformer.java
import javassist.build.IClassTransformer;
import javassist.build.JavassistBuildException;
public class TestTransformer implements IClassTransformer {
#Override
public void applyTransformations(CtClass ctClass) throws JavassistBuildException {
}
#Override
public boolean shouldTransform(CtClass ctClass) throws JavassistBuildException {
return false;
}
}
Now i get the following error when i try to sync the projects with gradle files:
Error:(44, 0) Cause: startup failed:
build file 'C:\Users\MyUser\AndroidStudioProjects\TestApplication\app\build.gradle': 44: unable to resolve class com.example.testlibrary.TestTransformer
# line 44, column 20.
transformers = new com.example.testlibrary.TestTransformer()
1 error
Is it even possible to achieve my planned propose?
Thanks alot!

Categories

Resources