jeudi 13 juin 2019

Writing multiple test functions gives NullPointerException for each case except the first

I am trying to test functions of VideoStore Class which contains a array of Video type named store. When I run the test class as junit test, only first of the 4 tests passes, other throw NullPointer Exception. Each tests passes when I run them individually. I have given my test class.

I have already tried using @BeforeClass instead of @Before Annotation. I have also tried instantiating in east @Test function separately.

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

import tm2.VideoStore;

public class VideoTest {
VideoStore vs;

@Before
public void before() {
    vs = new VideoStore();
    vs.addVideo("LifeOfGuy");
}

@Test
public void testAddVideo() {
    assertEquals("LifeOfGuy",vs.store[0].videoName);
}

@Test
public void testDoCheckout() {
    vs.doCheckout(vs.store[0].videoName);
    assertTrue(vs.store[0].checkout);
}

@Test
public void testDoReturn() {
    vs.doReturn("LifeOfGuy");
    assertFalse(vs.store[0].checkout);
}

@Test
public void receiveRating() {
    vs.receiveRating("LifeOfGuy", 5);
    assertEquals(5,vs.store[0].rating);
}
}

Junit Result:---- Runs 4/4 Errors 3 Failures 0 1. testAddVideo pass 2. testDoCheckout java.lang.NullPointerException 3. testDoReturn java.lang.NullPointerException 4. testreceiveRating java.lang.NullPointerException

Each one passes individually

Aucun commentaire:

Enregistrer un commentaire