我从未做过jUnit测试用例。我想知道如何做,但我只是用assertEquals()做了基本的测试用例。我不知道如何处理这个方法:
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");
}}
我从数据库中检索一个对象。然后,我用DB数据从PlanningObject类中填充一个列表。我不知道如何实现jUnit测试用例。我听说过假的?
谢谢各位!
发布于 2015-09-29 07:31:32
模拟就像假人一样。模拟DB意味着由某个Java对象来模拟它,而不是访问真正的DB (这样就避免了依赖关系)。在您的示例中,如果DummyApc7DAOFactory 是接口(请参阅抽象工厂模式),那么您可以实现接口的Junit版本,该接口返回一个DummyApc7实例,您可以对其进行测试(使用JUnit框架的assert方法)。
为此,您必须重新设计您的代码(并确保LogCvaultImport不会造成任何麻烦)。当涉及到单元测试时,在应用程序代码中包含静态方法的类始终是您希望避免的另一个依赖源。
在适当的TDD (测试驱动开发)方法中,您将使用DummyApc7DAOFactory的一个单元测试友好实例来设置Apc7Engine,该实例在retrievePlannings()中进行类似于dummyApc7DAOFactory.getDAO().getDummyApc7();的调用(请注意,这不再是一个抽象的方法调用了)。附加代码如下所示:
//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());
//...
}如果重新设计代码不是一个选项,Mockito可能会有所帮助。在这里,您不必模拟DB,而是对DummyApc7DAOFactory类的mehod调用进行存根处理。但是,也有一些限制: Mockito不能存根、最终或匿名类。如果是这样的话,你必须重新设计代码。如果不是,一个快速而肮脏的解决方案可能如下所示:
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());
//...
}
}https://stackoverflow.com/questions/32838017
复制相似问题