How to test the inner classes by using EasyMock
I am new to the EasyMock. I need to test my class using the EasyMock, but here the problem is my class has inner class and this inner class is instatiated in the outer class's method and calling the method of inner class by passing some parameters. I am not sure how to test this class.
Below is some sample code.
Any help or suggetions are highly appreciated.
public class ServiceClass implements ServiceInterface {
public void updateUSer(USer) {
//some logic over here.
sendEmailNotice(subject, vTemplate);
}
private sendEmailNotice(subject, vTemplate) {
MimeMessagePrepator eNotice = new PrepareEmailNotice(subject, vTemplate);
MailSender.send( eNotice );
}
public class PrepareEmailNotice implements MimeMessagePrepator {
// some local variables.
public PrepareEmailNotice(subject, vTemplate) {
subject = subject;
vTemplate = vTemplate;
}
public void prepare( MimeMessage message) {
MimeMessageHealper helper = new MimeMessageHealper(message, true);
// setting the mail properties like sub开发者_C百科ject, to address, etc..
}
}
There are some things that you cannot mock with Easymock as calls to static methods and calls to constructors. You might change your code to be able to test it with Easymock because in the method sendEmailNotice there is a call that you may like to mock but you can't. A mock for the MailSender.send() call would be appropriate. We could do that creating a class that contains the call to the MailSender that could be mocked.
public class MailWrapper {
public MailWrapper () {
}
public void send ( MimeMessagePrepator eNotice) {
MailSender.send(eNotice);
}
}
You could use an instance of this class to be used in your ServiceClass.
public class ServiceClass implements ServiceInterface {
//Added as a member
private MailWrapper mw;
public ServiceClass () {
this.mw = new MailWrapper();
}
//Constructor added for test purposes
public ServiceClass (MailWrapper mw) {
this.mw = mw;
}
public void updateUSer(USer) {
//some logic over here.
sendEmailNotice(subject, vTemplate);
}
private sendEmailNotice(subject, vTemplate) {
MimeMessagePrepator eNotice = new PrepareEmailNotice(subject, vTemplate);
mw.send( prepator );
}
...
}
The test of the ServiceClass class would be like this:
public class ServiceClassTest {
@Test
public void testUpdateUser() {
String subject = "Expected subject";
String vTemplate = "Expected vTemplate";
MimeMessagePrepator eNotice = new PrepareEmailNotice(subject,vTemplate);
MailWrapper mwMock = createMock (MailWrapper.class);
//expecting the void call to the MailWrapper
mwMock.send(eNotice);
//other expectations...
replay(mwMock);
ServiceClass sc = new ServiceClass(mwMock);
sc.updateUser(new User());
verify(mwMock);
//some asserts
}
}
In the message you were asking about the inner class, but I think that the test of the inner class is contained in the test of the outer class, and you would not need to test it apart. In case PrepareEmailNotice has complex code and should be mocked, you could do changes, adding a MimeMessagePrepator member that could be passed as a parameter in the constructor like the MailWrapper. But I think that in case it has complex and have-to-be-mocked code, maybe it would not be an inner class.
Also, you could use Powermock, that allows you to mock static calls and constructor calls, in case you don't mind to change your test framework.
Hope it helps. Regards.
精彩评论