You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by catequil <br...@yahoo.com> on 2016/11/04 15:42:28 UTC
CamelTestSupport best way to unit test the OUT or result of the
route
Using CamelTestSupport how do you test the out, or result of the route when
it does not end in a endpoint call? Would I just put in my production route
code a .to("mock:result) for each API call/route and intercept it or is
there a better way that does not put a mock:endpoint in my production route
code?
code example:
@Path("/v1")
@Api(value = "/v1", description = "My API")
class RestController {
@GET
@Path("/rest/service")
@Produces("application/json")
public Response someRestService() {
return producerTemplate.requestBody("direct:someCamelEndpoint",
someRestBody);
}
}
class CamelRoute extends RouteBuilder {
@Override
public void configure() throws Exception {
from("direct:someCamelEndpoint").routeId("myRouteId")
.to("direct:someOtherEndpoint")
.process(new MyJsonResponseProcessor())
//.to("mock:result") ???
.end();
}
}
class CamelUnitTests extends CamelTestSupport {
@Override
protected AbstractApplicationContext createApplicationContext() {
return new ClassPathXmlApplicationContext("camel-config.xml");
}
@Override
public boolean isUseAdviceWith() {
return true;
}
@Test
public void testSomeOtherEndpoint() throws Exception {
RouteDefinition route = context.getRouteDefinition("myRouteId");
route.adviceWith(context, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
this.interceptSendToEndpoint("direct:someOtherEndpoint")
.skipSendToOriginalEndpoint();
mockEndpoints("mock:direct:someOtherEndpoint");
}
});
context.start();
MockEndpoint mock =
getMockEndpoint("mock:direct:someOtherEndpoint");
mock.expectedMessageCount(1);
mock.assertIsSatisfied();
}
@Test
public void testOuputAfterProcessor() {
//Test the result going back to the initial rest call.
}
}
--
View this message in context: http://camel.465427.n5.nabble.com/CamelTestSupport-best-way-to-unit-test-the-OUT-or-result-of-the-route-tp5789761.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: CamelTestSupport best way to unit test the OUT or result of the
route
Posted by Steve973 <st...@gmail.com>.
catequil: Sure. My comment was more about the nature of testing
Camel-related things and how it differs from pure unit testing. Unit tests
mock everything outside of the unit being tested. With a Camel route, you
cannot unit test because you need other elements of Camel in order for the
test to actually do anything. It's likely that you are not doing anything
wrong, but if some of your team members are purists, they might argue with
you about calling these things unit tests.
--
View this message in context: http://camel.465427.n5.nabble.com/CamelTestSupport-best-way-to-unit-test-the-OUT-or-result-of-the-route-tp5789761p5789913.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: CamelTestSupport best way to unit test the OUT or result of the
route
Posted by catequil <br...@yahoo.com>.
@Steve973
Can you elaborate on your comment? I don't understand why that would be the
case.
Why have the "skipSendToOriginalEndpoint" if a strict unit test was not
intended?
I'm new to Camel so if you could help me understand. I would like to
correctly implement testing in camel.
--
View this message in context: http://camel.465427.n5.nabble.com/CamelTestSupport-best-way-to-unit-test-the-OUT-or-result-of-the-route-tp5789761p5789792.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: CamelTestSupport best way to unit test the OUT or result of the route
Posted by Steve973 <st...@gmail.com>.
Just a note that doesn't help you directly, but camel route tests can never
be unit tests. They are always integration tests.
On Nov 4, 2016 4:23 PM, "catequil" <br...@yahoo.com> wrote:
> I applogize, I just missed that. I actually do have that in my test. I've
> kinda got a mix of an example of what I have in prod and the test code.
>
> right after context.start();
> i have:
>
>
> template.request("direct:someCamelEndpoint", (Exchange exchange)
> ->
> setExchangeProperties(exchange, someRequestObject));
>
> I understand the intercepts pretty good and that all works and the test
> runs
> fine. What I am struggling to test is the eventual result at the end of
> the
> route that in production would normally get sent as the response to the
> inital rest request that called it in the first place. I could in fact
> place a to("mock:result") in the actual prod route and then grab the mock
> of
> that endpoint to see what is in the exchange at that point, but I am not
> sure if that is the best way to go about it as there would be this mock
> call
> that does nothing in the production code. My question is how do I test
> what
> is coming out the end of the route?
>
> Thanks
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.
> com/CamelTestSupport-best-way-to-unit-test-the-OUT-or-result-of-the-route-
> tp5789761p5789789.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>
Re: CamelTestSupport best way to unit test the OUT or result of the
route
Posted by catequil <br...@yahoo.com>.
I applogize, I just missed that. I actually do have that in my test. I've
kinda got a mix of an example of what I have in prod and the test code.
right after context.start();
i have:
template.request("direct:someCamelEndpoint", (Exchange exchange) ->
setExchangeProperties(exchange, someRequestObject));
I understand the intercepts pretty good and that all works and the test runs
fine. What I am struggling to test is the eventual result at the end of the
route that in production would normally get sent as the response to the
inital rest request that called it in the first place. I could in fact
place a to("mock:result") in the actual prod route and then grab the mock of
that endpoint to see what is in the exchange at that point, but I am not
sure if that is the best way to go about it as there would be this mock call
that does nothing in the production code. My question is how do I test what
is coming out the end of the route?
Thanks
--
View this message in context: http://camel.465427.n5.nabble.com/CamelTestSupport-best-way-to-unit-test-the-OUT-or-result-of-the-route-tp5789761p5789789.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: CamelTestSupport best way to unit test the OUT or result of the
route
Posted by Craig Washington <Cr...@aexp.com.INVALID>.
There are many ways to test your production camel routes – you’ve included a few with Mock and AdviceWith.
With this approach you shouldn’t need to include any mock endpoints in your prod route.
Instead, in your test you instructing camel to advice the prod route with your mock endpoints.
Lastly (assuming your mock/advice is setup correctly) you’re missing the actual route invocation for the test.
Something like the following invokes the route and returns the result:
Object response = template.requestBody("direct:someCamelEndpoint ", "blah");
More info here: http://camel.apache.org/mock.html
On 11/4/16, 11:42 AM, "catequil" <br...@yahoo.com> wrote:
Using CamelTestSupport how do you test the out, or result of the route when
it does not end in a endpoint call? Would I just put in my production route
code a .to("mock:result) for each API call/route and intercept it or is
there a better way that does not put a mock:endpoint in my production route
code?
code example:
@Path("/v1")
@Api(value = "/v1", description = "My API")
class RestController {
@GET
@Path("/rest/service")
@Produces("application/json")
public Response someRestService() {
return producerTemplate.requestBody("direct:someCamelEndpoint",
someRestBody);
}
}
class CamelRoute extends RouteBuilder {
@Override
public void configure() throws Exception {
from("direct:someCamelEndpoint").routeId("myRouteId")
.to("direct:someOtherEndpoint")
.process(new MyJsonResponseProcessor())
//.to("mock:result") ???
.end();
}
}
class CamelUnitTests extends CamelTestSupport {
@Override
protected AbstractApplicationContext createApplicationContext() {
return new ClassPathXmlApplicationContext("camel-config.xml");
}
@Override
public boolean isUseAdviceWith() {
return true;
}
@Test
public void testSomeOtherEndpoint() throws Exception {
RouteDefinition route = context.getRouteDefinition("myRouteId");
route.adviceWith(context, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
this.interceptSendToEndpoint("direct:someOtherEndpoint")
.skipSendToOriginalEndpoint();
mockEndpoints("mock:direct:someOtherEndpoint");
}
});
context.start();
MockEndpoint mock =
getMockEndpoint("mock:direct:someOtherEndpoint");
mock.expectedMessageCount(1);
mock.assertIsSatisfied();
}
@Test
public void testOuputAfterProcessor() {
//Test the result going back to the initial rest call.
}
}
--
View this message in context: http://camel.465427.n5.nabble.com/CamelTestSupport-best-way-to-unit-test-the-OUT-or-result-of-the-route-tp5789761.html
Sent from the Camel - Users mailing list archive at Nabble.com.
American Express made the following annotations
******************************************************************************
"This message and any attachments are solely for the intended recipient and may contain confidential or privileged information. If you are not the intended recipient, any disclosure, copying, use, or distribution of the information included in this message and any attachments is prohibited. If you have received this communication in error, please notify us by reply e-mail and immediately and permanently delete this message and any attachments. Thank you."
American Express a ajouté le commentaire suivant le Ce courrier et toute pièce jointe qu'il contient sont réservés au seul destinataire indiqué et peuvent renfermer des
renseignements confidentiels et privilégiés. Si vous n'êtes pas le destinataire prévu, toute divulgation, duplication, utilisation ou distribution du courrier ou de toute pièce jointe est interdite. Si vous avez reçu cette communication par erreur, veuillez nous en aviser par courrier et détruire immédiatement le courrier et les pièces jointes. Merci.
******************************************************************************