jeudi 11 juin 2020

I am new in programming and i need help in writing Junit 5 test case for file not found exception for private method

Below is a method of my class for which is want to junit write test case, I want to write a test case for file not found exception in private method

  private static String getCustomerName(String id) throws IOException {
        String csvFile = "E:\\detail.csv";
        BufferedReader br = null;
        String line = "";
        String cvsSplitBy = ",";

        if (customerMap == null) {
            try {
                br = new BufferedReader(new FileReader(csvFile));
                customerMap = new HashMap<>();
                while ((line = br.readLine()) != null) {

                    // use comma as separator
                    String[] str = line.split(cvsSplitBy);
                    customerMap.put(str[0], str[1]);

                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (br != null) {
                    try {
                        br.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
            return customerMap.get(id);
    }

Aucun commentaire:

Enregistrer un commentaire