错误:无效使用参数匹配器!预计有0个匹配器,已记录1个

时间:2019-07-08 08:14:51

标签: java unit-testing junit mockito

我正在尝试为我的方法做测试用例,但是我遇到了错误

  

无效使用参数匹配器!预计有0个匹配器,已记录1个

我不知道问题出在哪里。我尝试了很多事情,例如(anyIny(),anyString())也无法正常工作。

如何处理JsonObject?

此行中的错误:verify(mockPreparedStmnt,times(8))。setString(anyInt(),mock.toString());

我还有另一个问题:

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

这是我尝试测试的方法。

    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();

    }

1 个答案:

答案 0 :(得分:1)

根据您的原始课堂,我剥离了一些内容。


编辑: (我尚未将其与您的第二次编辑进行比较,因此可能不希望您有此想法)

看起来与您的第二次编辑类似,但是在实际情况下,您必须做更多的事情来模仿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", "segementName": "y"}]