Java Junit Mockito测试依赖类属性
目标是使用mockito执行单元测试。在第一个练习中,我创建了dependent class属性的dummy对象并测试了该类,它运行良好 现在我想用mockito模拟不直接测试的class属性。但是我得到了NullPointerException。请告知Java Junit Mockito测试依赖类属性,java,unit-testing,junit,mockito,Java,Unit Testing,Junit,Mockito,目标是使用mockito执行单元测试。在第一个练习中,我创建了dependent class属性的dummy对象并测试了该类,它运行良好 现在我想用mockito模拟不直接测试的class属性。但是我得到了NullPointerException。请告知 java.lang.NullPointerException at edu.uncc.ssdi.FlightReservation.<init>(FlightReservation.java:25)
java.lang.NullPointerException
at edu.uncc.ssdi.FlightReservation.<init>(FlightReservation.java:25)
at edu.uncc.ssdi.FlightReservationTest.testFlightReservation_ParaConstructor(FlightReservationTest.java:44)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.mockito.internal.junit.JUnitRule$1.evaluateSafely(JUnitRule.java:52)
at org.mockito.internal.junit.JUnitRule$1.evaluate(JUnitRule.java:43)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
航班预订
import java.util.Date;
public class FlightReservation {
public Flight flightType;
public Date flightReservationDate;
public int noOfAdult;
public int noOfChildren;
public FlightReservation() {
flightType = null;
flightReservationDate = null;
noOfAdult = 0;
noOfChildren = 0;
}
public FlightReservation(Flight flightType, Date flightReservationDate, int noOfAdult, int noOfChildren)
throws ReservationException {
super();
String flightDetails = flightType.getOriginAirport() + "-->" + flightType.getDestinationAirport();
if (flightType.getDepartureDate().compareTo(flightReservationDate) < 0)
throw new ReservationException(
"Departure Date must be atleast 1 day after Reservation/System Date " + flightDetails);
if (flightType.getArrivalDate().compareTo(flightType.getDepartureDate()) < 0)
throw new ReservationException("Arrival Date is before Departure Date" + ": " + flightDetails);
if (!checkAdultNo(noOfAdult))
throw new ReservationException("No of Adult cannot be less than 1" + ": " + flightDetails);
this.flightType = flightType;
this.flightReservationDate = flightReservationDate;
this.noOfAdult = noOfAdult;
this.noOfChildren = noOfChildren;
}
public boolean checkAdultNo(int noOfAdult) {
boolean flag = true;
if (noOfAdult < 1) {
flag = false;
}
return flag;
}
}
import java.util.Date;
公务舱机票预订{
公共航班类型;
公共日期航班预订日期;
公众舆论;
公共知识儿童;
公共航班预订(){
flightType=null;
flightReservationDate=null;
noOfAdult=0;
无子女=0;
}
公共航班预订(航班类型、日期航班预订日期、int noofdult、int noOfChildren)
抛出ReservationException{
超级();
字符串flightDetails=flightType.getOriginAirport()+“-->”+flightType.getDestinationAirport();
如果(flightType.getDepartureDate().compareTo(flightReservationDate)<0)
抛出新的ReservationException(
“起飞日期必须至少在预订/系统日期后1天”+航班详情);
如果(flightType.getArrivalDate().compareTo(flightType.getDepartureDate())<0)
抛出新的ReservationException(“到达日期早于出发日期”+:“+航班详细信息”);
如果(!checkAdultNo(noOfAdult))
抛出新的ReservationException(“成人数量不能小于1”+:“+flightDetails”);
this.flightType=flightType;
this.flightReservationDate=flightReservationDate;
this.noOfAdult=noOfAdult;
this.noOfChildren=noOfChildren;
}
公共布尔值checkAdultNo(int noofDult){
布尔标志=真;
if(noofdefault<1){
flag=false;
}
返回标志;
}
}
一旦您测试了构造函数,实际上您不需要模拟这些东西。您只需构建必要的对象即可
但是,如果您使用的是Mockito API注释,则应在类上使用@RunWith(MockitoJUnitRunner.class)注释,以使Mockito注释正常工作
PowerMock为这种特定情况提供了whenNew()方法,但我真的不建议仅为此使用它
import java.util.Date;
public class FlightReservation {
public Flight flightType;
public Date flightReservationDate;
public int noOfAdult;
public int noOfChildren;
public FlightReservation() {
flightType = null;
flightReservationDate = null;
noOfAdult = 0;
noOfChildren = 0;
}
public FlightReservation(Flight flightType, Date flightReservationDate, int noOfAdult, int noOfChildren)
throws ReservationException {
super();
String flightDetails = flightType.getOriginAirport() + "-->" + flightType.getDestinationAirport();
if (flightType.getDepartureDate().compareTo(flightReservationDate) < 0)
throw new ReservationException(
"Departure Date must be atleast 1 day after Reservation/System Date " + flightDetails);
if (flightType.getArrivalDate().compareTo(flightType.getDepartureDate()) < 0)
throw new ReservationException("Arrival Date is before Departure Date" + ": " + flightDetails);
if (!checkAdultNo(noOfAdult))
throw new ReservationException("No of Adult cannot be less than 1" + ": " + flightDetails);
this.flightType = flightType;
this.flightReservationDate = flightReservationDate;
this.noOfAdult = noOfAdult;
this.noOfChildren = noOfChildren;
}
public boolean checkAdultNo(int noOfAdult) {
boolean flag = true;
if (noOfAdult < 1) {
flag = false;
}
return flag;
}
}