5 Replies Latest reply on Aug 27, 2013 7:35 AM by Keith Babo

    Test Single Component

    noone100 Newbie

      Hi

       

      I would like to test a single component. For that I created an invoker for component 1 and mocked component 2 which follows on component 1. Due to the fact that there are transformations between component 1 and 2, the test has to handle that as well. This seems not to be very unit-testing-like.

       

      So I tried different solutions which all of them seem not to be very handy:

      1. remove transformations at runtime. This leads to error-prone refactorings.
      2. create a new switchyard.xml which only contains the relevant things for the test. This leads to redundant config.
      3. include everything that happens between component 1 and component 2 in the test. This leads to unnecessary big tests.

       

       

      Is there a chance to get a single component tested the right way (e.g. mock its reference)?

       

      Thanks in advance!

      MIchael

        • 1. Re: Test Single Component
          Keith Babo Master

          Generally 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.


          1 of 1 people found this helpful
          • 2. Re: Test Single Component
            noone100 Newbie

            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
              Keith Babo Master

              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 Newbie

                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
                  Keith Babo Master

                  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.