简体   繁体   English

使用Mockito框架模拟/存根JCo库

[英]Mocking/stubbing JCo library using mockito framework

I am writing a basic Java program to connect to SAP server and retrieve a BAPI Function. 我正在编写一个基本的Java程序以连接到SAP服务器并检索BAPI函数。 This program is to basically showcase the importance of Unit Testing in development. 该程序基本上是要展示单元测试在开发中的重要性。 However, while writing Unit Tests I would not like to really connect to the server and expect JCo return types. 但是,在编写单元测试时,我不想真正连接到服务器并期望JCo返回类型。 Considering this, I went ahead with Mockito framework to mock/stub some of the JCo libraries and it's methods. 考虑到这一点,我继续使用Mockito框架来模拟/存根一些JCo库及其方法。 I would like to mention that I am new to Java and Mockito, however, I have good experience with C and C++. 我想提一下,我是Java和Mockito的新手,但是,我在C和C ++方面有丰富的经验。

Main Code: 主要代码:

package SAPConnection;

import java.util.Properties;

import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.ext.DestinationDataProvider;

public class sapConnection
{    
    public static JCoDestination getDestination() throws JCoException
    {
        JCoDestination destination = JCoDestinationManager.getDestination(DESTINATION_NAME1);
        return destination;
    }

    public static JCoFunction retrieveFunction(String funName, JCoDestination destination) throws JCoException
    {
        return(destination.getRepository().getFunction(funName)); 
    }

    public static void printMetaData(JCoFunction function)
    {
         System.out.println(function.getTableParameterList().getMetaData().toString());
         System.out.println("MetaData Printed Successfully\n\n");
    }    

    public static void main(String[] args) throws JCoException
    {
        JCoDestination Finaldestination = getDestination(); 
        JCoFunction function = 
        retrieveFunction("ZBAPI_GET_DOCUMENTS",Finaldestination);
        printMetaData(function);
    }
}

Test Code 测试代码

package SAPConnection;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.JCoFunction;

public class testsapConnection {

public JCoDestinationManager destManager = Mockito.mock(JCoDestinationManager.class);
public JCoDestination destination=Mockito.mock(JCoDestination.class);
public JCoFunction function = Mockito.mock(JCoFunction.class);

@Test
public void testgetDestination() throws JCoException {
    when(destManager.getDestination("ABAP_AS_WITHOUT_POOL")).thenReturn(destination);
    assertEquals(destination,sapConnection.getDestination());
}
}

Rather than a straightforward solution, please explain the underlying mistakes and problems in these codes so that I can understand. 除了简单的解决方案外,请解释这些代码中的潜在错误和问题,以便我理解。

While running the test it says, 在运行测试时说:

org.mockito.exceptions.misusing.MissingMethodInvocationException: 
when() requires an argument which has to be 'a method call on a mock'.

JCoDestinationManager.getDestination is a static method which you can't mock with 'when' as that expects an instance method. JCoDestinationManager.getDestination是一个静态方法,您不能使用'when'进行模拟,因为它需要一个实例方法。 You'll have to use a mocking framework that supports static methods like PowerMock. 您必须使用支持静态方法(例如PowerMock)的模拟框架。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM