• Post Reply Bookmark Topic Watch Topic
  • New Topic

Easy mock class intantiation  RSS feed

 
umesh kathiriya
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am mocking class using easymock , but i am not able to create instance of mocked class using new .

below is my class.

public class XmlTxnAddMetadata extends KonaProcessor {

public static final String ERROR_IN_GENERATING_RESPONSE_JADE_XML = "Error in Generating Response JadeXml";
private static Log log = LogFactory.getLog(XmlTxnAddMetadata.class);
private GetBundleIdService service;
private BpelEndPointGetBundleIdPortType port;
private KonaMessage request;
private KonaMessage response;

/**
* Default C'tor
*/
public XmlTxnAddMetadata() {

}
/**
* @param action
* transaction name to be passed
* @param requestXml
* jade requst xml coming from client
* @throws KonaException
* thrown
*/

public XmlTxnAddMetadata(String action, String requestXml)
throws KonaException {
super(action, requestXml);
log.info("requestXml = " + requestXml);
}

/*
* (non-Javadoc)
* @see com.apple.ist.kona.service.KonaProcessor#processRequest(com.apple.ist.kona.remoting.protocol.Message)
*/
@Override
public String processRequest(Message mes) throws KonaRequestException {
MessageHeader message = mes.getHeader();


if (log.isDebugEnabled()) {
log.debug((new StringBuilder("Sending message to Bpel --")).append(message.toString()).toString());
log.debug((new StringBuilder("Sending message to Bpel --")).append(requestXml).toString());
}

/*KonaMessage request = new KonaMessage(
String.valueOf(message.getCode()), String.valueOf(message.getTid()), String.valueOf(message.getSubCode()),
String.valueOf(message.getAltCode()), String.valueOf(message.getErrorCode()), String.valueOf(message.getRows()),
String.valueOf(message.getLength()), String.valueOf(message.getChecksum()), requestXml, message.getSessionId(),"istsf");
*/
// Call the BPEL process


service = new GetBundleIdService();

/*BpelEndPointGetBundleIdPortType port = service.getBpelEndPointGetBundleIdPortTypeRolePort();
KonaMessage response = port.bpelEndPointGetBundleIdOperation(request);*/

port = service.getBpelEndPointGetBundleIdPortTypeRolePort();

System.out.println("Port in class is : "+port);

response = port.bpelEndPointGetBundleIdOperation(request);

String xml = response.getXml();
String istXML1 = null;
try {
log.info("requestXml :" + requestXml);
ResponseXmlCache.initXmlCache();
istXML1 = IstsfToJade.getJadeXml(
com.apple.ist.kona.remoting.protocol.Message
.getJadeMessageBody(), "XmlTxnAddMetadata");
log.info("getJadeXml :" + istXML1);
String[] tag = { "FILEID" };
String output = IstsfToJade.getResponseXml(istXML1, xml, tag);
log.info("Response JADE Xml : " + output);
setResponseXml(output);
log.info("Transaction completed successfully");
} catch (KonaException e) {
throw new KonaRequestException(e);
}


return null;
}



public GetBundleIdService getService(){
return service;
}
public void setService(GetBundleIdService service){
this.service = service;
}
public BpelEndPointGetBundleIdPortType getPort(){
return port;
}
public void setPort(BpelEndPointGetBundleIdPortType port){
this.port = port;
}
public KonaMessage getRequest(){
return request;
}
public void setRequest(KonaMessage request){
this.request = request;
}
}

In this class, i have call to GetBundleIdService class, which is available at run time only, so for writing junit i am mocking this class as per my junit class below

