Why does a NullPointerException error appear for ArrayList.java when my code doesn't use an ArrayLIst?

gotj :

So I have 2 pieces of Java code. One is a simple little piece of source code and the other is a JUnit test class that I'm trying to mess around with. However, whenever I want to debug the code to make sure that this simple code is appearing correctly, I get a NullPointerException at the java.util.ArrayList.forEach(ArrayList.java:1507) error.

Here's my source code:

public class sourceCode {
    public int mid(int x, int y, int z) {
        int m = z;

        if (y < z) {
            if (x < y) {
                m = y;
            }
            else if (x < z) {
                m = x;
            }
        }
        else {
            if (x > y) {
                m = y;
            }
            else if (x > z) {
                m = x;
            }
        }
        return m;
    }

}

Here is my JUnit Test Case code:

import static org.junit.jupiter.api.Assertions.*;
import org.junit.Before;
import org.junit.jupiter.api.Test;

public class sourceCodeTest {
      public sourceCode sourceCodeVar;

      //Test Fixture
      @Before
      public void setUpBeforeClass() throws Exception {
            sourceCodeVar = new sourceCode();
      }

      @Test
      void test() {
          //Test Oracle
          int oracle = 2;
          //Test Case
          int middleTest1 = sourceCodeVar.mid(1, 2, 3);
          assertEquals(oracle, middleTest1);
      }

}

Findings

They are both different files under the same directory, they were created in Eclipse in a project as well. I saw in another slightly related thread that it may be a bug related to Eclipse however upon testing it on VS Code, the same error appears which would indicate that it's an error on my part.

I guess the reason why I'm so confused is that the code seems to be so simple that I can't pinpoint where I messed up.

ruhul :

It seems that you are mixing both JUnit-4 and JUnit-5 in your test codes.

import org.junit.Before;            // <- Junit-4 annotation
import org.junit.jupiter.api.Test;  // <- Junit-5 annotation

As you you are using Junit-5 for test, better use all of the Junit-5 annotation. Replace @Before with org.junit.jupiter.api.BeforeEach -> @BeforeEach.

Your code should be look like:

import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class sourceCodeTest {
      public sourceCode sourceCodeVar;

      // change:
      @BeforeEach
      public void setUpBeforeClass() throws Exception {
            sourceCodeVar = new sourceCode();
      }

      @Test
      void test() {
          //Test Oracle
          int oracle = 2;
          //Test Case
          int middleTest1 = sourceCodeVar.mid(1, 2, 3);
          assertEquals(oracle, middleTest1);
      }

}

For more check this DOCUMENTATION:

@BeforeEach

Denotes that the annotated method should be executed before each @Test, @RepeatedTest, @ParameterizedTest, or @TestFactory method in the current class; analogous to JUnit 4’s @Before. Such methods are inherited unless they are overridden.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=23231&siteId=1