0

I am unable to write easymock or expect for void methods.I want to writ test class for Board.Please anyone help in that..my class like this which is given below

 public class Board{
    Snmp snmp;
    Board(Snmp snmp){
    this.snmp = new Snmp();
    }
    private void readTable() throws SnmpException {
            ArrayList<String> boardOIDs = new ArrayList<String>();
            List<List<String>> valuesList = new ArrayList<List<String>>();
            List<List<String>> oidsList = new ArrayList<List<String>>();

            boardOIDs.add(OID_BOARD_INDEX);
            boardOIDs.add(OID_BOARDNAME);
            boardOIDs.add(OID_BOARDTYPE);

            //this method read and put value into valueList
            snmp.snmpGetTable(boardOIDs, oidsList,valuesList);

            s.o.p( "Value List size" +valuesList.size);

    }       
    }
shree
  • 2,745
  • 7
  • 28
  • 35

3 Answers3

2

Your constructor doesn't use the passed in snmp instance, but instead creates a new one. Why do you do that?

Board(Snmp snmp){
  this.snmp = new Snmp();
}

Should be

Board(Snmp snmp){
  this.snmp = snmp;
}

Then you can use easymock to create a mock Snmp instance and pass it to Board's constructor.

Snmp mock = createMock(Snmp.class);

Board board = new Board(mock);

To expect on void methods in easymock, you don't need to use the expect method. Just call the method on the mock when the mock is in the replay state.

So to expect a call to snmpGetTable() you just say

ArrayList<String> boardOIDs = ...
List<List<String>> valuesList =...
List<List<String>> oidsList = ...

Snmp mock = createMock(Snmp.class);

//this is the expectation
mock.snmpGetTable(boardOIDs, oidsList,valuesList);
//now replay the mock
replay(mock);

Board board = new Board(mock);

If you need to throw an Exception from a void method you can use easymock's expectLastCall()

 //this is the expectation
mock.snmpGetTable(boardOIDs, oidsList,valuesList);

expectLastCall().andThrow( new Exception(... ));

See the Easymock documentation for more details

dkatzel
  • 31,188
  • 3
  • 63
  • 67
1

You can train the mock to use an answer:

@Test
public void testReadTable() {
    Snmp snmp = createMock(Snmp.class);
    snmp.snmpGetTable(anyObject(List.class), anyObject(List.class), anyObject(List.class));

    IAnswer answer = new IAnswer() {

        @Override
        public Object answer() throws Throwable {
            List list = (List) getCurrentArguments()[2];
            list.add("a");
            return null;
        }
    };

    expectLastCall().andAnswer(answer);
    replay(snmp);

    Board board = new Board(snmp);
    board.readTable();

    verify(snmp);
}

Note that you need to fix the constructor of your Board class and make the method at least default visible or call it in any other way.

...
Board(Snmp snmp){
   this.snmp = snmp;
}

void readTable(){
...

See also the this answer: easymock-void-methods

Community
  • 1
  • 1
Spindizzy
  • 7,244
  • 1
  • 19
  • 33
  • I want to mock snmp..and I want to write testValueListSize().. but snmp class is third party source code..i wrote test case.but value list size always zero tat am getting – shree Dec 12 '13 at 13:00
  • Just for clarification: Do you want to mock the method 'snmpGetTable' and expect that the variable 'valuesList' is not empty? – Spindizzy Dec 12 '13 at 13:10
  • No. That is third party source code method which is mocked already Creatmock(Snmp.class); but I am getting value empty in 'snmpGetTable' after adding also. – shree Dec 12 '13 at 13:24
  • With 'expect' I mean that the valuesList is filled after the call to the mock. So that the last line prints out "Value List size1" or "Value List size2". Is that what you want? – Spindizzy Dec 12 '13 at 13:43
  • Yes. actually the size is 1.But am always getting 0 only even if I mocked Snmp class. :-( – shree Dec 12 '13 at 14:23
  • I updated my answer. Hope it helps now! – Spindizzy Dec 12 '13 at 14:31
  • Really thanks a lot..its very helpful..please help me to with other class ...http://stackoverflow.com/questions/20604031/how-to-write-test-method-for-void-method-in-junit-easymock-in-javalittle-diff-i – shree Dec 16 '13 at 05:21
0

You could, if you want to check the results of the output.

Your method is not, however, designed to allow easy capture of the output, because you don't pass in the output stream. You can mock System.out.println, though, and ensure it gets the correct argument.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302