简体   繁体   中英

Junit Mockito Test Dependent Class Attribute

The objective is to perform Unit Testing Using 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. But I am getting 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.

PowerMock have whenNew() method for this specific case, but I really don't recommend it just for that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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