[英]Cannot pass junit test because of the nullpointerexception
由於 NullPointerException,我的 junit 測試給出了以下失敗,但我必須將空間分配為 null,我該如何解決這個問題? 我添加錯誤的圖片在此處輸入圖片描述
public class Guest {
private Guest guest=null;
private String customerName;
private Room room= null; // This is line 7
/**
* @param name of the new Customer
* **/
public Guest(String customerName){
this.customerName=customerName;
}
/**
* @param checkinRoom the room to be assigned to this guest
* @return returns true if runs successfully
* **/
public boolean checkin(Room checkinRoom) {
boolean result=false;
if( this.room==null && room.getGuest()==null) {
this.room=checkinRoom;
checkinRoom.setGuest(this);
result=true;
}
return result;
}
/**
* Checks out the customer out of the room
* @return returns true if runs successfully
* **/
public boolean checkout() {
if(this.getRoom()==null) {
return false;
}else {
this.getRoom().setGuest(null);
this.room=null;
return true;
}
}
/**
* @return the room of the customer if exists or null
*
* **/
public Room getRoom() {
return this.room;
}
/**
* @return returns name of the customer;
*
* **/
public String getName(){
return this.customerName;
}
public String toString() {
return "Guest " + this.getName();
}
my junit fails are: java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because "null" is null
以下是分配給客戶的房間代碼
package ss.week2.hotel;
public class Room {
private int number;
private Guest guest;
private Room room;
/**
* Creates a <code>Room</code> with the given number, without a guest.
* @param no number of the new <code>Room</code>
*/
public Room(int number) {
this.number = number;
}
/**
* Returns the number of this Room
*/
public int getNumber() {
return number;
}
/**
* Returns the current guest living in this Room
* @return the guest of this Room, null if not rented
*/
public Guest getGuest() {
return guest;
}
/**
* Assigns a Guest to this Room.
* @param guest the new guest renting this Room, if null is given, Room is empty afterwards
*/
public void setGuest(Guest guest) {
this.guest = guest;
}
public String toString() {
return "Room "+ this.getNumber();
}
}
我添加了完整的 junit 測試
package ss.week2.test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import ss.week2.hotel.Guest;
import ss.week2.hotel.Room;
import static org.junit.jupiter.api.Assertions.*;
/**
* Test program for Room and Guest.
* Lab Exercise Software Systems
* @author Arend Rensink, Jip Spel
*/
public class GuestTest {
/** Test variable for a <tt>Guest</tt>-object. */
private Guest ot;
/** Test variable for a <tt>Guest</tt>-object. */
private Guest sien;
/** Test variable for a <tt>Room</tt>-object. */
private Room k101;
/** Test variable for a <tt>Room</tt>-object. */
private Room k102;
/**
* Sets initial variables to a well-defined initial value.
* <p>
* Assigns a <tt>Guest</tt> object to the <tt>ot</tt> instance variable with the
* name {@code "Ot"} and assigns a <tt>Guest</tt> object to the <tt>sien</tt>
* instance variable with the name {@code "Sien"}. Also assigns <tt>Room</tt>
* objects to instance variables <tt>k101</tt> and <tt>k102</tt> with numbers
* <tt>101</tt> and <tt>102</tt> respectively. Lastly, checks in <tt>sien</tt>
* in room <tt>k102</tt>.
*/
@BeforeEach
public void setUp() {
// initialisation of Guest-variables
ot = new Guest("Ot");
sien = new Guest("Sien");
// initialisation of Room-variables
k101 = new Room(101);
k102 = new Room(102);
// check in sien to room 102
sien.checkin(k102);
}
/**
* Tests if the initial condition complies to the specification.
*/
@Test
public void testInitialcondition() {
assertEquals("Ot", ot.getName());
assertEquals("Sien", sien.getName());
assertNull(ot.getRoom());
assertEquals(k102, sien.getRoom());
}
/**
* Tests checking in a new guest in an empty room.
* Calls <tt>ot.checkin(k101)</tt>.
*/
@Test
public void testCheckinEmpty() {
assertTrue(ot.checkin(k101));
assertEquals(k101, ot.getRoom());
assertEquals(ot, k101.getGuest());
}
/**
* Checks checking in a new guest in a room that is already taken.
* Calls <tt>ot.checkin(k102)</tt>.
*/
@Test
public void testCheckinTaken() {
assertFalse(ot.checkin(k102));
assertNull(ot.getRoom());
assertEquals(sien, k102.getGuest());
}
/**
* Tests checking out a guest that rented a room.
* Calls <tt>sien.checkout()</tt>.
*/
@Test
public void testCheckoutKnown() {
assertTrue(sien.checkout());
assertNull(sien.getRoom());
assertNull(k102.getGuest());
}
/**
* Tests checking out a guest that didn't rent a room.
* Calls <tt>ot.checkout()</tt>.
*/
@Test
public void testCheckoutUnknown() {
assertNull(ot.getRoom());
assertFalse(ot.checkout());
assertNull(ot.getRoom());
}
}
堆棧跟蹤:
java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because "null" is null
at ss.week2.hotel.Guest.<init>(Guest.java:7)
at ss.week2.test.GuestTest.setUp(GuestTest.java:39)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:688)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptLifecycleMethod(TimeoutExtension.java:126)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptBeforeEachMethod(TimeoutExtension.java:76)
at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.invokeMethodInExtensionContext(ClassBasedTestDescriptor.java:490)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$synthesizeBeforeEachMethodAdapter$19(ClassBasedTestDescriptor.java:475)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeBeforeEachMethods$2(TestMethodTestDescriptor.java:167)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeBeforeMethodsOrCallbacksUntilExceptionOccurs$5(TestMethodTestDescriptor.java:195)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeBeforeMethodsOrCallbacksUntilExceptionOccurs(TestMethodTestDescriptor.java:195)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeBeforeEachMethods(TestMethodTestDescriptor.java:164)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:127)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:65)
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:129)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
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:143)
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:129)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
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:143)
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:129)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)
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.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:108)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:96)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:84)
at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:98)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:40)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:529)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:756)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:452)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
您發布的代碼與導致您失敗的代碼不同。
導致您失敗的代碼看起來如何以及為什么會這樣,我不知道。
當我運行您的代碼時,對於五種測試方法中的每一種,我都會遇到以下異常:
java.lang.NullPointerException
at ss.week2.hotel.Guest.checkin(Guest.java:22)
at ss.week2.test.GuestTest.setUp(GuestTest.java:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at …
我可以告訴你為什么。 正如您從堆棧跟蹤中看到的那樣,異常是通過設置發生的,因此對於所有測試方法都是一樣的。 由於設置失敗,該方法本身無法執行。 問題出在Guest.checkin()
這一行的邏輯中:
if (this.room == null && room.getGuest() == null) {
當您在setUp()
中嘗試檢查 sien 時,他們的房間是 null。 所以上述條件的第一部分為真,Java 嘗試評估第二部分。 由於room
是 null, room.getGuest()
拋出NullPointerException
。
您可能打算:
if (room == null && checkinRoom.getGuest() == null) {
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.