How to write test case for the if-else in Mockito

Spell Blade :

I am trying to write test case for the following code

   public boolean discharge(RegBean reg_data) {

        boolean status = false;
        try{
            con = dbConObj();

            PreparedStatement pst = con.prepareStatement("select * from patient where patientid=?");
            pst.setString(1,reg_data.getpatientID());
            ResultSet rs = (ResultSet) pst.executeQuery();

            if (rs.next()){

                String docname=rs.getString("docname").trim();
                String pstatus=rs.getString("status").trim();

                PreparedStatement ps=con.prepareStatement("update patient set ddate=?,status=? where patientID=?");
                if (pstatus.equalsIgnoreCase("Active"))
                {
                    ps.setString(1,new java.util.Date().toString());
                    ps.setString(2,"Discharged");

                    PreparedStatement pst1=con.prepareStatement("insert into history values(?,?,?,?)");

                    pst1.setString(1,docname);
                    pst1.setString(2,reg_data.getpatientID());
                    pst1.setString(3,"Discharged");
                    pst1.setString(4,new java.util.Date().toString());
                    pst1.executeUpdate();

                }
                else
                if (pstatus.equalsIgnoreCase("Discharged"))
                {
                    ps.setString(1,"");
                    ps.setString(2,"Active");

                    PreparedStatement pst1=con.prepareStatement("insert into history values(?,?,?,?)");

                    pst1.setString(1,docname);
                    pst1.setString(2,reg_data.getpatientID());
                    pst1.setString(3,"Active");
                    pst1.setString(4,new java.util.Date().toString());
                    pst1.executeUpdate();
                9
                }
                ps.setString(3,reg_data.getpatientID());
                ps.executeUpdate();

            }

            status = true;

        }
        catch(Exception e){
            e.printStackTrace();
            return status;
        }

        return status;
    }

In case of the first if(rs.next()) I mocked rs and I was able to test it and get a coverage. However in the case of second if if (pstatus.equalsIgnoreCase("Active")) pstatus is a variable and I cannot use DoReturn.when(mockedmethod). Is there any way to test is so that I can get a test coverage when run with JUnit ?

Roland Kreuzer :

If you already managed to mock the ResultSet for rs.next(), you should also be able to do

when(rs.getString("status")).thenReturn("Active");

Going forward I'd try to break the method down into smaller methods that take the mocked type as an argument. It simplifies the whole testcase as you don't have to prepare and check highly nested mocks. The same would help to work with the statement types.

A sidenote: You might want to close ResultSet and Statements before returning.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=18981&siteId=1