Skip to Content
Author's profile photo Anoop Rai

SAP PI/PO Java Mapping — Test and Debug on Eclipse

Hi all,
This is my first Blog on SAP Blog.

It’s a simple blog that explains how to test a java mapping directly on Eclipse or any other IDE.

Till NW 7.0 it’s easy to debug with XML files on system, as we can directly able to convert File to InputStream and pass that a parameter into excecute method of StreamTransformation interface. But as StreamTransformation interface is deprecated and AbstractTransformation class has been implemented with NW 7.1.

As transform method of AbstractTransformation only accept TransformationInput as parameter, it’s difficult to pass offline XML file as payload and test it.

Here we will use a very simple approach to make sure our java mapping will work fine offline.

  1. Method getInputPayload() of TransformationInput object will return payload as InputPayload.
  2. Method getInputStream of InputPayload object will return payload as InputStream.
  3. Now it’s same as of previous Java mapping, we just need to create another method with any name in our JM and pass it as parameter, here we used execute method to do that.
  4. In execute method we are able to write any logic that we want and build output xml.
  5. Now, as we already know we need to change OutputStream to payload.
  6. Follow it : – getOutputStream -> getOutputPayload -> TransformationOutput and we have TransformationOutput object.
  7. That’s it.

Here is a simple code, that just push whole source XML into a target node.


package com.sapss.testme;


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;

import org.w3c.dom.Document;
import org.w3c.dom.Element;


 * @author i24989 (Anoop Rai)
 * @since 19-11-2017
 * @version 1.0

public class TestJavaMapping extends AbstractTransformation {

	TransformationInput transInput = null;
	TransformationOutput transOutput = null;

	public static void main(String[] args) throws StreamTransformationException, FileNotFoundException,
			TransformerConfigurationException, TransformerFactoryConfigurationError {
		TestJavaMapping pm = new TestJavaMapping();

		FileInputStream fin = new FileInputStream("C:\\Users\\154151.XML");
		FileOutputStream fout = new FileOutputStream("C:\\Users\\OFFLINEOUT.xml");

		pm.execute(fin, fout);


	public void transform(TransformationInput in, TransformationOutput out) throws StreamTransformationException {
		this.execute(in.getInputPayload().getInputStream(), out.getOutputPayload().getOutputStream());


	public void execute(InputStream in, OutputStream out) throws StreamTransformationException {

		try {
			DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			Document doc = db.newDocument();

			Element outRoot = doc.createElement("SAP_TestPayload");
			outRoot.setAttribute("xmlns", "");

			Element piXMLInput = doc.createElement("PIXMLInput");

			BufferedReader bfr = new BufferedReader(new InputStreamReader(in, "utf-8"));
			String line;
			String contentLine = "";
			while ((line = bfr.readLine()) != null) {
				contentLine = contentLine + line.trim();

			Transformer trns = TransformerFactory.newInstance().newTransformer();
			DOMSource targetMsg = new DOMSource(doc);
			StreamResult stRslt = new StreamResult(out);
			trns.transform(targetMsg, stRslt);

		} catch (ParserConfigurationException e) {
			throw new StreamTransformationException("Unable to Parse Input XML message: " + e.getMessage());
		} catch (UnsupportedEncodingException e) {
			throw new StreamTransformationException(
					"Unsupportable input (XML UTF-8 Only) or blank : " + e.getMessage());
		} catch (IOException e) {
			throw new StreamTransformationException("Unable to read xml lines or it may be blank: " + e.getMessage());
		} catch (TransformerConfigurationException e) {
			throw new StreamTransformationException("Unable to build output XML message: " + e.getMessage());
		} catch (TransformerFactoryConfigurationError e) {
			throw new StreamTransformationException("Unable to build output XML message: " + e.getMessage());
		} catch (TransformerException e) {
			throw new StreamTransformationException("Unable to build output XML message: " + e.getMessage());
		try {
		} catch (IOException e) {
			throw new StreamTransformationException("Unable to flush output XML message: " + e.getMessage());





Anoop Kumar Rai

Assigned Tags

      You must be Logged on to comment or reply to a post.
      Author's profile photo Apu Das
      Apu Das

      Hi Anoop,

      Congrats for your first blog. This was well described.

      But, there are numerous blogs in SCN on this topic and as far as XML data is concerned you can directly put and test this in ESR itself. Check below blog -



      Author's profile photo Fatih Pense
      Fatih Pense

      Hi Anoop,

      I think using relative paths while testing is better. Create a test folder in Eclipse.

      Like "test/case1/154151/input.xml" instead of "C:\\Users\\154151.XML"

      Thank you for your contribution.



      Author's profile photo Fábio Teixeira
      Fábio Teixeira

      Hi Anoop,


      Thanks for sharing information.


      Very helpful.


      Best Regards,

      Fábio Teixeira