How to mock 2 invocations in same method
jorgemoralespou_2 Apr 7, 2014 3:06 AMHi,
I have a Bean Component from where I do 2 invocations to same method, with different values as parameters and another use case where I have a method that calls a reference with 2 different methods, in the scope of the same Bean component's method. How can I mock it?
This question is needed for a project I'm involved in as well as a document I'm writing regarding JUnit in SwitchYard, witch tips and tricks. http://unpoucode.blogspot.com/2014/03/junit-in-switchyard.html
public class BeanA {
@Inject @Reference private myReferencedService;
public void execute(...){
...
String a = myReferencedService.test(paramsA);
...
String b = myReferencedService.test(paramsB);
...
}
}
public class BeanA {
@Inject @Reference private myReferencedService;
public void execute(...){
...
String a = myReferencedService.methodA(paramsA);
...
String b = myReferencedService.methodB(paramsB);
...
}
}
I want to mock the invocations to this methods in myReferencedService, but I haven't been able.
I have tried different options, with no success, like creating a new ExchangeHandler and trying to figure in the handleMessage which invocation it is or which methoid has been invoked.
I have also tried to register 2 different operations for the service being called, for different operations signature, and it doesn't also work.
@Test
public void test() {
final QName serviceName = new QName(
"urn:com.example.switchyard:stringtemplate-example:1.0",
"TestMethod");
for (Service s : testKit.getServiceDomain().getServices()) {
System.out.println(s);
}
testKit.replaceService(serviceName, new ExchangeHandler() {
@Override
public void handleMessage(Exchange exchange)
throws HandlerException {
Message mess = exchange.createMessage();
mess.setContent("Jorge");
exchange.send(mess);
}
@Override
public void handleFault(Exchange exchange) {
}
});
SubscriptionRequest req = new SubscriptionRequest();
SubscriptionResponse res = serviceOperationInvocation.sendInOut(req)
.getContent(SubscriptionResponse.class); // Expect Element as
// transformation is
// for XML
Assert.assertEquals("Jorge", res.getItem().get(0).getCompanyId());
}
@Test
public void testInvocation2Services() {
final QName serviceName = new QName(
"urn:com.example.switchyard:stringtemplate-example:1.0",
"TestMethod");
final QName serviceReferenceName = new QName(
"urn:com.example.switchyard:stringtemplate-example:1.0",
"testJAXBBean/TestMethod");
for (Service s : testKit.getServiceDomain().getServices()) {
System.out.println(s);
}
ExchangeHandler testHandler = new ExchangeHandler() {
@Override
public void handleMessage(Exchange exchange)
throws HandlerException {
System.out.println("---- testHandler");
Message mess = exchange.createMessage();
mess.setContent("Jorge");
exchange.send(mess);
}
@Override
public void handleFault(Exchange exchange) {
}
};
ExchangeHandler holaHandler = new ExchangeHandler() {
@Override
public void handleMessage(Exchange exchange)
throws HandlerException {
System.out.println("---- holaHandler");
Message mess = exchange.createMessage();
mess.setContent("Hola");
exchange.send(mess);
}
@Override
public void handleFault(Exchange exchange) {
}
};
InOutOperation testMethodContract = new InOutOperation("test",
JavaTypes.toMessageType(String.class), // input
JavaTypes.toMessageType(String.class), null); // output, no-fault
InOutOperation holaMethodContract = new InOutOperation("hola",
JavaTypes.toMessageType(String.class), // input
JavaTypes.toMessageType(String.class), null); // output, no-fault
// testKit.removeService(serviceName);
testKit.getDeployment().getDomain().registerServiceReference(serviceReferenceName, new InOutService(testMethodContract), testHandler);
testKit.getDeployment().getDomain().registerServiceReference(serviceReferenceName, new InOutService(holaMethodContract), holaHandler);
// testKit.registerInOutService("TestMethod", holaHandler, new InOutService(holaMethodContract));
SubscriptionRequest req = new SubscriptionRequest();
SubscriptionResponse res = serviceOperationInvocation.sendInOut(req)
.getContent(SubscriptionResponse.class); // Expect Element as
// transformation is
// for XML
Assert.assertEquals("Jorge", res.getItem().get(0).getCompanyId());
SubscriptionRequest req2 = new SubscriptionRequest();
req2.setCompanyId("Hola");
SubscriptionResponse res2 = serviceOperationInvocation.sendInOut(req2)
.getContent(SubscriptionResponse.class); // Expect Element as
// transformation is
// for XML
Assert.assertEquals("Hola", res2.getItem().get(0).getCompanyId());
}