Giter Site home page Giter Site logo

braisgabin / fragmenttestrule Goto Github PK

View Code? Open in Web Editor NEW
102.0 5.0 16.0 180 KB

JUnit Rule to test a Fragment in isolation

Home Page: https://engineering.21buttons.com/how-to-test-fragments-in-isolation-a7435a4cdc92

License: Apache License 2.0

Java 100.00%
android android-testing espresso junit-rule

fragmenttestrule's Introduction

Fragment Test Rule

Test Fragments in isolation.

Download

androidTestImplementation 'com.21buttons:fragment-test-rule:2.0.1'
debugImplementation 'com.21buttons:fragment-test-rule-extras:2.0.1'

Note: If you don't support AndroidX yet use the version 1.1.0.

Usage

@Rule
public FragmentTestRule<?, FragmentWithoutActivityDependency> fragmentTestRule =
    FragmentTestRule.create(FragmentWithoutActivityDependency.class);

@Test
public void clickButton() throws Exception {
  onView(withText(R.string.button)).perform(click());

  onView(withText(R.string.button_clicked)).check(matches(isDisplayed()));
}

You can check the sample code for more examples.

fragmenttestrule's People

Contributors

albertvilacalvo avatar braisgabin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

fragmenttestrule's Issues

Support the new dagger-android library

Does FragmentTestRule support the new dagger-android library?

Having a Fragment with the following code:

public class FragmentA extends Fragment {

    @Inject
    public Dependency dependency;

    @Override
    public void onAttach(Context context) {
        AndroidSupportInjection.inject(this);
        super.onAttach(context);
    }

The corresponding Activity should embed a code as follows:

public class MainActivity
        extends Activity
        implements HasSupportFragmentInjector {

    @Inject
    public DispatchingAndroidInjector<Fragment> supportFragmentInjector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        AndroidInjection.inject(this);
        super.onCreate(savedInstanceState);
        // ...
    }

    @Override
    public AndroidInjector<Fragment> supportFragmentInjector() {
        return supportFragmentInjector;
    }

with all the Modules and Subcomponents necessary to configure Dagger 2 for Android support, as described in the official documentation.

What is the proper to do so with FragmentTestRule?

How to pass Bundle to Fragments ?

Usually when we use Fragments we pass Data to them via setBundle().
How can i achieve this in my Test Code when using this library ?

Proguard warnings in debug

When I enable proguard in debug builds, I get this warnings:

Warning:com.android21buttons.fragmenttestrule.FragmentTestRule: can't find referenced method 'void runOnUiThread(java.lang.Runnable)' in program class com.android21buttons.fragmenttestrule.FragmentTestRule
Warning:com.android21buttons.fragmenttestrule.FragmentTestRule: can't find referenced method 'android.app.Activity getActivity()' in program class com.android21buttons.fragmenttestrule.FragmentTestRule
Warning:com.android21buttons.fragmenttestrule.FragmentTestRule$1: can't find referenced method 'android.app.Activity getActivity()' in program class com.android21buttons.fragmenttestrule.FragmentTestRule

To compile I have to add -dontwarn com.android21buttons.fragmenttestrule.* to my proguard-rules.pro.

Would it be possible to have the proguard rules included in the library itself so that the each user doesn't have to add them manually? Maybe using consumerProguardFiles.

Error: Unable to resolve activity

If I use:

androidTestCompile 'com.21buttons:fragment-test-rule:1.0.0'

I get this error when I run the tests:

java.lang.RuntimeException: Could not launch activity
at android.support.test.runner.MonitoringInstrumentation.startActivitySync(MonitoringInstrumentation.java:371)
at android.support.test.rule.ActivityTestRule.launchActivity(ActivityTestRule.java:219)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:268)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1960)
Caused by: java.lang.RuntimeException: Unable to resolve activity for: Intent { act=android.intent.action.MAIN flg=0x14000000 cmp=com.prova.provafragmenttestrule/android.support.v4.app.FragmentActivity }
at android.app.Instrumentation.startActivitySync(Instrumentation.java:408)
at android.support.test.runner.MonitoringInstrumentation.access$201(MonitoringInstrumentation.java:90)
at android.support.test.runner.MonitoringInstrumentation$5.call(MonitoringInstrumentation.java:351)
at android.support.test.runner.MonitoringInstrumentation$5.call(MonitoringInstrumentation.java:348)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)

To get rid of the error I have to use:

debugCompile 'com.21buttons:fragment-test-rule:1.0.0'

By using debugCompile (instead of androidTestCompile) the FragmentActivity declared in the library's manifest gets packaged into the apk and found.

Is there a way to use the library with androidTestCompile?


I'm playing with a similar library that allows to test views in isolation, and they have an artifact with the JUnit rule and another one with the Activity + manifest:

https://github.com/novoda/spikes/tree/master/espresso-support#usage

The rule is added with androidTestCompile and the activity + manifest with debugCompile, like this:

androidTestCompile 'rule'
debugCompile 'activity + manifest'

Personally, I haven't found a way to use an Activity placed into the androidTest sourceset without declaring the Activity into the debug sourceset's manifest. The Activity can live in androidTest but it always needs to be declared in the debug manifest to be found.

See this for more info on this:

https://stackoverflow.com/questions/36276909/create-dummyactivity-inside-androidtest-folder-for-testing

https://stackoverflow.com/questions/24451076/android-gradle-espresso-test-not-starting-activity

FragmentTestRule could not found

Added this line in build.gradle

androidTestCompile 'com.21buttons:fragment-test-rule:1.0.0'

Inside MyUiAutomatorTest.Java

@rule
public FragmentTestRule<?, MyVideoFragment> fragmentTestRule =
FragmentTestRule.create(MyVideoFragment.class);

Error:- Could not resolve symbol 'FragmentTestRule'

java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library

In my Android library project, when using FragmentTestRule with the following configuration:

compile 'com.android.support:appcompat-v7:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:support-v4:26.+'
compile 'com.android.support:design:26.+'

I reach the following exception:

java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library

To solve it, I had to add a little more configuration in my AndroidManifest.xml file:

<manifest package="fr.guddy.android_modular_reloaded.first"
          xmlns:android="http://schemas.android.com/apk/res/android">

    <application
        android:allowBackup="true"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.Light">
    </application>

</manifest>

since, by default, the application tag does not contain a android:theme attribute.
Maybe could it be mentioned in the README file.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.