importing correct AssertThat method for Robolectric Test - android

I am attempting to run the test from Robolectric.org's Writing Your First Test page. The test in question looks like this:
#Test
public void clickingLogin_shouldStartLoginActivity() {
WelcomeActivity activity = Robolectric.setupActivity(WelcomeActivity.class);
activity.findViewById(R.id.login).performClick();
Intent expectedIntent = new Intent(activity, WelcomeActivity.class);
assertThat(shadowOf(activity).getNextStartedActivity()).isEqualTo(expectedIntent);
}
I get this compile error: Cannot resolve method 'assertThat(android.content.Intent).
The two possibilities I see for importing this method are org.hamcrest.MatcherAssert.assertThat and org.junit.Assert.assertThat, neither of which have a single-argument assertThat method as is being used in this Robolectric test.
My app's build.gradle has these dependencies:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.android.support:design:23.1.0'
testCompile "org.robolectric:robolectric:3.0"
testCompile 'junit:junit:4.12'
}
What framework/library is this test using?

It is neither junit or hamcrest assertion API. I think it is Android AssertJ or just AssertJ:
testCompile 'org.assertj:assertj-core:1.7.1'

follow the following and the issue should go away. Put the first line in you gradle build file
testCompile 'org.mockito:mockito-core:1.9.5'
testCompile 'junit:junit:4.12'
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
public class SomethingTest {
#Test
public void testSomething() {
assertThat(true, 1>1);
}
}
this link should provide more details also
Android Studio and Robolectric

Related

Stetho - Android Studio Initialization

I am having trouble trying to use Stetho, I've tried to search and follow the steps provided by others but, it seems none of them highlight my problem. Below is what is in my needed files for Stetho utilization.
My problem is it cannot identify the method "initialize" I've tried other ways of implementing the method through the suggestion of other users but, I still meet the same error
build.gradle (Module: app)
dependencies {
compile 'com.facebook.stetho:stetho:1.5.0'
compile 'com.facebook.stetho:stetho-okhttp3:1.5.0'
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:support-v4:24.2.1'
testCompile 'junit:junit:4.12'
}
in my Stetho Application Class
import android.app.Application;
public class Stetho extends Application {
#Override
public void onCreate() {
super.onCreate();
Stetho.initializeWithDefaults(this);
}
}
declared in my Manifest
<application
android:name=".Stetho"
android:allowBackup="true"
I thought the problem is, I don't actually have the library implemented correctly, but I am not sure.
You are probably getting name conflicts because your App class is named Stetho as well. Rename your App class on the .java and .manifest files. Import the Stetho package into your app class.
Your app class should look something like this:
import android.app.Application;
import com.facebook.stetho.Stetho;
public class MyApp extends Application {
#Override
public void onCreate() {
super.onCreate();
Stetho.initializeWithDefaults(this);
}
}
And your manifest like this:
<application
android:name=".MyApp"
android:allowBackup="true" />

AndroidJUnit4.class not found on Instrumentation tests

