Custom shadows in roboelectric 2? - android

As per the official guide for Roboelectric 1.X (at http://pivotal.github.io/robolectric/customizing.html), the way to use your own shadow class is to create your own test runner and override the appropriate method OR by using #Roboelectric.bindShadowClass (see below).
However, things have changed for 2.X and I can't seem to find the new way to do this.
Anyone know how to use a custom shadow class without changing the
public class CustomTestRunner extends RobolectricTestRunner {
public CustomTestRunner(Class testClass) throws InitializationError {
super(testClass);
}
#Override public void beforeTest(Method method) {
Robolectric.bindShadowClass(ShadowBitmapFactory.class);
Robolectric.bindShadowClass(ShadowDrawable.class);
Robolectric.bindShadowClass(ShadowGeocoder.class);
}
}

You can use the new Robolectric 2.0 Config annotation type to bind custom Shadow classes.
For example you annotate a test method like this:
#Config( shadows = { MyShadow.class, MyOtherShadow.class } )
public void testSomething {
...
}
Best Regards,
Seb

Related

How to assertion a void method in Robolectric Unit Test

How to verify a void method call in Robolectric test case where as no data coming out the called method.
What to assert in this case? Below given an example of the requirement.
public class SampleClass(){
final String TAG = SampleClass.class.getSimpleName();
public void log(){
Log.d(TAG, "Entry Loggd");
}
}
#Test
public void logEntry_test(){
SampleClass sc = new SampleClass();
sc.log();
// What to assert here to verify this log method
}
First off, good on you for writing tests!!! There are a few ways to go about testing that an internal logger is called. It's equally as important to understand what you're looking to test. Testing that a class is logging a specific message is most likely a fragile test, so be fore-warned that you probably don't need it.
Method #1: Using Robolectric
Robolectic documentation doesn't lend itself to answering basic questions, but its codebase is very well documented with its tests. A basic understanding of its principles and how shadows work can get you a long way. ShadowLog tests lay the ground work to this solution.
#RunWith(RobolectricTestRunner.class)
public class SampleClassTest {
#Test
public void log_writesExpectedMessage() {
new SampleClass().log();
ShadowLog.LogItem lastLog = ShadowLog.getLogs().get(0);
assertThat(lastLog.msg).isEqualTo("some message");
// or
assertThat(lastLog.msg).isNotNull();
}
}
Tests using Robolectric v3.1.2
Add the following to your build.gradle file:
testCompile 'org.robolectric:robolectric:3.1.2'
Method #2: Making use of Abstractions
If your sample class derives from an Android class (Activity, Fragment, Application, etc), then using android.util.Log makes sense, but bear in mind that your test will need to be a Robolectric or AndroidInstrumented test. If your SampleClass is just some POJO, then using a simple logging framework may make your testing efforts easier. For example, using Jake Wharton's Timber, your class and test can be written as follows:
import timber.log.Timber;
public class SampleClass {
void log() {
Timber.d("some message");
}
}
// SampleClassTest.java
public class SampleClassTest {
// setting up a Tree instance that we define below
TestTree testTree = new TestTree();
#Test
public void log_writesExpectedMessage() {
// setting up Timber to us the test classes log writer
Timber.plant(testTree);
// invoke the logging function
new SampleClass().log();
// assert
assertThat(testTree.lastMessage).isEqualTo("some message");
}
private class TestTree extends Timber.Tree {
private String lastMessage;
#Override
protected void log(int priority, String tag, String message, Throwable t) {
lastMessage = message;
}
}
}
Good luck, happy testing!
In my understanding you want to mock static methods. I guess, using static mocks are not the most elegant way to testing. Better to use an abstraction as recommended by abest. Although, it can be done with PowerMock.

Android Espresso black-box testing

Does anyone try to do black-box testing with Android Espresso?
Could anyone provides me with some simple example?
I had tried some example before, but failed every time!
Example, I had tried this one:
public class ApplicationTest extends ActivityInstrumentationTestCase2
{
private static final String ACTIVITY_CLASSNAME = "com.example.kai_yu.blackboxtest";
private static Class launchActivityClass;
static
{
try
{
launchActivityClass = Class.forName(ACTIVITY_CLASSNAME);
}
catch (ClassNotFoundException e)
{
throw new RuntimeException(e);
}
}
public ApplicationTest()
{
super(launchActivityClass);
}
#Test
public void testClick()
{
}
}
But Android Studio said:
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.kai_yu.blackboxtest"
com.example.kai_yu.blackboxtest is applicationId which is another installed application on my phone
Thank you!
Espresso can only run as part of an instrumentation test.
Instrumentation tests can only act upon the app under test ( i.e. the target of the instrumentation ).
UIAutomator might be better for your use case.
https://developer.android.com/tools/testing-support-library/index.html#UIAutomator
In Espresso docs you would find this line:
While it can be used for black-box testing, Espresso's full power is unlocked by those who are familiar with the code base under test."
For that reason Espresso testing is called by gray-box testing.
If you're not familiar with programming in Java or Android, or you want just to write black-box testing in the clearest way as possible try to learn instead of Espresso this framework
Calabash-iOS and Calabash-Android are the underlying low-level libraries that empower the Cucumber tool to run automated functional tests on Android...
Website: https://calaba.sh/
GitHub: https://github.com/calabash
Here would you find how and why to start using this framework:
http://blog.teddyhyde.com/2013/11/04/a-better-way-to-test-android-applications-using-calabash/
#RunWith(AndroidJUnit4.class)
#LargeTest
public class EspressoTest1 extends ActivityInstrumentationTestCase2<MainActivity>{
public EspressoTest1() {
super(MainActivity.class);
}
#Before
public void setUp() throws Exception {
super.setUp();
injectInstrumentation(InstrumentationRegistry.getInstrumentation());
}
#Test
public void test1ChatId() {
getActivity();
onView(withId(R.id.anuja)).check(matches(isDisplayed()));
}
#After public void tearDown() throws Exception {
super.tearDown();
}
}
There are two ways to write Espresso Test case one is as per shown above
The Examples are taken from this blog
http://qaautomated.blogspot.in/p/blog-page.html
Where you can find details of hot to run the espresso test case in detail.

