java - Mocking a List and attempting to iterate over it -
currently using mockito test method 1 of classes. class contains list, , method takes in object of same class. issue when attempt iterate on list object. i'm getting null pointer list. below see code snippets.
private shipment shipment; private shipment shipment2; @mock private order mockorder1; @mock private order mockorder2; @mock private order mockorder3; @mock private arraylist<order> mockshipmentorders; @mock private arraylist<order> mockshipmentorders2; @before public void setup(){ mockitoannotations.initmocks(this); mockshipmentorders.add(mockorder1); mockshipmentorders.add(mockorder2); mockshipmentorders2.add(mockorder3); shipment = new shipment(1, mockshipmentorders); shipment2 = new shipment(2, mockshipmentorders2); } @test public void test_mergeshipments_increasesbyonewhenashipmentofoneaddedtoashipmentordersizeoftwo(){ shipment.mergeshipments(shipment2); assertequals(3, shipment.getshipmentorders().size()); }
above can see mockito test , below class method:
public class shipment {
private long shipmentid; private list<order> shipmentorders; public shipment(long shipmentid, list<order> shipmentorders){ this.shipmentid = shipmentid; this.shipmentorders = shipmentorders; } public list<order> getshipmentorders(){ return shipmentorders; } public void mergeshipments(shipment shipment2){ list<order> existingshipment = shipment2.getshipmentorders(); (order order : existingshipment){ shipmentorders.add(order); } }
when run test getting java.lang.nullpointerexception line: (order order : existingshipment){ in mergeshipemts();
the question is; possible mock list, call list , run foreach on mocked list?
there fundamental issues why example not work , throws nullpointerexception
.
- the call
add()
on mocked list doesn't anything. void methods on mocks "no-ops" default - iterating on list using for-each syntax calls
collection.iterator()
under hood. returns null, because you've not setup mockito return else.
instead, not mock list , instead pass actual list. arrays.aslist()
convenient testing.
@before public void setup(){ mockitoannotations.initmocks(this); shipment = new shipment(1, arrays.aslist(mockorder1, mockorder2)); shipment2 = new shipment(2, arrays.aslist(mockorder3)); }
if you're determined mock list you'll have mock behaviour, i.e. making add() store , .iterator() return iterator. can done rather painfully follows. i've included demonstrate principle.
@mock private list<string> mockedlist; @before public void init() { mockitoannotations.initmocks(this); list<string> reallist = new arraylist<>(); doanswer(new answer<string>() { @override public string answer(invocationonmock invocation) throws throwable { reallist.add(invocation.getargumentat(0, string.class)); return null; } }).when(mockedlist).add(any()); when(mockedlist.iterator()).thenanswer(new answer<iterator<string>>() { @override public iterator<string> answer(invocationonmock invocation) throws throwable { return reallist.iterator(); } }); mockedlist.add("bar"); mockedlist.add("baz"); } @test public void iterateovermockedlist() { (string each : mockedlist) { system.out.println(each); } }
Comments
Post a Comment