Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JUnit5 getting NullPointerException when expecting another kind of exception

I’m trying to test a simple class, where I’m testing a case where if I put a string into a method, where the method is not part of a certain list (sidearms), it should return an illegalArguementException. In this example: "Not Desert Eagle" is not a part of the array list sidearms. But why is the test failing and returning that it expected an IllegalArguementException but was a in fact a nullPointerException?

The test class:

package RandomClassGenerator;

import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.Test;

public class SidearmTest {
    
    private Sidearm sideArm;
    private String sidearm = "NOT Desert Eagle";

    @Test
    public void testSidearm() {
        assertThrows(IllegalArgumentException.class, () -> {
            sideArm.sidearmAttachment(sidearm);
        });
        
    }

The class being tested:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import java.util.Arrays;
import java.util.List;

public class Sidearm implements IGiveRandom{
    
    // This class gives the player a sidearm with/without an attachment, as long as Overkill is not a given perk

    private String sidearm;
    private String sidearmAttachment;

    private List<String> sidearms = Arrays.asList("M9","USP.45","M1911.45","Desert Eagle");
    private List<String> sidearmAttachments = Arrays.asList("None","Silencer");

    // Constructor:
    public Sidearm() {
        sidearm = sidearm();
        sidearmAttachment = sidearmAttachment(sidearm);
    }
    // Gives a random sidearm from the sidearms array list
    public String sidearm() {
        int randomFromList = rand.nextInt(sidearms.size());
        sidearm = sidearms.get(randomFromList);
        return sidearm;
    }
    // Randomely, either gives or not, an attachment for the sidearm:
    public String sidearmAttachment(String weapon) {
        // The Desert Eagle has no attachments
        if (!(sidearms.contains(weapon))) throw new IllegalStateException("Sidearm not found");
        if (sidearm.equals("Desert Eagle")) {
            sidearmAttachment = "None";
        } else {
            // The rest of the sidearms can either have a silencer or no attachment
            int randomFromList = rand.nextInt(sidearmAttachments.size());
            sidearmAttachment = sidearmAttachments.get(randomFromList);
        }
        return sidearmAttachment;
    }

>Solution :

why is the test failing and returning that it expected an IllegalArguementException but was a in fact a nullPointerException?

Because variable sideArm still has its default value (null) in the test, when you try to invoke sidearmAttachment() on it. You need to create a Sidearm for it to refer to, that will be the subject of the test.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading