gpt4 book ai didi

java - 错误 :Invalid use of argument matchers! 预计有 0 个匹配器,已记录 1 个

转载 作者:行者123 更新时间:2023-12-02 09:42:44 36 4
gpt4 key购买 nike

我正在尝试为我的方法做测试用例,但出现以下错误

Invalid use of argument matchers! 0 matchers expected, 1 recorded

不知道问题出在哪里。我尝试了很多东西,比如 (anyIny(), anyString()) 也不起作用。

我如何处理 JsonObject?

这一行的错误:verify(mockPreparedStmnt, times(8)).setString(anyInt(), mock.toString());

我还有一个问题:

如果在我的情况下它不等于“ADD”,我如何处理 if 语句?

这是我尝试测试的方法。

    public ApprovalManagementBean insertOrUpdateApprovalManagement(String body,
String transactionType) throws IOException,
JsonParseException,
JsonMappingException {
ApprovalManagementBean obj = new ApprovalManagementBean();
List<JSONObject> beanList = new ArrayList<JSONObject>();
JSONArray arr = new JSONArray(body);
JSONObject jsonObjInput;
String body2 = "";

try {
if (transactionType.equals("ADD")) {
int transactionNum = 23;
connection = getConnection();
for (int i = 0; i < arr.length(); i++) {
jsonObjInput = arr.getJSONObject(i);
String query =
"INSERT INTO " + " " + getSchema_Name() + ".XXX_APROVAL_MANAGEMENT(EIT_CODE,TRANSACTION_NUM)\n" +
"VALUES(?,?)";
ps = connection.prepareStatement(query);
ps.setString(1, jsonObjInput.get("eitCode").toString());
ps.setInt(2, transactionNum);
ps.executeUpdate();
}

} else if (transactionType.equals("EDIT")) {
connection = getConnection();
for (int i = 0; i < arr.length(); i++) {
jsonObjInput = arr.getJSONObject(i);
String query =
"UPDATE " + " " + getSchema_Name() + ".XXX_APROVAL_MANAGEMENT AM SET am.enable = ?";
ps = connection.prepareStatement(query);
ps.setString(1,
!jsonObjInput.isNull("enable") ? jsonObjInput.get("enable").toString() :"");
ps.executeUpdate();
}
}
} catch (Exception e) {
//("Error: ");
e.printStackTrace();
} finally {
closeResources(connection, ps, rs);
}
return obj;
}



public int getTransactionApprovalManagementNumber() {
connection = getConnection();
String query =
"SELECT NVL(MAX(TRANSACTION_NUM),0) AS max_transaction_approval FROM " +
" " + getSchema_Name() + ".XXX_APROVAL_MANAGEMENT";
try {
ps = connection.prepareStatement(query);
rs = ps.executeQuery();

while (rs.next()) {
max_transaction = rs.getInt("max_transaction_approval");
}

} catch (Exception e) {
//("Error: ");
e.printStackTrace();
} finally {
closeResources(connection, ps, rs);
}

max_transaction++;

return max_transaction;

}



@Before
public void setUp() throws SQLException {
approvalManagementDAO = new ApprovalManagementDAO();
when(mockConn.prepareStatement(anyString())).thenReturn(mockPreparedStmnt);
doNothing().when(mockConn).commit();
doNothing().when(mockPreparedStmnt).setString(anyInt(), anyString());
when(mockPreparedStmnt.executeQuery()).thenReturn(mockResultSet);
when(mockResultSet.next()).thenReturn(Boolean.TRUE, Boolean.FALSE);
approvalManagementDAO = new ApprovalManagementDAO() {
@Override
public Connection getConnection() {
return mockConn;
}
};
}

@Test
public void insertOrUpdateApprovalManagement() throws IOException,
JsonParseException,
JsonMappingException,
SQLException {
String body = "[]";
String transactionType = "ADD";
JSONObject mock = Mockito.mock(JSONObject.class);
approvalManagementDAO.insertOrUpdateApprovalManagement(body,
transactionType);
verify(mockConn, times(1)).prepareStatement(anyString());

verify(mockPreparedStmnt, times(8)).setString(anyInt(), mock.toString());
// verify(mockPreparedStmnt, times(1)).setInt(anyInt(), anyInt());
verify(mockPreparedStmnt, times(1)).executeUpdate();

}

