简体   繁体   English

Junit Mockito测试依赖类属性

[英]Junit Mockito Test Dependent Class Attribute

The objective is to perform Unit Testing Using mockito. 目的是使用Mockito执行单元测试。 In the first excercise, I created dummy object of dependent class attribute and tested the class, It works fine. 在第一个练习中,我创建了依赖类属性的伪对象并测试了该类,它工作正常。

Now I want to mock the class attribute not directly under test using mockito. 现在,我想不使用Mockito直接模拟类属性。 But I am getting NullPointerException. 但是我正在收到NullPointerException。 Kindly advise. 好心提醒。

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

    }

Once you are testing constructors you don't need, in fact, to mock the stuffs. 实际上,一旦测试了构造函数,就不需要模拟这些东西了。 You can just build the necessary object and that's it. 您可以仅构建必要的对象,仅此而已。

However, if you are using Mockito API annotations, you should use @RunWith(MockitoJUnitRunner.class) annotation on your class to Mockito annotations work normally. 但是,如果使用的是Mockito API批注,则应在类上使用@RunWith(MockitoJUnitRunner.class)批注,以使Mockito批注正常工作。

PowerMock have whenNew() method for this specific case, but I really don't recommend it just for that. PowerMock在这种情况下具有whenNew()方法,但我确实不建议这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM