0

I have been scouring the internet for solutions to this. It's my first time implementing junit and I'm fairly new to Java (more familiar with Python). I am writing a test class for a Library class and trying to instantiate a Library object using @Before, but I keep getting a null pointer exception for every test method I write. Here is the test class:

class LibraryTest {

    public Library l1;

    @Before
    public void setUp() {
        l1 = new Library("Public Library", "Glasgow");

    }

    @After
    public void tearDown() {
        l1 = null;
    }

    // test methods...

}

Apologies for the formatting, I can't figure out how to tell this editor where my code begins and ends. Hopefully it's clear enough.

An example of a test that fails:

@Test
public void newMembershipNoTest() {
    int before = l1.newMembershipNo();
    int after = l1.newMembershipNo();
    assertEquals(1, after - before);
}

And the method it is testing:

public int newMembershipNo()
{
    highestMembershipNo ++;
    return highestMembershipNo;
}

Here is the constructor for the library class (all variables are set to private):

public Library(String name, String location)
{
    this.name = name;
    this.location = location;
    allResources = new ArrayList<Resource>();
    members = new ArrayList<Member>();
    authors= new ArrayList<Author>();
    highestMembershipNo = 0;
}

This is what I think people mean by the stack trace (I haven't been doing this very long!)

java.lang.NullPointerException
    at com.enanram.LibraryTest.changeTitleTest(LibraryTest.java:55)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:628)
    at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:117)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$7(TestMethodTestDescriptor.java:184)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:180)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:127)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:68)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:135)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:229)
    at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:197)
    at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:211)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:191)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)
    at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:71)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)
Ole V.V.
  • 76,217
  • 14
  • 120
  • 142
Donagh
  • 67
  • 8
  • Please share the exception trace. – Tushar Dec 06 '20 at 18:20
  • The test looks correct, at least from the snippet you shared. Can you share the stacktrace of the exception? – Mureinik Dec 06 '20 at 18:21
  • Right, nothing wrong so far. The stack trace and at least one example of a failing test would help here. – markspace Dec 06 '20 at 18:23
  • @Tushar do you mean this (based on a test method where l1 is the only variable used, I think it is referring to that): java.lang.NullPointerException at com.enanram.LibraryTest.addMemberTest(LibraryTest.java:32) at java.base/java.util.ArrayList.forEach(ArrayList.java:1541) at java.base/java.util.ArrayList.forEach(ArrayList.java:1541) – Donagh Dec 06 '20 at 18:23
  • Your question needs a lot more info. How are we supposed to know how NPE is caused, without seeing the accused methods? – DraxDomax Dec 06 '20 at 18:23
  • So that looks like your test is trying to add entries in to as list that you haven't instantiated correctly – DaveH Dec 06 '20 at 18:25
  • 1
    @DraxDomax I assumed it was something to do with how I was setting up the object. I've added more info to the post. – Donagh Dec 06 '20 at 18:26
  • And `newMembershipNoTest()` is *inside the class* `LibraryTest`, yes? And you are actually running JUnit, because the framework has to run or those annotations do nothing. Again, a *stack trace* of the failing method would help, I suspect you are editing too much out of your code for us to guess what the problem is. – markspace Dec 06 '20 at 18:28
  • @markspace yes, everything is within the scope of the class. The annotations are showing yellow in IntelliJ (I assume I'd get a red line if something hadn't been imported). I have the following import statements in the class file, before the class declaration: import org.junit.After; import org.junit.Before; import org.junit.jupiter.api.Test; And I'll edit the post to include (what I think is) the stack trace. – Donagh Dec 06 '20 at 18:34
  • is highestMembershipNo declared at class level , can u share that code ? – Amit Kumar Lal Dec 06 '20 at 18:36
  • Can you share the code for `Library's constructor? – Mureinik Dec 06 '20 at 18:38
  • @Hades that's declared as private int in the Library class, then set to 0 in the constructor. I know it works as I have tested it with print statements in the Main method. So the problem is almost certainly in my testing code. – Donagh Dec 06 '20 at 18:40
  • Can you post the code from the method changeTitleTest? – dreamcrash Dec 06 '20 at 18:44
  • @dan1st I read that before posting - it makes me think that somehow my l1 variable is still null, despite seemingly setting it up correctly. – Donagh Dec 06 '20 at 18:53
  • 1
    The stack trace tells you exactly what is wrong: `java.lang.NullPointerException at com.enanram.LibraryTest.changeTitleTest(LibraryTest.java:55)` So which line is 55? Can you show its containing method and any instance variables it references? – markspace Dec 06 '20 at 19:02
  • @markspace that's just the test method that I'm running. I don't think the error is in the method, since it comes up for all the methods. I've been writing more of them since I posted this, and they're all the same (the reason I have kept going is that this is for an assignment and we get some marks for writing the test code even if it doesn't work). – Donagh Dec 06 '20 at 19:14
  • 1
    @Donagh Post this method .LibraryTest.changeTitleTest this is the reason for the NPE – dreamcrash Dec 06 '20 at 19:24
  • It’s hard for us to relate to *all the methods*. To give us something concrete to work with, please tell us which of the code lines you have posted is line 55 of `LibraryTest.java`. – Ole V.V. Dec 06 '20 at 19:29
  • @OleV.V. I can see how it's difficult for people to help short of showing you the whole package. Line 55 is simply: public void containsResourceTest() { – Donagh Dec 06 '20 at 19:31
  • You're using JUnit4 annotations while running tests with JUnit5 (Jupiter). Probably it can cause the problem. I prefer use JUnit Jupiter only. – zforgo Dec 06 '20 at 19:31

1 Answers1

2

Solved - someone pointed out that I was using JUnit4 annotations (@Before, @After) with JUnit5 (@BeforeEach, @AfterEach). This is what happens when you google how to do things without checking the version. Thanks @zforgo for spotting it.

Donagh
  • 67
  • 8