Home > front end >  Unit testing. java.lang.NullPointerException
Unit testing. java.lang.NullPointerException

Time:06-02

everyone! I am writing unit tests for the mobile app. But I have an error that I don't know how to solve. The error has the following form:

java.lang.NullPointerException
    at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:841)
    at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:806)
    at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:630)
    at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:223)
    at com.example.projectMP3.SplashScreenActivityTest.testLaunchOfNewActivity(SplashScreenActivityTest.java:14)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)

Program code "SplashScreenActivity.java":

public class SplashScreenActivity extends AppCompatActivity {
    LinearLayout linearLayout;
    /** Toolbar */
    androidx.appcompat.widget.Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_screen);

        toolbar = findViewById(R.id.toolBar);
        setSupportActionBar(toolbar);

        linearLayout = findViewById(R.id.Splash_Activity);
        linearLayout.setOnClickListener(v -> {
            Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        });

        getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
                WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
        );

        Objects.requireNonNull(getSupportActionBar()).hide();

        Thread thread = new Thread() {
            public void run() {
                try {
                    sleep(3000);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    Intent intent = new Intent(SplashScreenActivity.this,
                            MainActivity.class);
                    startActivity(intent);
                    finish();
                }
            }
        };
        thread.start();
    }
}

Program code "SplashScreenActivityTest.java":

import android.widget.TextView;

import org.junit.Test;

import static org.junit.Assert.assertNotNull;

public class SplashScreenActivityTest {

    @Test
    public void testLaunchOfNewActivity() {
        SplashScreenActivity activity = new SplashScreenActivity();
        TextView nameApplication =  activity.findViewById(R.id.textView3);
        assertNotNull(nameApplication.getText());
    }
}

Program code "activity_splash_screen.xml":

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@ id/Splash_Activity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimaryDark"
    android:orientation="vertical"
    tools:context=".SplashScreenActivity">
    <androidx.appcompat.widget.Toolbar

        android:id="@ id/toolBar"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:background="@drawable/tab_indicater"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical">

        <ImageView
            android:id="@ id/imageView2"
            android:layout_width="320dp"
            android:layout_height="320dp"
            android:layout_gravity="center_horizontal"
            app:srcCompat="@drawable/splash_screen_logo"
            android:layout_marginTop="100dp"
            app:tint="#120a8f"
            android:contentDescription="@string/startSplashScreen" />
    </LinearLayout>

    <TextView
        android:id="@ id/textView3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="cursive"
        android:gravity="center"
        android:text="@string/appName"
        android:textColor="#FFAB00"
        android:textSize="50sp"
        android:paddingLeft="30dp"
        android:paddingRight="30dp"
        android:layout_marginTop="2dp"
        android:textStyle="bold" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="bottom"
        android:orientation="vertical">

        <TextView
            android:id="@ id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/all_rights_are_reserved"
            android:textColor="@color/white"
            android:textSize="15sp" />
    </LinearLayout>
</LinearLayout>

This is my first time writing unit tests. I hope for your help! Thank you in advance.

CodePudding user response:

The issue related to your Activity class. You just tried to test activity like a simple class. But in the android, Activity it's a system component managed by the system, and you can't just create an Activity using constructor.

On the official site it has a documentation how to test an activity: https://developer.android.com/guide/components/activities/testing

CodePudding user response:

It seems you are calling the activity and accessing the UI component in that activity. I don't know if you can do that in unit test without launching the app which makes it Instrumented test(which should be tested with AndroidJunit4)

  • Related