How do I test this Spring MVC method with JUnit? [closed]

0

Hello, I need help to create a JUnit test class for the Spring method below.

@Controller
public class ControllerFuncionario {

    @RequestMapping("/index")
    public String index(Funcionario funcionario, Project pro, Model model) {

        JSONArray jObject;
        JSONParser jParser = new JSONParser();
        List<Funcionario> funcionarios = new ArrayList<Funcionario>();

        try {
            // FAZ ABRE O ARQUIVO JSON.
            // LOCAL PODE SER ALTERADO SEM PROBLEMAS.
            jObject = (JSONArray) jParser.parse(new FileReader("D:\employees.json"));

            // LOOP PRINCIPAL QUE MONTA OS OBJETOS FUNCIONARIO E ADICIONA NO List<Funcionario> funcionarios.
            for (int i = 0; i < jObject.size(); i++) {
                JSONObject jFuncionario = (JSONObject) jObject.get(i);

                funcionario = new Funcionario();

                funcionario.setIdFuncionario(i);
                funcionario.setName(jFuncionario.get("name").toString());
                funcionario.setRole(jFuncionario.get("role").toString());
                funcionario.setSalary(jFuncionario.get("salary").toString());
                funcionario.setManager(jFuncionario.get("manager").toString());
                funcionario.setGcm(jFuncionario.get("gcm").toString());

                // DENTRO DESTE IF SÃO INSTANCIADOS OS PROJETOS QUE SERÃO POPULADOS E ADICIONADOS AO FUNCIONARIO.
                if (jFuncionario.get("projects") != null) {
                    JSONArray projectArray = (JSONArray) jFuncionario.get("projects");
                    List<Project> projectList = new ArrayList<Project>();

                    for (int j = 0; j < projectArray.size(); j++) {
                        JSONObject projects = (JSONObject) projectArray.get(j);
                        pro = new Project();

                        pro.setName(projects.get("name").toString());
                        pro.setCustomer(projects.get("customer").toString());
                        pro.setValueOfProject(projects.get("valueOfProject").toString());
                        pro.setDtBegin(projects.get("dtBegin").toString());
                        pro.setDtEnd(projects.get("dtEnd").toString());

                        projectList.add(pro);
                    }
                    funcionario.setProjectList(projectList);
                }

                // DENTRO DESTE IF SÃO ADICIONADOS AS SKILLS DO FUNCIONARIO.
                if (jFuncionario.get("skills") != null) {
                    JSONArray skillArray = (JSONArray) jFuncionario.get("skills");
                    Iterator<String> iterator = skillArray.iterator();
                    String[] skills = new String[skillArray.size()];

                    while (iterator.hasNext()) {                        
                        for (int k = 0; k < skills.length; k++) {
                            skills[k] = iterator.next();
                        }
                        funcionario.setSkills(skills);
                    }
                }

                // DENTRO DESTE IF SÃO ADICIONADOS AS CERTIFICATIONS DO FUNCIONARIO.
                if (jFuncionario.get("certification") != null) {
                    JSONArray certificationArray = (JSONArray) jFuncionario.get("certification");
                    Iterator<String> iterator = certificationArray.iterator();

                    while (iterator.hasNext()) {
                        String[] certifications = new String[certificationArray.size()];

                        for (int l = 0; l < certifications.length; l++) {
                            certifications[l] = iterator.next();
                        }
                        funcionario.setCertification(certifications);
                    }
                }
                // FUNCIONARIO POPULADO ADICIONADO NA LISTA.
                funcionarios.add(funcionario);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return "errorPages/fileNotFoundError";
        } catch (IOException e) {
            e.printStackTrace();
            return "errorPages/fileNotFoundError";
        } catch (ParseException e) {
            e.printStackTrace();
            return "errorPages/parseError";
        }
        model.addAttribute("funcionarios", funcionarios);

        return "index";
    }
}

I do not know if it is necessary, but it also follows the class Officer.

    public class Funcionario {

    private long idFuncionario;
    private String name;
    private String role;
    private String salary;
    private String manager;
    private String gcm;
    private String[] skills;
    private String[] certification;
    private List<Project> projectList = new ArrayList<Project>();


    public long getIdFuncionario() {
        return idFuncionario;
    }
    public void setIdFuncionario(long idFuncionario) {
        this.idFuncionario = idFuncionario;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getRole() {
        return role;
    }
    public void setRole(String role) {
        this.role = role;
    }
    public String getSalary() {
        return salary;
    }
    public void setSalary(String salary) {
        this.salary = salary;
    }
    public String getManager() {
        return manager;
    }
    public void setManager(String manager) {
        this.manager = manager;
    }
    public String getGcm() {
        return gcm;
    }
    public void setGcm(String gcm) {
        this.gcm = gcm;
    }
    public String[] getSkills() {
        return skills;
    }
    public void setSkills(String[] skills) {
        this.skills = skills;
    }
    public String[] getCertification() {
        return certification;
    }
    public void setCertification(String[] certification) {
        this.certification = certification;
    }
    public List<Project> getProjectList() {
        return projectList;
    }
    public void setProjectList(List<Project> projectList) {
        this.projectList = projectList;
    }
}

And also the Project class.

    public class Project {

    private long projectId;
    private String name;
    private String customer;
    private String valueOfProject;
    private String dtBegin;
    private String dtEnd;
    private Funcionario funcionario;


    public long getProjectId() {
        return projectId;
    }
    public void setProjectId(long projectId) {
        this.projectId = projectId;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getCustomer() {
        return customer;
    }
    public void setCustomer(String customer) {
        this.customer = customer;
    }
    public String getValueOfProject() {
        return valueOfProject;
    }
    public void setValueOfProject(String valueOfProject) {
        this.valueOfProject = valueOfProject;
    }
    public String getDtBegin() {
        return dtBegin;
    }
    public void setDtBegin(String dtBegin) {
        this.dtBegin = dtBegin;
    }
    public String getDtEnd() {
        return dtEnd;
    }
    public void setDtEnd(String dtEnd) {
        this.dtEnd = dtEnd;
    }
    public Funcionario getFuncionario() {
        return funcionario;
    }
    public void setFuncionario(Funcionario funcionario) {
        this.funcionario = funcionario;
    }
}

Sorry for anything, I'm new here on the forum and a beginner in programming. This code was requested as a test in a job interview and is missing only the JUnit test class.

Note: The controller method returns a JSP.

Thank you in advance.

    
asked by anonymous 24.12.2018 / 13:37

0 answers