Search code examples
javaxsltjunitmuletransformer-model

Unit testing is not working with XSLT tansformer


I have written unit test cases to test the message processors individually in my mule flow.

But the unit test fails with error

org.mule.api.transformer.TransformerMessagingException: Property "xsl-file or xsl-text" not set.  
One or more of them must be set (org.mule.api.lifecycle.InitialisationException).
 Message payload is of type: String    
 (org.mule.api.transformer.TransformerMessagingException). Message payload is of type: String

One of the transformers is an XSLT as shown below.

<mule-xml:xslt-transformer  maxIdleTransformers="2" maxActiveTransformers="5"   xsl-file="C:\EWS\myproj\src\main\resources\xslt\DataAdder.xsl"
         name="AdderXSLT"   >
    </mule-xml:xslt-transformer>

The unit test method looks as below.

    MessageProcessor subFlow = muleContext.getRegistry().lookupObject("AdderXSLT");
    MuleEvent result = subFlow.process(getTestEvent(getFileAsString("SamplePayloads/input.xml")));  

    System.out.println("The output from Event is " + result.getMessageAsString());
    System.out.println("The converted XML is " + result.getMessage().getPayloadAsString()); 

    assertNotNull(result);
    assertNull(result.getMessage().getExceptionPayload());
    assertFalse(result.getMessage().getPayload() instanceof NullPayload);

Please help me understand what's going wroong here.


Solution

  • I came across something similar before where you need to initialise the transformer explicitly when you're not executing it within the context of a flow. To test xslt transformers I have used similar to the following is the past:

    XsltTransformer xslt = FunctionalTestCase.muleContext.getRegistry()
                    .lookupObject("SuccessResponseTransformer");
            xslt.setReturnDataType(DataType.STRING_DATA_TYPE);
            xslt.initialise();
    
            String result = (String) xslt.transform(srcXML);
    

    You could try something like this or try casting to an XsltTransformer to initialise.

    I believe this is because when you execute the MP as part of a flow it is part of a MessageProcessorChain that will initialise each MP where appropriate. If you take a look at the following code from AbstractMessageProcessorChain - http://grepcode.com/file/repo1.maven.org/maven2/org.mule/mule-core/3.3.1/org/mule/processor/chain/AbstractMessageProcessorChain.java#AbstractMessageProcessorChain.initialise%28%29 :

    public void initialise() throws InitialisationException
        {
             for (MessageProcessor processor : processors)
           {
             // MULE-5002 TODO review MP Lifecycle
                if (processor instanceof Initialisable /* && !(processor instanceof Transformer) */)
               {
                    ((Initialisable) processor).initialise();
                }
            }
        }
    

    Note that (!instanceof Transformer) is commented out. So it will initialise the XsltTransformer for you.

    Where as directly referencing the MessageProcessor will not.