4 Replies Latest reply on Dec 7, 2012 10:33 AM by Kostas Papageorgiou

    Registering a service using SwitchYardTestKit

    Kostas Papageorgiou Newbie

      Hello,

       

      in my project we are now moving from switchyard 0.5.0.Final to 0.6.0.Final. The transition was smooth in general, however we have some tests that were succeeding with version 0.5 are now failing with 0.6.

      More particular in some tests we are using SwitchYardTestKit to mock some InOnly services. In 0.5 it was enough to register a new Service with the same name as the real service- SwitchYardTestKit.registerInOnlyService("serviceName")- and the real service would be replaced from a mock.

      In 0.6 however this does not seem to work - the real implementation is still being called.

       

      I tried removing the service first and then registering the service the same way, but now the transformers don't kick in.

      I did some debugging and my assumption is that when I remove the service from the kit all information regarding the service is gone - including the interface . Trasformers don't kick in this case because there is no consumer contract.

       

      Would you confirm that this is the case?

      Is there a workaournd that allows to mock switchyard services using SwitchYardTestKit?

        • 1. Re: Registering a service using SwitchYardTestKit
          Keith Babo Master

          The short answer is that you now need to do this:

          https://github.com/jboss-switchyard/quickstarts/blob/master/camel-jms-binding/src/test/java/org/switchyard/quickstarts/camel/jms/binding/CamelJMSBindingTest.java#L61

           

          The long answer is that we made some changes to the internal addressing of SY core and tightened things up a bit.  In 0.5 the ability to replace a service in the test kit was a happy accident based on the way that dispatchers were created.  In reality, it's possible to have multiple implementations of a given service and the current behavior of 0.6 is correct in allowing both to be registered and choosing one.  One solution is to simply remove the original registration as part of your test as in the example linked above.  Another option, depending on what you are trying to assert in your test, is to introduce an Auditor to intercept the processing of the exchange.

           

          Sorry this bit you in the move from 0.5 to 0.6, but I hope the above is both clear and functional for your use case.

           

          cheers,

          keith

          1 of 1 people found this helpful
          • 2. Re: Registering a service using SwitchYardTestKit
            Kostas Papageorgiou Newbie

            Hello Keith,

             

            thank you for the suggestions.

            Regarding the actual tests, as I mentioned above I have already followed the quickstart example ( removing & then registering the service) but got some issues with the transformers. I am attaching a small example similar to the issue I am facing. Is my example failing due to some configuration error from my side, or some misuse of the SwitchYardTestKit? Any help will be appreciated.

             

            If no other way we will move on using the Auditors as you suggest. I would like to avoid them though, cause Auditors are powerful tools and using them for assertions in tests is like "using a sledgehammer to crack a nut".As I see it some SwitchYardTestKit enhancements that would allow easier mocking of switchyard services , would be more handy for such a case.

             

            Regards,

            Kostas

            • 3. Re: Registering a service using SwitchYardTestKit
              Kostas Papageorgiou Newbie

              I am re - attaching the example due to a problem in the switchyard.xml of the previous one. Sorry for any inconvenience.

              • 4. Re: Registering a service using SwitchYardTestKit
                Kostas Papageorgiou Newbie

                Eventually I got the example running.

                 

                Some of the issues that would cause the test to fail,  were solved based on solutions discussed here: https://community.jboss.org/thread/214238

                 

                Regarding the mock-specific part, I got the transformers to work for mocked services using a slightly approach :

                 

                public class CamelServiceTest {

                ....

                SwitchYardTestKit testkit;

                ....

                testKit.removeService("StringPrinter");

                MockHandler handler = new MockHandler();

                testKit.registerInOutService("StringPrinter", handler, JavaService.fromClass(StringPrinter.class));

                ....

                }

                 

                Registering the service by providing the Java Interface allows transformation to work fine even in the mocked environment.

                This is only possible when registering mocked services as InOut. For some reason the SwitchYardTestKit doesn't offer the same  functionality for InOnly services, but this doesn't seem to affect the test in any way.

                 

                I'm attaching the solution to the above example in .zip format.