3 Replies Latest reply on Apr 9, 2015 12:26 PM by mlybarger

    unit testing sca binding

    mlybarger

      i have the following setup.  i have two composites. and one references a component on the other via an sca binding.  Foo(foo.jar) ,  Foo  has sca reference to Bar. 

       

      Bar (bar.jar), BarBean exposed by SCA as Bar.

       

      I want to unit test Foo and have been looking at how to mock Bar, but I'm just not getting there.

       

        at java.lang.reflect.Method.invoke(Method.java:606)

        at org.switchyard.component.bean.ServiceProxyHandler.handle(ServiceProxyHandler.java:163)

        ... 95 more

      Caused by: org.switchyard.HandlerException: SWITCHYARD039604: Service reference {urn:com.example.switchyard:bar:1.0}Bar not found in domain org.switchyard.domains.root

        at org.switchyard.component.sca.SCAInvoker.invokeLocal(SCAInvoker.java:114)

       

      Here's the FooBean

      @Service(Foo.class)

      public class FooBean implements Foo {

       

       

        Logger LOG = Logger.getLogger(FooBean.class);

       

       

        @Inject

        @Reference

        private Bar bar;

       

        @Override

        public void service(String message) {

        LOG.info("message:"+ message);

        ComplexMessage c = new ComplexMessage();

        c.setMessage(message);

        bar.complexMessage(c);

        }

       

       

      }

      And the test:

       

      @RunWith(SwitchYardRunner.class)

      @SwitchYardTestCaseConfig(config = SwitchYardTestCaseConfig.SWITCHYARD_XML, mixins = {

        CDIMixIn.class, NamingMixIn.class })

      public class FooBeanTest {

       

       

        private SwitchYardTestKit testKit;

        @ServiceOperation("Foo")

        private Invoker service;

       

        @Before

        public void before() {

        testKit.registerInOutService("urn:com.example.switchyard:bar:1.0");

        testKit.getServiceDomain().registerService(new QName("urn:com.example.switchyard:bar:1.0"), new InOutService(),

        new MockHandler());

        }

       

        @Test

        public void testOne() {

        service.sendInOut("message");

        }

       

       

      }

       

      here's some perhaps relevant sy.xml stuff.

          <sca:component name="FooBean">

            <bean:implementation.bean class="com.example.switchyard.foo.FooBean"/>

            <sca:service name="Foo">

              <sca:interface.java interface="com.example.switchyard.foo.Foo"/>

            </sca:service>

            <sca:reference name="Bar">

              <sca:interface.java interface="com.example.switchyard.bar.Bar"/>

            </sca:reference>

          </sca:component>

          <sca:service name="Foo" promote="FooBean/Foo">

            <sca:interface.java interface="com.example.switchyard.foo.Foo"/>

            <file:binding.file name="file1">

              <file:directory>/tmp</file:directory>

              <file:fileName>foo</file:fileName>

              <file:consume/>

            </file:binding.file>

          </sca:service>

          <sca:reference name="Bar" multiplicity="0..1" promote="FooBean/Bar">

            <sca:interface.java interface="com.example.switchyard.bar.Bar"/>

            <sca:binding.sca sy:targetNamespace="urn:com.example.switchyard:bar:1.0" name="sca1"/>

          </sca:reference>


      Any insights into how I should mock Bar when testing Foo?  It works fine when all the components are deployed inside FSW, but I want to unit test of course.

        • 1. Re: unit testing sca binding
          mlybarger

          fyi. my sample code is on github.  here's how to recreate the issue:

           

          git clone https://github.com/mlybarger/foo.git

          git clone https://github.com/mlybarger/bar.git

          cd bar && mvn install jboss-as:deploy

          cd ../

          cd foo && mvn test

           

          Notice test failure. The test shouldn't fail.

          • 2. Re: unit testing sca binding
            igarashitm

            Hi Mark,

            And the test:

             

            @RunWith(SwitchYardRunner.class)

            @SwitchYardTestCaseConfig(config = SwitchYardTestCaseConfig.SWITCHYARD_XML, mixins = {

              CDIMixIn.class, NamingMixIn.class })

            public class FooBeanTest {

             

             

              private SwitchYardTestKit testKit;

              @ServiceOperation("Foo")

              private Invoker service;

             

              @Before

              public void before() {

              testKit.registerInOutService("urn:com.example.switchyard:bar:1.0");

              testKit.getServiceDomain().registerService(new QName("urn:com.example.switchyard:bar:1.0"), new InOutService(),

              new MockHandler());

             

             

            What if you just do this istead:

            testKit.removeService("Bar");

            final MockHandler greetingService = testKit.registerInOutService("Bar");

            Also you may want to take a look at CamelJMSBindingTest in the camel-jms-binding quickstart for mock up example.

             

            hth,

            Tomo

            1 of 1 people found this helpful
            • 3. Re: unit testing sca binding
              mlybarger

              this worked. thank you.