public class XmlTxnAddMetadataTest {

String requestXml = null;
String reponseXml = null;
URL url = null;
String action = null;
MessageHeader header = null;
BpelEndPointGetBundleIdPortType port = null;
GetBundleIdService service = null;
XmlTxnAddMetadata addMetadataObj = null;
private static Log log = LogFactory.getLog(XmlTxnAddMetadataTest.class);

@Before
public void setUp() {
action = "XmlTxnAddMetadata";
requestXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+ "<TXN CODE=\"XmlTxnAddMetadata\" TYPE=\"jems\" TID=\"1\">"
+ "<BUNDLE ID=\"DESNFA000000001\" KEY=\";8=YZOwpS!6-+<[>\">"
+ "<FILE NAME=\"/jade/src/jems/src/Enclosure.java\" CTIME=\"1970/01/01\" MTIME=\"1970/01/01\" CFT=\"111\" CFC=\"111\" SSIZE=\"123\" CSIZE=\"123\" COMPTYPE=\"-\" ENCODINGTYPE=\"c\" UNIXPRIV=\"DFLT\"/>"
+ "</BUNDLE>" + "</TXN>";
reponseXml = "<?xml version=\"1.0\"?>"
+ "<TXN CODE = \"ISTSFXmlTxn\" TID = \"9\" TYPE = \"ISTSF\" STATUS=\"0\">"
+ "<SQL QID=\"5\" ROWSINCACHE=\"0\" ROWSTART=\"1\" ROWSRETURNED=\"1\" ACTIVECACHE=\"N\" SHORTXML=\"NO\" >"
+ "<DATASET NAME=\"RESULT\">"
+ "<ROW><FILEID>RJ1GDV0023114048</FILEID></ROW>"
+ "</DATASET>" + "</SQL>" + "<DEBUG>"
+ "<QUERY_EXECUTION_TIME>147</QUERY_EXECUTION_TIME>"
+ "<RESPONSE_BUILD_TIME>0</RESPONSE_BUILD_TIME>"
+ "<PARSER_EXECUTION_TIME>5</PARSER_EXECUTION_TIME>"
+ "<QUERY_GENERATION_TIME>25</QUERY_GENERATION_TIME>"
+ "</DEBUG>" + "</TXN>";

addMetadataObj = new XmlTxnAddMetadata();

}

/**
* Test method for {@link XmlTxnAddMetadataTest#processRequest()}
* @throws Exception
*/
@Test
public void testProcessRequest() throws Exception {

MessageHeader header = new MessageHeader(10, 20, 30, 40, 50, 60, 70, 80);
header.setSessionId("abcd");
Message mes = new Message(header,requestXml);
KonaMessage request = new KonaMessage(
String.valueOf(header.getCode()), String.valueOf(header.getTid()), String.valueOf(header.getSubCode()),
String.valueOf(header.getAltCode()), String.valueOf(header.getErrorCode()), String.valueOf(header.getRows()),
String.valueOf(header.getLength()), String.valueOf(header.getChecksum()), requestXml, header.getSessionId(),"istsf");

KonaMessage resp = new KonaMessage(
String.valueOf(header.getCode()), String.valueOf(header
.getTid()), String.valueOf(header.getSubCode()),
String.valueOf(header.getAltCode()), String.valueOf(header
.getErrorCode()), String.valueOf(header.getRows()),
String.valueOf(header.getLength()), String.valueOf(header
.getChecksum()), reponseXml, header.getSessionId(),
"istsf");

port = createMock(BpelEndPointGetBundleIdPortType.class);
expect(port.bpelEndPointGetBundleIdOperation(request)).andReturn(resp);

replay(port);

System.out.println("Port in test class is : "+port);
/*service = EasyMock.createMock(GetBundleIdEndPoint.class);
EasyMock.expect(service.getBpelEndPointGetBundleIdPortTypeRolePort()).andReturn(port);

EasyMock.replay(service);*/
service = createMock(GetBundleIdService.class);


expect(service.getBpelEndPointGetBundleIdPortTypeRolePort()).andReturn(port);
replay(service);
addMetadataObj.setRequestXml(requestXml);
addMetadataObj.setService(service);
addMetadataObj.setPort(port);
addMetadataObj.setRequest(request);

addMetadataObj.processRequest(mes);

verify(service);
verify(port);
System.out.println("Verify complete");
}
}

But when we execute it , below lines tries to find original class instead of my mocking class and it is not available at compile time junit fails

service = new GetBundleIdService();

So above execute fails,
Is there any way to set expectation for creating objects to run above line.

Thanks,
Umesh

 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, why are you instantiating a new service instead using the one that is injected using the setter?
 
umesh kathiriya
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

One change here,

i want to achieve this,

GetBundleIdService service = new GetBundleIdService();

Its local instance, not class variable,


 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!