Roboelectric testing - android

I have never used any kind of JUnit before,so from what I search there is a good tool called roboelectric.
I try to run a simple test as shown in their website but the assertThat() and shadowOf() are not recognised.
Here is the test
#RunWith(RobolectricTestRunner.class)
#Config(constants = BuildConfig.class)
public class MainActivityTest {
#Test
public void clickingLogin_shouldStartLoginActivity() {
MainActivity activity = Robolectric.setupActivity(MainActivity.class);
activity.findViewById(R.id.login).performClick();
Intent expectedIntent = new Intent(activity, LoginActivity.class);
//This line doesn't work.
assertThat(shadowOf(activity).getNextStartedActivity()).
isEqualTo(expectedIntent);
}
}
And this how I set Roboelectric in gradle.
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "24.0.0"
defaultConfig {
applicationId "testing.theo.robotutorial"
minSdkVersion 14
targetSdkVersion 24
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:24.0.0'
testCompile "org.robolectric:robolectric:3.0"
}
Any ideas?
Thanks.

Replace with the following assertion will work.
assertTrue(shadowOf(activity).getNextStartedActivity().filterEquals(expectedIntent));
You can refer to answer by Hoisie in Roboelectric github issue #2627 for details.

Related

error: package android.v7.app does not exist

Hi there stackoverflow community, hope you can help!
Im using udacity to learn android development and I have ran into a problem. there are a few errors that have come up:
error: package android.v7.app does not exist
error: cannot find symbol class AppCompatActivity
error: method does not override or implement a method from a supertype
error: cannot find symbol variable super
error: cannot find symbol method setContentView(int)
error: cannot find symbol class openNumbersList
error: cannot find symbol method startActivity(Intent)
Everything was working fine but I was following a tutorial and noticed that my imports in my Main Activity didn't match with what was on the tutorial so I changed:
"import..."
to
import android.v7.app.AppCompatActivity;
import android.os.Bundle;
also even before this, "AppCompatActivity" was in red:
public class MainActivity extends AppCompatActivity {
Main Activity:
package com.example.android.miwok;
import android.content.Intent;
import android.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the content of the activity to use the activity_main.xml layout file
setContentView(R.layout.activity_main);
}
public void openNumbersList(View view){
Intent i = new Intent(this, openNumbersList.class);
startActivity(i);
}
}
Gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion '27.0.3'
defaultConfig {
applicationId "com.example.android.miwok"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
implementation 'com.android.support:appcompat-v7:23.3.0'
implementation 'com.android.support:support-v4:23.3.0'
implementation 'com.android.support:design:23.3.0'
}
hope this helps.
Try your gradle as follow
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.example.android.miwok"
minSdkVersion 19
targetSdkVersion 27
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:support-v4:27.1.1'
implementation 'com.android.support:design:27.1.1'
}
I think that above code solve your issue
Anyone who faces this problem has to revise the coding of padding and alignment attributes.
The fault is simple.
All you have to do is to be careful with the attributes specifying.

Room Persistence inside of Lib

I am looking for a good way to import Room Persistence inside of my Lib to export .aar . I have the following issue that .aar cannot handle dependencies.
When I export my arr and import into another project as lib, it seems as it has no scope to room.
my lib Gradle file :
apply plugin: 'com.android.library'
apply plugin: 'maven'
archivesBaseName = '*****'
android {
compileSdkVersion 26
buildToolsVersion "25.0.3"
defaultConfig {
minSdkVersion 16
targetSdkVersion 22
versionCode 5
versionName "1.1.2"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
// Write out the current schema of Room
javaCompileOptions {
annotationProcessorOptions {
arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
configurations {
deployerJars
}
repositories {
mavenCentral()
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'android.arch.persistence.room:runtime:1.1.0-beta3'
annotationProcessor 'android.arch.persistence.room:compiler:1.1.0-beta3'
}
Thanks.

Watson Conversation autogenerate Android app - 'Unknown pattern character 'X''

I'm using the auto generated example from IBM Watson Mobile App, but when i tried to run it, it shows an error in the following part of code. I found similar errors around SO, but i think this is another one different.
public ServiceCall<MessageResponse> message(String workspaceId, MessageRequest request) {
Validator.isTrue((workspaceId != null) && !workspaceId.isEmpty(), "'workspaceId' cannot be null or empty");
RequestBuilder builder = RequestBuilder.post(String.format(PATH_MESSAGE, workspaceId));
builder.query(VERSION_PARAM, versionDate);
if (request != null) {
//the error shows here
builder.bodyJson(GsonSingleton.getGson().toJsonTree(request).getAsJsonObject());
} else {
builder.bodyJson(new JsonObject());
}
return createServiceCall(builder.build(), ResponseConverterUtils.getObject(MessageResponse.class));
}
The following is my build.gradle file:
apply plugin:'base'
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.ibm.watson_conversation"
minSdkVersion 19
targetSdkVersion 25
versionCode 1
versionName "1.0"
manifestPlaceholders = ['appIdRedirectScheme': android.defaultConfig.applicationId]
vectorDrawables.useSupportLibrary = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
lintOptions {
warning 'InvalidPackage'
abortOnError false
}
}
dependencies {
compile ('com.ibm.mobilefirstplatform.clientsdk.android:core:[2.0.0,3.0.0)')
compile 'com.ibm.watson.developer_cloud:conversation:3.8.0'
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:25.1.0'
compile 'com.android.support:design:25.1.0'
}
The issue was fixed in 3.9.0.
You just need to update the dependency version to be 3.9.1:
compile 'com.ibm.watson.developer_cloud:conversation:3.9.1'

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);
}

I cant import the Facebook library

I´m following the next tutorial to use Facebook on my app, them, In the step five I write the line: compile 'com.facebook.android:facebook-android-sdk:4.0.0' but nothing happends and I can import the library in the step 6.
This is my build.grandle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "21.1.2"
repositories {
mavenCentral()
}
defaultConfig {
applicationId "com.mookup.singup2"
minSdkVersion 14
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.facebook.android:facebook-android-sdk:4.0.0'
}
And in my MainAplication, I have this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FacebookSdk.sdkInitialize(getApplicationContext());
}
I saw the dependencies and they say "compile", but I can´t import the facebook library.
Dependencies

Categories

Resources