-
1. Re: Test Single Component
kcbabo Aug 20, 2013 1:41 PM (in response to noone100)1 of 1 people found this helpfulGenerally speaking, transformation shouldn't lead to bigger tests since the transformation is handled outside of your test logic. If you are referring to the amount of work that happens under the covers in any test, then yes it's testing more than just the component calling the reference if you simply replace the existing service with a mock.
What are you using to mock component 2? The replaceService() method will use the existing contract for component 2. You could unregister the existing service and then register a mock service with the consumer's (component 1) contract, which would remove the need for transformation. Post a sample unit test and switchyard.xml for your app and we can provide specific advice.
-
2. Re: Test Single Component
noone100 Aug 21, 2013 10:02 AM (in response to kcbabo)Component 1 is a Camel route which does not care much about the message type that is being processed:
{code}
public void configure() {
from("switchyard://Component1Service").doAggregatation().doSomethingElse().to("switchyard://Component2Service");
}
{code}
The transformations defined in switchyard.xml perform a transformation between component 1 and component 2. So in tests I would have to pass messages (objects) into component 1 which are capable to get tranformed even though I'm only interested in testing the functionality between from() and to().
My current approach for testing a single camel component is mocking the SY's "direct" endpoint to get rid of the transformation:
{code}
protected MockEndpoint createCamelMockEndpoint(String switchYardServiceName) {
SwitchYardCamelContext camelContext = getCamelContext();
String endpointString =
"direct:"
+ testKit.createQName(switchYardServiceName).toString().replaceAll("\\{", "%7B")
.replaceAll("\\}", "%7D");
camelContext.addRegisterEndpointCallback(new InterceptSendToMockEndpointStrategy(endpointString, true));
return (MockEndpoint) camelContext.getEndpoint("mock:" + endpointString);
}
{code}
Your suggestion looks a bit similar to 1) but maybe it's a better solution than the one above.
I should rephrase my question to "how to mock a camel route's to() endpoint using the switchyard runner?".
-
3. Re: Test Single Component
kcbabo Aug 21, 2013 10:16 AM (in response to noone100)You shouldn't have to go that low level.
1) Inject SwitchYardTestKit into your unit test by defining it as an instance variable in your test class:
private SwitchYardTestKit testKit;
2) Remove the current service that's registered for component2 in your test method:
testKit.removeService("Component2Service");
3) Register a mock service:
MockHandler mock = testKit.registerInOnlyService("Component2Service");
4) Profit!
-
4. Re: Test Single Component
pzl_mz Aug 27, 2013 4:28 AM (in response to kcbabo)I'm not able to log in to my account anymore. I always get an "An unexpected error has occurred" on all threads. That's why I can't set the thread to answered.
But your suggestions works as expected.
To summarize: Use removeService/registerInOnlyService to drop the contract and its resulting transformation.
-
5. Re: Test Single Component
kcbabo Aug 27, 2013 7:35 AM (in response to pzl_mz)You can email help@jboss.org if you are having difficulty with your other account. Thanks very much for taking the time to post back with your results - that helps everyone in the community.