Unable to create shadow classes with Robolectric 3 [duplicate]

This question already has an answer here:
Robolectric shadow not working
(1 answer)
Closed 7 years ago.
I followed the Robolectric documentation to create shadow classes but I'm not able to run shadow methods during test, it always uses original methods.
This is my code:
the Original class:
public class Original {
public void print(){
System.out.println("Hi from original class!");
}
}
the Shadow class:
#Implements(Original.class)
public class ShadowOriginal {
#Implementation
public void print(){
System.out.println("Hi from shadow class!");
}
}
the test:
#RunWith(RobolectricGradleTestRunner.class)
#Config(manifest = "src/main/AndroidManifest.xml",
emulateSdk = 21,
reportSdk = 21,
constants = BuildConfig.class,
shadows = {ShadowOriginal.class})
public class OasisTests {
#Test
public void test() {
Original t = new Original();
t.print();
}
}
When I run the tests, it always display "Hi from original class!"
What is wrong in my code? I uses
Android studio 1.2
robolectric 3.0-rc2
robolectric-gradle-plugin 1.0.1
How can I solve this issue?
Thanks in advance
You need a custom robolectric runner where you can register own classes so they can be shadowed. See Robolectric shadow not working

Shadowing TimeZone.getDefault using Robolectric

I'm having some troubles shadowing TimeZone.getDefault() using Robolectric since my AppTest.class is not using my static shadow mwthod in ShadowTimeZone.class.
AppTest.class
#RunWith(RobolectricTestRunner.class)
#Config(manifest = "../App/AndroidManifest.xml")
public class AppTest{
#Test
#Config(shadows = {ShadowTimeZone.class})
public void testTimeZone() {
String expectedTimeZoneId = "Europe/London";
TimeZone timeZone = TimeZone.getDefault();
assertThat(timeZone.getID(), equalTo(expectedTimeZoneId));
}
}
ShadowTimeZone.class
#Implements(TimeZone.class)
public class ShadowTimeZone {
#Implementation
public static TimeZone getDefault() {
return TimeZone.getTimeZone("Europe/London");
}
}
You don't need to use a shadow at all. Use TimeZone.setDefault(TimeZone.getTimeZone("Europe/London")) before your test or in a #Before setup method.
If you still want to use a shadow, the actual signature for getDefault is public static synchronized, so you may need to add synchronized to your shadow method to match.
By default you can only shadow classes from android package. But you can add more classes to work with shadows. See https://stackoverflow.com/a/29641926/3619179

Adding Test Module for RoboGuice When Using Robolectric 2

I am currently upgrading robolectric from version 1 to 2. In my current version I use the following to provide the test module (for binding) to roboguice.
public class RoboTestRunner extends RobolectricTestRunner {
public RoboTestRunner(Class<?> testClass) throws
InitializationError {
super(testClass);
}
#Override
public void prepareTest(Object test) {
Application app = Robolectric.application;
RoboGuice.setBaseApplicationInjector(app, RoboGuice.DEFAULT_STAGE,
Modules.override(RoboGuice.newDefaultRoboModule(app)).with(new
TestModule()));
Injector injector = RoboGuice.getInjector(app);
injector.injectMembers(test);
}
}
However now I have upgraded the prepareTest method is not in this class. Where should I run this code in the new version?
UPDATE
I have found the way to do this. I need to create an class which extends android.app.Application in the project and reference this in the Manifest. Then I create a class like so
public class TestApplication extends Application implements TestLifecycleApplication {
#Override
public void onCreate() {
super.onCreate();
RoboGuice.setBaseApplicationInjector(this, RoboGuice.DEFAULT_STAGE,
RoboGuice.newDefaultRoboModule(this), new TestModule());
}
#Override
public void beforeTest(Method method) {}
#Override
public void prepareTest(Object test) {
TestApplication application = (TestApplication) Robolectric.application;
RoboGuice.setBaseApplicationInjector(application, RoboGuice.DEFAULT_STAGE,
RoboGuice.newDefaultRoboModule(application), new TestModule());
RoboGuice.getInjector(application).injectMembers(test);
}
#Override
public void afterTest(Method method) {}
}
As this class has Test at the start robolectric should automatically find it and use it. However this doesn't seem to be happening. Does anybody know why?
UPDATE 2
This blog would suggest that the testmodule needs to be in the same package however I have all tests in a different package. How do I work around this?
Your TestApplication class should extend your own Application class, not android.app.Application, and it should be in the same package as your Application.
... however I have all tests in a different package.
That shouldn't be a problem. Put your TestApplication in your test module, but use the package from Application.
e.g., if you're using maven, the files would live here:
src/main/java/com/example/Application.java
src/test/java/com/example/TestApplication.java

Categories

Resources