Junit Mockito测试依赖类属性

时间:2018-04-22 13:33:57

标签: java unit-testing junit mockito

目标是使用mockito执行单元测试。在第一个练习中,我创建了依赖类属性的虚拟对象并测试了类,它工作正常。

现在我想使用mockito模拟不直接测试的class属性。但我得到NullPointerException。请建议。

        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)

FlightReservationTest

        import static org.junit.Assert.assertEquals;

        import java.text.ParseException;
        import java.text.SimpleDateFormat;
        import java.util.Date;

        import org.junit.Before;
        import org.junit.Rule;
        import org.junit.Test;
        import org.mockito.Mock;
        import org.mockito.Mockito;
        import org.mockito.junit.MockitoJUnit;
        import org.mockito.junit.MockitoRule;

        public class FlightReservationTest {

        @Mock
        private Flight tempFlight;

        private Date tempDate;

        @Rule
        public MockitoRule rule = MockitoJUnit.rule();

        @Before
        public void setup() {
            tempDate = new Date();

        }

        @Test
        public void testFlightReservation_ParaConstructor() {
            try {

                Date date1 = new SimpleDateFormat("dd/MM/yyyy").parse("26/05/2018");

                // tempFlight = new Flight("s1","s2","s3","s4",date1,date1,100);

                FlightReservation fRes1 = new FlightReservation(tempFlight, tempDate, 1, 1);

                Mockito.when(tempFlight.getDepartureDate()).thenReturn(date1);
                Mockito.when(tempFlight.getArrivalDate()).thenReturn(date1);
                Mockito.when(tempFlight.getOriginAirport()).thenReturn("s1");
                Mockito.when(tempFlight.getDestinationAirport()).thenReturn("s1");

                assertEquals(tempFlight, fRes1.flightType);
                assertEquals(tempDate, fRes1.flightReservationDate);
                assertEquals(1, fRes1.noOfAdult);
                assertEquals(1, fRes1.noOfChildren);

            } catch (ReservationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

        }

FlightReservation

     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;
        }

    }

1 个答案:

答案 0 :(得分:0)

一旦你测试了构造函数,你实际上并不需要模拟这些东西。你可以构建必要的对象,就是这样。

但是,如果您使用的是Mockito API注释,则应在类上使用 @RunWith(MockitoJUnitRunner.class)注释,以便Mockito注释正常工作。

对于这个特定情况,PowerMock有 whenNew()方法,但我真的不建议只为此。