[英]Nullpointer exception - JAVA Mockito
Probably just me being blind, but why do i get a nullpointer exception at "when(sqldummy.getAllMembers()).thenReturn(mockedMemberList);"?可能只是我瞎了,但为什么我会在“when(sqldummy.getAllMembers()).thenReturn(mockedMemberList);”处得到一个空指针异常? Trying out Mockito for the first time, so i might have bad implementations.
第一次尝试 Mockito,所以我可能有不好的实现。 (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.
除了 null 之外,系统输出调试没有产生任何实际信息。
I tried moving the entire code from the @before into the actual method and it works there.我尝试将整个代码从 @before 移动到实际方法中,并且它在那里工作。 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.
但这很丑陋,@Before 是处理这样的重复代码,否则我必须将其添加到我创建的所有其他测试方法中。
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));
}
You're mixing Junit3's TestCase and JUnit4's @Before and @After annotations .您正在混合 Junit3 的 TestCase 和 JUnit4 的 @Before 和 @After annotations 。
Looking to the javadoc of TestCase you read:查看您阅读的 TestCase的javadoc :
A test case defines the fixture to run multiple tests.
一个测试用例定义了运行多个测试的夹具。 To define a test case
定义测试用例
1.implement a subclass of TestCase
1.实现TestCase的一个子类
2.define instance variables that store the state of the fixture2.定义存储夹具状态的实例变量
3.initialize the fixture state by overriding setUp()3.通过覆盖setUp()初始化fixture状态
4.clean-up after a test by overriding tearDown().4.通过覆盖tearDown()进行测试后的清理。
Hence to make this code work you must:因此,要使此代码工作,您必须:
Option 1 : Do not extend TestCase
and import org.junit.Assert
for the assertTrue
.选项 1 :不要扩展
TestCase
并为assertTrue
导入org.junit.Assert
。
Option 2 : Change the name of before()
to setUp()
;选项 2 :将
before()
的名称更改为setUp()
; change the name of after()
to tearDown()
;将
after()
的名称更改为tearDown()
; remove @Test
, @Before
and @After
annotations.删除
@Test
、 @Before
和@After
注释。
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 ;) PS:您不需要将字段设为静态;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.