I would like to mock an object inside my class.
public class Controller{
private StandardEmailSender sender = new StandardEmailSender();
public void process() throws EmailException {
// do some stuff
sender.sendEmail(to, subject, body);
// do some stuff
}
}
I would like to mock sender.sendEmail(to, subject, body);. I've spent time finding a solution but I'm stuck. I tried to mock directly the object StandardEmailSender like this :
@Mock
StandardEmailSender sender;
@Before
public void setUp() throws EmailException {
MockitoAnnotations.initMocks(this);
doNothing().when(sender).sendEmail(anyString(), anyString(), anyString());
}
@Test
public void test() throws EmailException {
Controller controller= new Controller ();
controller.process();
//make some asserts
}
Would someone have a solution to my problem? Thanks!