Hi I writing test cases for the application and I am using jmock to mock the objects, but I am not able to do so . Please help:
Here is the Restful webservice implementation. Now I would like to write junit test case. In the following class "BusinessClass" I want to mock the part " partnerService.validatePartner(QuoteRQ.partnerid, transactionId)" and for that I have written mock class called " PartnerServiceMock" which will be injected at runtime.
The problem that I am facing is while running the junit test case, instead of mock object, actual code is getting executed, i.e., mock object is not getting injected at runtime. Please help.
Following is the code snippet
public interface BaseMockInjector {
public void injectMock(List<Class> clist);
public void injectMock(Class c);
}
public abstract class BusinessServiceBaseMockInjector implements BaseMockInjector {
AbstractMock abs = null;
PartnerService partnerService = new PartnerService();
try {
abs = (AbstractMock) Class.forName("test.chartis.tguard.stdoff.service." + c.getSimpleName() + "Mock").newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
abs.mock(context, mockSingleton);
public void injectMock(List<Class> clist) {
Mockery context = new JUnit4Mockery() {
{
setImposteriser(ClassImposteriser.INSTANCE);
}
};
try{
Class c = BusinessClass.class;
Field[] fields = c.getFields();
for(Field field : fields){
Class cls = field.getClass();
Object mock = context.mock(cls);
this.mock(context, mock, c);
if(field.getName().equalsIgnoreCase("partnerService"))
System.out.println("partnerService");
else if(field.getName().equalsIgnoreCase("isoCountryService"))
System.out.println("isoCountryService");
else if(field.getName().equalsIgnoreCase("uwCountryService"))
System.out.println("uwCountryService");
else if(field.getName().equalsIgnoreCase("agencyService"))
System.out.println("agencyService");
else if(field.getName().equalsIgnoreCase("productDTOService"))
System.out.println("productDTOService");
}
}catch(Exception e){
}
}
}
Test Class:
===========
public class TestBusinessClass extends BusinessServiceBaseMockInjector {
List<Class> classes = null;
@Before
public void test() throws Exception {
List<Class> classes = new ArrayList<Class>();
classes.add(PartnerService.class);
injectMock(classes);
}
@Test
public final void testExecuteRule() {
BusinessClass business = new BusinessClass();
QuoteRQ quoteRQ = new QuoteRQ();
quoteRQ.setTimestamp(today);
quoteRQ.setTransactionID("Scenario2Cycle206282012");
business .executeRule(quoteRQ);
}
}
Class under Test:
===================
@Path("/rule")
@Singleton
public class BusinessClass{
public PartnerService partnerService = new PartnerService();
@POST
@Path("/executeRule")
@Produces("application/xml")
@Consumes("application/xml")
public Response executeRule(QuoteRQ quoteRQ) {
// some code here
//now the following line will have a call to a web service
if (isPartnerNotNull) {
try {
isPartnerValid = partnerService.validatePartner(QuoteRQ.partnerid, transactionId);
} catch (ServiceException e1) {
logger.info("[" + transactionId + "ServiceException while validating partner Id");
}
}
}
}
Mock class for PartnerService:
==============================
public class PartnerServiceMock extends AbstractMock {
@Override
public void mock(Mockery context, Object obj) {
final PartnerService partnerService = (PartnerService) obj;
try {
final boolean isPartnerValid = true;
context.checking(new Expectations() {
{
atLeast(1).of(partnerService).validatePartner("QABHIAIR", "Scenario2Cycle206282012");
will(returnValue(isPartnerValid));
}
});
} catch (ServiceException serviceException) {
serviceException.printStackTrace();
}
}
}