I'm having trouble with AndroidJunit4.class import.
I created the test class in the androidTest/java/ folder but it seems that the class is not found.
Even if I force the right import it still doesn't work. ( import android.support.test).
#RunWith(AndroidJunit4.class)
public class RandomTest {
}
Here is my gradle :
defaultConfig {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
dependencies {
compile "com.android.support:support-annotations:23.4.0"
compile "com.android.support:appcompat-v7:23.4.0"
compile 'cat.ereza:customactivityoncrash:1.5.0'
testCompile 'junit:junit:4.12'
androidTestCompile 'junit:junit:4.12'
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')
androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2.2')
androidTestCompile('com.android.support.test.espresso:espresso-intents:2.2.2')
testCompile('com.android.support.test:runner:0.5')
testCompile('com.android.support.test:rules:0.5')
testCompile('com.android.support.test.espresso:espresso-core:2.2.2')
testCompile('com.android.support.test.espresso:espresso-contrib:2.2.2')
testCompile('com.android.support.test.espresso:espresso-intents:2.2.2')
testCompile 'org.mockito:mockito-core:1.10.19'
testCompile "org.robolectric:robolectric:3.1.1"
}
Weird thing is that I can find AndroidJunit4 file in my project, but in its parent AndroidJunit4ClassRunner, AndroidRunnerParams class is not resolved even if I also can find the file in my Project.
Comment out the annotation
//#RunWith(AndroidJunit4.class)
In Android Studio, choose "Build > Rebuild Project"
Get the annotation back in again
#RunWith(AndroidJunit4.class)
ALT-ENTER -> Import class

AWS MobileHub Android - Import statement com.amazonaws.mobile

I am new to AWS. I created AWS sample app which works fine. But when I try to add the code to my new application, I am getting error in Application class on import statement.
import com.amazonaws.mobile.AWSMobileClient;
import com.amazonaws.mobile.push.PushManager;
import com.amazonaws.mobile.user.signin.SignInManager;
public class Application extends MultiDexApplication {
private final static String LOG_TAG = Application.class.getSimpleName();
#Override
public void onCreate() {
// My code //
.
.
.
Android studio gives error on "mobile" part of these import statements.
I don't know if any Jars needs to be added in the project.
Gradle code builds without the error.
Following is my gradle code -
compile fileTree(dir: 'libs', include: ['*.jar'])
compile fileTree(include: ['*.jar'], dir: 'app/libs')
compile 'com.android.support:design:23.2.1'
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:support-v4:23.1.0'
compile('com.amazonaws:aws-android-sdk-s3:2.2.11')
compile 'com.amazonaws:aws-android-sdk-core:2.+'
compile('com.amazonaws:aws-android-sdk-cognito:2.2.11')
compile('com.amazonaws:aws-android-sdk-mobileanalytics:2.2.11')
compile('com.amazonaws:aws-android-sdk-sns:2.2.11')
compile 'com.google.android.gms:play-services-gcm:7.8.0'
compile 'com.google.android.gms:play-services-plus:7.8.0'
compile 'com.android.support:multidex:1.0.0'
Let me know if I should add some more of my code here.
The classes you try to import do not exist in the AWS SDK. Check the sample to find out where they come from.
Please read the instructions in the READ_ME/index.html file of your sample download project. The same instructions exist on the Build page in AWS Mobile Hub. There you will find instructions to copy/paste the helper source code that was included in the download into your own project, including the com.amazonaws.mobilehub java package. This source is included in the project download and must be copied to your own product application.
https://forums.aws.amazon.com/forum.jspa?forumID=88

In Android, why do I need to use androidTestCompile for the "test" sourceSet?

So I have a library module that wraps an API, and I want to write some tests. I want to import things like JUnit and MockWebServer, but only for the test sourceSet, and not for the androidTest one, as I want to use the former because the latter would cause the tests to be run in an android device or AVD, which I do not want. Therefore, I have this in my gradle file:
sourceSets {
main {
test {
setRoot('src/test')
}
}
}
...
dependencies {
...
testCompile 'junit:junit:4.12'
testCompile 'com.squareup.okhttp:mockwebserver:2.4.0'
}
However, this will not work, and instead I will have to import the dependencies as androidTestCompile ones. Why is this?
You should have a structure like this:
root
module
src
main
test
And all you need in your build.gradle is (without setting source sets):
dependencies {
// Unit testing dependencies.
testCompile 'junit:junit:4.12'
testCompile 'com.squareup.okhttp:mockwebserver:2.4.0'
}
You can check in the official repo from Google:
A collection of samples demonstrating different frameworks
collection of Google's Android testing tools
If you would like to use unit test and Instrumentation tests you should have:
root
module
src
main
test
androidTest
In this case your build.gradle should be:
dependencies {
// Dependencies for local unit tests
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-all:1.10.19'
testCompile 'org.hamcrest:hamcrest-all:1.3'
// Android Testing Support Library's runner and rules
androidTestCompile 'com.android.support.test:runner:0.3'
androidTestCompile 'com.android.support.test:rules:0.3'
}

AndroidStudio/Gradle with powermock

I couldn't find any info on how to setup powermock with Android Studio/Gradle. Everything I've tried resulted in build exceptions.
Could anybody show a correct way to do it?
Thanks.
I'm posting in order to help future readers, you need to add these dependencies for powermock in AS
testImplementation 'junit:junit:4.12'
testImplementation 'org.powermock:powermock-api-mockito:1.6.2'
testImplementation 'org.powermock:powermock-module-junit4-rule-agent:1.6.2'
testImplementation 'org.powermock:powermock-module-junit4-rule:1.6.2'
testImplementation 'org.powermock:powermock-module-junit4:1.6.2'
Add the following lines to your dependencies{} block:
testCompile 'junit:junit:4.12'
testCompile 'org.powermock:powermock:1.6.5'
testCompile 'org.powermock:powermock-module-junit4:1.6.5'
And if you would like to use PowerMockito, add the following line:
testCompile 'org.powermock:powermock-api-mockito:1.6.5'
In the build script, add the following:
sourceSets {
unitTest {
java.srcDir file('*your test directory*') //for example: tests/java
}
}
android {
sourceSets {
instrumentTest.setRoot('*your root test directory*') //for example: tests
}
}
repositories {
mavenCentral()
}
dependencies {
testCompile 'junit:junit:4.11'
testCompile 'org.powermock:powermock-mockito-release-full:1.4.9'
}
Then, do gradle unitTest from the command line.
Hope that works. If it doesn't, post the output of the command line.
If you want to use more recent versions of Mockito, you can use something like this, which is adapted from the mockito 2 Powermock docs. Do make sure you use the right version of PowerMock for the given version of Mockito.
...
testCompile 'junit:junit:4.12'
testCompile "org.mockito:mockito-core:2.4.0"
testCompile 'org.powermock:powermock-module-junit4:1.7.0RC2',
'org.powermock:powermock-api-mockito2:1.7.0RC2'
// mockito
testImplementation 'org.mockito:mockito-core:2.4.0'
androidTestImplementation 'org.mockito:mockito-core:2.4.0'
// PowerMock
testImplementation 'org.powermock:powermock-core:1.7.0RC2'
testImplementation 'org.powermock:powermock-module-junit4:1.7.0RC2'
testImplementation 'org.powermock:powermock-api-mockito2:1.7.0RC2'
I have used same as #Bhargav used with some additional features added with it
code coverage for test case (if testCoverageEnabled is true, then it enable Jacoco tool)
unit test will test only your code and do not depend on any particular behaviour of Android platform by using (UnitTests.returnDefaultValues = true)
Add this marked lines in build.gradle to enable JUnit, PowerMockito, JaCoCo
My example compiled from all the other answers I could find, with latest versions at the time of writing:
app\build.gradle
dependencies {
testImplementation group: 'junit', name: 'junit', version: '4.13'
...
testImplementation group: 'org.powermock', name: 'powermock-api-mockito2', version: '2.0.7'
testImplementation group: 'org.powermock', name: 'powermock-module-junit4', version: '2.0.7'
}
A test class where say Android Log class was static mocked.
import android.util.Log;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
#RunWith(PowerMockRunner.class)
#PrepareForTest({Log.class})
public class DateUtilsTest {
#BeforeClass
public static void beforeClass() {
PowerMockito.mockStatic(Log.class);
}
...
}

Categories

Resources