Express Test on Android returns null to mActivityRule.getActivity(). getWindowManager()

Asked

Viewed 39 times

1

I get an error when I try to access the property of Activity mActivityRule.getActivity(). getWindowManager(). getDefaultDisplay();

I tried to change Intentstestrule instead of Activitytestrule and nothing.

I tried calling Instructions activitytest2.class instead of Statements.class and nothing

``` @Runwith(Junit4.class) public class Instrucoesactivitytest2 {

@Rule public IntentsTestRule<InstrucoesActivity> mActivityRule = new IntentsTestRule<InstrucoesActivity>(
        InstrucoesActivity.class,false, false){
    @Override
    protected Intent getActivityIntent() {
        Receita receita = new Receita();
        receita.setId(1);
        receita.setName("Nutella Pie");
        receita.setServings(8);
        receita.setImage("");
        receita.setIngredients(null);
        receita.setSteps(null);

        Bundle bundle = new Bundle();
        bundle.putParcelable("receita", receita);
        Intent intent = new Intent(InstrumentationRegistry.getContext(),InstrucoesActivity.class);
        intent.putExtras(bundle);
        return intent;
    }

};


@Before
public void init(){
    mActivityRule.getActivity ()
            .getSupportFragmentManager().beginTransaction();
}

@Before
public void setUp() throws Exception {
    //assumeTrue(isScreenSw600dp());
    assumeTrue(!isScreenSw600dp());
}


@Test public void test(){
    assumeTrue(isScreenSw600dp());
    onView(withId(R.id.container_instrucoes)).check(matches(isDisplayed()));
}


private boolean isScreenSw600dp() {
    //Intents.init();
    DisplayMetrics displayMetrics = new DisplayMetrics();
    mActivityRule.getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    float widthDp = displayMetrics.widthPixels / displayMetrics.density;
    float heightDp = displayMetrics.heightPixels / displayMetrics.density;
    float screenSw = Math.min(widthDp, heightDp);

   // Intents.release();
    return screenSw >= 600;
}

public int getScreenOrientation(){
    Display getOrient = mActivityRule.getActivity().getWindowManager().getDefaultDisplay();
    int orientation = Configuration.ORIENTATION_UNDEFINED;
    if(getOrient.getWidth()==getOrient.getHeight()){
        orientation = Configuration.ORIENTATION_SQUARE;
    } else{
        if(getOrient.getWidth() < getOrient.getHeight()){
            orientation = Configuration.ORIENTATION_PORTRAIT;
        }else {
            orientation = Configuration.ORIENTATION_LANDSCAPE;
        }
    }
    return orientation;
}

} ```

getting errors

```Running tests

$ adb shell am Instrument -w -r -e debug false -e class com.studiotopdev.culinaria.Instrucoesactivitytest2 com.studiotopdev.culinaria.test/android.support.test.runner.Androidjunitrunner Client not ready yet. Started running tests

java.lang.Nullpointerexception: Attempt to invoke virtual method 'android.view.Windowmanager com.studiotopdev.culinaria.InstructionActivity.getWindowManager()' on a null Object Reference with.studiotopdev.culinaria.Instrucoesactivitytest2.isScreenSw600dp(Instrucoesactivitytest2.java:81) with.studiotopdev.culinaria.Instrucoesactivitytest2.setup(Instrucoesactivitytest2.java:68) at java.lang.reflect.Method.invoke(Native Method) at org.junit.Runners.model.Frameworkmethod$1.runReflectiveCall(Frameworkmethod.java:50) at org.junit.Internal.runners.model.ReflectiveCallable.run(Reflectivecallable.java:12) at org.junit.Runners.model.Frameworkmethod.invokeExplosively(Frameworkmethod.java:47) at org.junit.Internal.runners.statements.RunBefores.evaluate(Runbefores.java:24) at android.support.test.rule.Activitytestrule$Activitystatement.evaluate(Activitytestrule.java:433) 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.Chedule(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.Chedule(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:58) at android.support.test.runner.Androidjunitrunner.onStart(Androidjunitrunner.java:375) at android.app.Instrumentation$Instrumentationthread.run(Instrumentation.java:1932) ```

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.