-3

Probably just me being blind, but why do i get a nullpointer exception at "when(sqldummy.getAllMembers()).thenReturn(mockedMemberList);"? Trying out Mockito for the first time, so i might have bad implementations. (I'm not quite sure).

I've been staring myself blind for a while now. System-out debugging didn't yield any real information other than it's null.

I tried moving the entire code from the @before into the actual method and it works there. But that is ugly and @Before is to handle duplicate code like this, that I would otherwise have to add to every other testmethod I create.

Please help.

public class TestShipHandling extends TestCase{

private static ShipHandling shipHandling;
private static SQLDUMMY sqldummy;
private static ArrayList<Member> mockedMemberList;
private static ArrayList<Ship> mockedShipList;


@Before
public void before() {
    sqldummy = mock(SQLDUMMY.class);
    mockedMemberList = new ArrayList<>();
    mockedShipList = new ArrayList<>();
    shipHandling = new ShipHandling(sqldummy);  //Instance shiphandling class with mocked SQLDUMMY

    Member m1 = new Member();
    m1.setMemberFirstName("Max");
    m1.setMemberLastName("W0w");
    m1.setMemberID("MW222");
    mockedMemberList.add(m1);

    System.out.println(mockedMemberList);
    Member m2 = new Member();
    m2.setMemberFirstName("Andrew");
    m2.setMemberLastName("Gower");
    m2.setMemberID("AG222");
    mockedMemberList.add(m2);

    Member m3 = new Member();
    m3.setMemberFirstName("Maximum");
    m3.setMemberLastName("Crispness");
    m3.setMemberID("MC999");
    mockedMemberList.add(m3);

    Ship s = new Ship();
    s.setShipName("qweqwe");
    s.setShipClass("big");
    s.setShipGunCaliber(300);
    s.setShipLength(200);
    s.setShipNGuns(30);
    mockedShipList.add(s);

}

@After
public void after() {
    System.out.println(mockedMemberList);
    mockedMemberList.clear();
    mockedShipList.clear();
}

@Test
public void testAddShipReturnsTrue() throws Exception {
    when(sqldummy.getAllMembers()).thenReturn(mockedMemberList);

    Member tempMem = sqldummy.getAllMembers().get(0);

    String shipName = "Victorium";
    String shipClass = "Battleship";
    int shipGunCaliber = 305;
    int shipLength = 320;
    int shipNGuns = 10;

    assertTrue(shipHandling.addShip(tempMem, shipName, shipClass, shipGunCaliber, shipLength, shipNGuns));
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Danny
  • 97
  • 1
  • 15

1 Answers1

1

You're mixing Junit3's TestCase and JUnit4's @Before and @After annotations.

Looking to the javadoc of TestCase you read:

A test case defines the fixture to run multiple tests. To define a test case

1.implement a subclass of TestCase
2.define instance variables that store the state of the fixture
3.initialize the fixture state by overriding setUp()
4.clean-up after a test by overriding tearDown().

Hence to make this code work you must:

Option 1: Do not extend TestCase and import org.junit.Assert for the assertTrue.

Option 2: Change the name of before() to setUp(); change the name of after() to tearDown(); remove @Test, @Before and @After annotations.

Any option above should work, but I recommend you to use the first one (reasons on the link above).

PS: You don't need to make your fields static ;)

Community
  • 1
  • 1
Willian
  • 504
  • 1
  • 9
  • 22
  • Ah, YES! Thank you so much! I'd never have figured that one out. NullPointers in my experience have always been a silly error like in initializing or using the wrong instance or something along those lines. Thank you good sir! – Danny Nov 23 '15 at 20:11