如何使用EasyMock爲getBoards方法編寫JUnit(下面在示例中提到)。我試過但無法通過在EasyMock中使用JUnit來覆蓋代碼。我已經簡單多了一個環節「https://stackoverflow.com/questions/20604031/how-to-write-test-method-for-void-method-in-junit-easymock-in-javalittle-diff-i」如何在java中使用easymock編寫junit?
public class DCI implements ...{
private Device device = null;
private SnmpUtils snmp = null;
DCM(Device device){
this.device = device;
}
@override
void openCommun(){
snmp = new SnmpUtils(device);
snmp.openSnmpComm();
}
// ---> How to write Junit test with easymock for this method?
public List<Board> getBoards(DeviceIdn deviceIdn) throws SnmpException {
List<Board> boardList = new ArrayList<Board>();
try {
//BoardTableClass --> Below given
BoardTable boardTable = new BoardTable(snmp);
boardTable.readTable();
for (int row = 0; row < boardTable.size(); row++) {
String strBoardIndex = boardTable.getValue(row, BoardTable.BoardColumn.BoardIndex);
String strBoardName = boardTable.getValue(row, BoardTable.BoardColumn.BoardName);
String strBoardType = boardTable.getValue(row, BoardTable.BoardColumn.BoardType);
int boardIndex = new Integer(strBoardIndex);
BoardIdn boardIdn = new BoardIdn(deviceIdn, boardIndex);
Board board = new Board(boardIdn);
board.setName(strBoardName);
board.setType(strBoardType);
boardList.add(board);
}
logger.info(boardList.size());
}
//In handleException method , snmpException checked
catch (Exception e) {
handleException(e);
}
return boardList;
}
}
這種佈局並不完全有利於嘲笑。你將一個實例變量傳遞給你正在構建的對象。除非你想模擬實例變量,否則嘲笑實際上很少。 – Makoto
嗨,謝謝。但是哪一個在這裏嘲笑.. – shree