Mocking/stubbing 使用 mockito 框架的 JCo 库
Mocking/stubbing JCo library using mockito framework
我正在编写一个基本的 Java 程序来连接到 SAP 服务器并检索 BAPI 函数。这个程序基本上是为了展示单元测试在开发中的重要性。但是,在编写单元测试时,我不想真正连接到服务器并期望 JCo return 类型。考虑到这一点,我继续使用 Mockito 框架 mock/stub 一些 JCo 库及其方法。我想提一下,我是 Java 和 Mockito 的新手,但是,我对 C 和 C++ 有很好的经验。
主要代码:
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);
}
}
测试代码
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());
}
}
请解释这些代码中潜在的错误和问题,而不是直接的解决方案,以便我理解。
虽然 运行 测试显示,
org.mockito.exceptions.misusing.MissingMethodInvocationException:
when() requires an argument which has to be 'a method call on a mock'.
JCoDestinationManager.getDestination 是一个静态方法,你不能用 'when' 模拟它,因为它需要一个实例方法。您必须使用支持静态方法的模拟框架,例如 PowerMock。
使用最新的 Mockito,您可以模拟静态方法。
try (MockedStatic<JCoDestinationManager> manager = mockStatic(JCoDestinationManager.class)) {
manager.when(() ->
JCoDestinationManager.getDestination("ABAP_AS_WITHOUT_POOL"))
.thenReturn(destination);
assertEquals(destination,sapConnection.getDestination());
}
我正在编写一个基本的 Java 程序来连接到 SAP 服务器并检索 BAPI 函数。这个程序基本上是为了展示单元测试在开发中的重要性。但是,在编写单元测试时,我不想真正连接到服务器并期望 JCo return 类型。考虑到这一点,我继续使用 Mockito 框架 mock/stub 一些 JCo 库及其方法。我想提一下,我是 Java 和 Mockito 的新手,但是,我对 C 和 C++ 有很好的经验。
主要代码:
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);
}
}
测试代码
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());
}
}
请解释这些代码中潜在的错误和问题,而不是直接的解决方案,以便我理解。
虽然 运行 测试显示,
org.mockito.exceptions.misusing.MissingMethodInvocationException:
when() requires an argument which has to be 'a method call on a mock'.
JCoDestinationManager.getDestination 是一个静态方法,你不能用 'when' 模拟它,因为它需要一个实例方法。您必须使用支持静态方法的模拟框架,例如 PowerMock。
使用最新的 Mockito,您可以模拟静态方法。
try (MockedStatic<JCoDestinationManager> manager = mockStatic(JCoDestinationManager.class)) {
manager.when(() ->
JCoDestinationManager.getDestination("ABAP_AS_WITHOUT_POOL"))
.thenReturn(destination);
assertEquals(destination,sapConnection.getDestination());
}