The quickest way to remedy this is to build the ability to use serialized RFC interfaces into your application. In Java, you can create serialized Java objects, whereby the full state of the object is written to the filesystem and can be re-read and re-used whenever necessary. In this case, I create and save a serialized RFC from a live SAP system, then reuse the serialized interface whenever I need to test the application.
The included application demonstrates how to create the serialized interface, then disconnect from SAP and use the serialized JCo function object as if it was STILL connected to an online SAP system.
import java.io.* ;
import com.sap.mw.JCo.*;
public class SerializeRFC {
static final String[][] sapParams = { {"client", "000"}, {"user", "myUsername"},
{"passwd", "myPassword"}, {"lang", "EN"}, {"ashost", "mySapHost"}, {"sysnr", "00"}};
static final String filePath = "c:/dev/JCofunction.ser";
static final String interfaceName = "RFC_SYSTEM_INFO";
public static void getInterface() {
File fileOut = new File(filePath);
try {
System.out.println("Serializing online RFC function..");
JCo.Client connection = JCo.createClient(sapParams);
IRepository repository = new JCo.Repository("saprep", connection);
JCo.Function function = repository.getFunctionTemplate(interfaceName).getFunction();
connection.execute(function);
connection.disconnect();
ObjectOutputStream functionOut = new ObjectOutputStream(new FileOutputStream(fileOut));
functionOut.writeObject(function);
functionOut.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private static JCo.Function retrieveInterface() {
//Retrieve serialized RFC interface
ObjectInputStream functionIn = null;
JCo.Function function = null;
try {
File fileIn = new File(filePath);
System.out.println("Retrieving offline RFC function...");
functionIn = new ObjectInputStream(new FileInputStream(fileIn));
function = (JCo.Function) functionIn.readObject();
} catch (Exception ex){
ex.printStackTrace();
}
return function;
}
public static void executeTestCase() {
//Fake test case
JCo.Function function = retrieveInterface();
System.out.println("Executing test case for RFC System application...");
JCo.Structure expStruct = function.getExportParameterList().getStructure("RFCSI_EXPORT");
if (expStruct.getValue("RFCHOST").equals("mySapHost"))
System.out.println("Test successful");
else
System.out.println("Test failed");
}
public static void main(String[] args) {
SerializeRFC.getInterface();
SerializeRFC.executeTestCase();
}
}
THE JCO UTILITY COOKBOOK
Home: Introduction
Part 1: Writing an RFC structure to XML
Part 2: Generating metadata for an RFC structure
Part 3: Load testing SAP with JCo
Part 4: Creating a serialized RFC interface
Review: Conclusion and more resources
This was first published in August 2005
Join the conversationComment
Share
Comments
Results
Contribute to the conversation