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

1

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

I tried to change IntentsTestRule instead of ActivityTestRule and nothing.

I tried calling InstructionsActivityTest2.class instead of InstructionActivity.class and nothing

'' ' @RunWith (JUnit4.class) public class InstructionActivityTest2 {

@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.InstrucoesActivity.getWindowManager ()' on a null object reference at com.studiotopdev.culinaria.InstrucoesActivityTest2.isScreenSw600dp (InstrucoesActivityTest2.java:81) at com.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.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:58) at android.support.test.runner.AndroidJUnitRunner.onStart (AndroidJUnitRunner.java:375) at android.app.Instrumentation $ InstrumentationThread.run (Instrumentation.java:1932) '' '

    
asked by anonymous 15.01.2018 / 21:37

0 answers