最佳答案

根据您原来的类(class),我删除了一些东西。

<小时/>

编辑:(我还没有将它与您的第二次编辑进行比较,所以它可能不是您想要的)

看起来与您的第二次编辑足够相似,但是您必须做更多的事情来模拟实际情况中 getTransactionApprovalManagementNumber() 的行为。现在您已将其替换为固定数字。

<小时/>

看看这个(我使用了 JUnit 5 和 Mockito 2.27.x):

@ExtendWith(MockitoExtension.class)
class Test {

public static class ApprovalManagementDAO {

public ApprovalManagementBean insertOrUpdateApprovalManagement(JSONArray arr, String transactionType)
throws Exception {

ApprovalManagementBean obj = new ApprovalManagementBean();
JSONObject jsonObjInput;

Connection connection = null;
PreparedStatement ps = null;
ResultSet rs = null;

try {
if (transactionType.equals("ADD")) {

connection = getConnectionFromAppsPro();
for (int i = 0; i < arr.length(); i++) {
jsonObjInput = arr.getJSONObject(i);

String query = "some insert query";
ps = connection.prepareStatement(query);

ps.setString(1, jsonObjInput.get("eitCode").toString());
ps.setString(2, !jsonObjInput.isNull("segementName") ? jsonObjInput.get("segementName").toString() : "");

ps.executeUpdate();

}

} else if (transactionType.equals("EDIT")) {
connection = getConnectionFromAppsPro();
for (int i = 0; i < arr.length(); i++) {
jsonObjInput = arr.getJSONObject(i);
String query = "some update query";
ps = connection.prepareStatement(query);
ps.setString(1, !jsonObjInput.isNull("enable") ? jsonObjInput.get("enable").toString() : "");

ps.executeUpdate();
}
}

} catch (Exception e) {
e.printStackTrace();
} finally {
closeResources(connection, ps, rs);
}

return obj;
}

private void closeResources(Connection connection, PreparedStatement ps, ResultSet rs) {
}

Connection getConnectionFromAppsPro() {
return null;
}
}

Connection mockConn;
PreparedStatement mockPreparedStmnt;
ApprovalManagementDAO approvalManagementDAO;

@BeforeEach
public void setUp() throws SQLException {

mockConn = Mockito.mock(Connection.class);
mockPreparedStmnt = Mockito.mock(PreparedStatement.class);

Mockito.when(mockConn.prepareStatement(Mockito.anyString())).thenReturn(mockPreparedStmnt);

approvalManagementDAO = new ApprovalManagementDAO() {

@Override
public Connection getConnectionFromAppsPro() {
return mockConn;
}
};
}

@Test
public void insertOrUpdateApprovalManagement() throws Exception {

String transactionType = "ADD";

JSONObject mockJsonObject = Mockito.mock(JSONObject.class);
Mockito.when(mockJsonObject.get(Mockito.anyString())).thenReturn("");

JSONArray mockJsonArray = Mockito.mock(JSONArray.class);
Mockito.when(mockJsonArray.length()).thenReturn(1);
Mockito.when(mockJsonArray.getJSONObject(0)).thenReturn(mockJsonObject);

approvalManagementDAO.insertOrUpdateApprovalManagement(mockJsonArray, transactionType);
Mockito.verify(mockConn, Mockito.times(1)).prepareStatement(Mockito.anyString());

Mockito.verify(mockPreparedStmnt, Mockito.times(2)).setString(Mockito.anyInt(), Mockito.anyString());
Mockito.verify(mockPreparedStmnt, Mockito.times(1)).executeUpdate();

}
}
<小时/>

如果您不想更改方法签名,请记住在正文中使用有效的 json 格式,例如:[{"eitCode": "x", "segmentName": "y"}]

关于java - 错误 :Invalid use of argument matchers! 预计有 0 个匹配器,已记录 1 个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56930845/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com