Mocking bean with constructor arguments

1,651 views
Skip to first unread message

Karthik Krishnan

unread,
Feb 3, 2010, 9:30:10 PM2/3/10
to mockito
How do we mock constructor with arguments? I have a service bean that
accepts another bean populated using string constructor. Based on that
state operations are performed and value object is returned.

public class Bean {

private final String state;

public class Bean(String state) {
this.state= state;
}

public VO process() {
// Process based on state.
// Return value object
}
}

public class Service {
private final Bean bean;

Service(Bean bean) {
this.bean = bean;
}
}

Mockito code

Bean bean = Mockito.mock(Bean.class //, not sure what to do);
// Stub behaviour.

Any help would be appreciated.

James Carr

unread,
Feb 3, 2010, 10:02:26 PM2/3/10
to moc...@googlegroups.com
Don't do anything? It just works! :)


Bean bean = mock(Bean.class);
when(bean.process()).thenReturn(somethingThatShouldBeNamedVO);

Thanks,
James

> --
> You received this message because you are subscribed to the Google Groups "mockito" group.
> To post to this group, send email to moc...@googlegroups.com.
> To unsubscribe from this group, send email to mockito+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mockito?hl=en.
>
>

Kartik Kumar

unread,
Feb 5, 2010, 5:01:59 AM2/5/10
to moc...@googlegroups.com
It works. Thanks for the tip.
Reply all
Reply to author
Forward
0 new messages