1

I never did jUnit test cases. I looked for how to do but I've just done basics test cases with assertEquals(). I do not know how to do for this method :

public class Apc7Engine extends BaseEngine {

/**
 * This method retrieve plannings 
 * in APC7 configuration
 * 
 * It is an implementation of an abstract method
 * from BaseEngine.java
 *
 */
@Override
public void retrievePlannings() {
    LogCvaultImport.code(200).debug("A7: start retrievePlannings");
    try {
        List importList = DummyApc7DAOFactory.getDAO().getDummyApc7();
        Iterator poIterator = importList.iterator();

        while(poIterator.hasNext()) {
             DummyApc7 dummy = (DummyApc7) poIterator.next();
             PlanningObject planning = new PlanningObject();
             planning.setAchievedDate(dummy.getLastUpdate());
             planning.setAircraftType(dummy.getAcType());
             planning.setBaselineDate(dummy.getLastUpdate());
             planning.setDeliverySite(dummy.getDeliverySite());
             planning.setEventId(dummy.getEvtId());
             planning.setEventName(dummy.getEvent());
             planning.setEventStatus(dummy.getEvtStatus());
             planning.setLastUpdate(dummy.getLastUpdate());
             planning.setModel(dummy.getModel());
             planning.setMsn(dummy.getMsn());
             planning.setOperator(dummy.getOperator());
             planning.setOwner(dummy.getOwner());
             planning.setProgram(dummy.getProg());
             planning.setSerial(dummy.getSerial());
             planning.setTargetDate(dummy.getLastUpdate());
             planning.setVersion(dummy.getVersion());
             planning.setVersionRank(dummy.getVersionRank());
             LogCvaultImport.code(800).info("A7|Event name: "+planning.getEventName()+" - MSN: "+planning.getMsn()+" - Delivery site: "+planning.getDeliverySite());
             listPlanningObject.add(planning);        
        }
    } catch (DAOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    LogCvaultImport.code(1000).debug("A7: end retrievePlannings");
}

}

I retrieve an object from the DB. Then I fill a List from the PlanningObject class with the DB data. I do not have any idea how to realize jUnit test cases about it. I heard about mock?

Thanks guys !

2
  • You should have a look at Mockito. A popular framework for creating JUnit tests. you'll find plenty of examples. mockito.org Commented Sep 29, 2015 at 7:31
  • Thanks man, I'm watching it :) It seems to be really helpful ! Commented Sep 29, 2015 at 8:12

1 Answer 1

1

A mock is a something like a dummy. To mock a DB would mean to simulate it by some Java object instead of accessing the real DB (thus avoiding dependencies). In your case, if DummyApc7DAOFactory would be an interface (see Abstract Factory pattern), then you could implement a Junit version of the interface which returns an instance of DummyApc7 with values you can test against (using assert methods of the JUnit framework).

For this to work, you have to redesign your code accordingly (and make sure that LogCvaultImport does not cause any trouble). Classes with static methods in your application code are always another source of dependencies you would want to avoid when it comes to unit testing.

In proper TDD (test driven development) approach you would set up Apc7Engine with a unit testing friendly instance of DummyApc7DAOFactory which does the call in retrievePlannings() like this dummyApc7DAOFactory.getDAO().getDummyApc7(); (note that this is not an abstract method call, anymore). The additional code would look like this:

//AbstractDummyApc7DAOFactory.java
public class AbstractDummyApc7DAOFactory {
    /** @param real true, for real DAOFactory, false for Junit testing*/
    public static DummyApc7DAOFactory create(boolean real) {
        if (real) {
            //create and return the real DAOFactory object
        }
        else {
            //return dummy implementation for Junit testing, better define in separate class
            return new DummyApc7DAOFactory() {
                public DummyApc7DAO getDAO() {
                    return new DummyApc7DAO() {
                        public List getDummyApc7() {
                            List dummyList = new ArrayList();
                            testApc7 = new DummyApc7();
                            testApc7.setVersion("1.Unit.Test");
                            //....
                            dummyList.add(testApc7);
                            return dummyList;
                        }
                    };
                }
            };
        }
    }
}

//test code in junit test class
@Test
public void testRetrievePlannings() {
    DummyApc7DAOFactory fac = AbstractDummyApc7DAOFactory.create(false);
    testObj.setDummyApc7DAOFactory(fac);
    testObj.retrievePlannings();
    PlanningObject testPO = test.getListPlanningObject().get(0);
    assertEquals(testApc7.getVersion(), testPO.getVersion()); 
    //...
}

If redesigning of the code is not an option, Mockito might help. Here you do not have to mock the DB but stub the mehod call of your DummyApc7DAOFactory class. There are limitations however: Mockito cannot stub final or anonymous classes. If that is the case, you have to redesign the code. If not, a quick and dirty solution might look like this:

public class  RetrievePlanningsTest {       
        private DummyApc7 testApc7;
        private Apc7Engine testObj = new Apc7Engine();

        @Before
        public void setUp() {
            DummyApc7DAOFactory mockedObj = mock(DummyApc7DAOFactory.class);
            List dummyList = new ArrayList();
            testApc7 = new DummyApc7();
            testApc7.setVersion("1.Unit.Test");
            //....
            dummyList.add(testApc7);
            when(DummyApc7DAOFactory.getDAO().getDummyApc7()).thenReturn(dummyList);
        }

        @Test
        public void testRetrievePlannings() {
            testObj.retrievePlannings();
            PlanningObject testPO = testObj.getListPlanningObject().get(0);
            assertEquals(testApc7.getVersion(), testPO.getVersion()); 
            //...
        }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks ! I'll take a look to this. I'll be back with test cases :)
@Majestic I added sketches of a TDD-style and Mockito solution.
This is amazing what you show me ! Thanks a lot ! :D I did not get what means your variable called "test" there :**test**.getListPlanningObject().get(0);.
@Majestic That was a typo. Anyway, this line is a assumption. There should be a way to get the list of planning objects from Apc7Engine. Otherwise retrievePlanning would make no sense because it returns void. If any of the sketched solutions is helpful, please mark it by clicking the upper arrow.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.