Nav
You are viewing an older version of this section. Click here to navigate to the latest version.

Using Dynamic Ports in Mule Test Cases

If you find yourself hitting test failures due to ports not being available during testing, using the dynamic ports testing framework should fix your issue.

Pre-requisites:

  1. You need to know how many ports you test will use. Count the number of unique ports in your mule-config file(s) for a given test class.

  2. Your test class extends FunctionalTestCase.

  3. You are ok with the ports being between 5000-6000

Enabling dynamic ports

Adding dynamic ports to your tests is pretty simple:

  1. Wherever your test extends FunctionalTestCase, extend DynamicPortTestCase instead

  2. Define a method getNumPortsToFind and return an int with the number of ports you need for testing:

    
               
            
    1
    2
    3
    4
    
    protected int getNumPortsToFind()
        {
            return 1;
        }
  3. Replace the hard-coded ports in your mule config files with incremental parameters, starting with ${port1}, i.e.

    
               
            
    1
    2
    
    <http:endpoint name="clientEndpoint1" host="localhost" port="${port1}"
            path="test1/?foo=boo&amp;far=bar" exchange-pattern="request-response"/>
  4. If your test class uses hardcoded port numbers, you have a couple of ways of updating it:

    1. If you are using a Mule api call like client.send, add the name parameter to your endpoint and use that to refer to the endpoint, i.e.

      
                    
                 
      1
      2
      3
      
      MuleMessage result = client.send(((InboundEndpoint) client.getMuleContext().getRegistry()
                  .lookupObject("inMain")).getAddress(),
                  msg, null);
    2. If you need access to the dynamic port number, use the AbstractMuleTestCase.getPorts() method, where index 0 maps to ${port1}, index 1 maps to ${port2}, etc:

      
                    
                 
      1
      
      getPorts().get(1)