ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 06. ReturnType, Junit [Spring]
    Spring 2016. 11. 4. 15:12
    반응형



    01. Return Type



    01-1Contoller

    @RequestMapping("/hr/ajax/employees")

    public void getAllEmployees(HttpServletResponse response) {

    List<EmployeesVO> allEmployees = indexService.getAllEmployees(); StringBuffer

     

    employeesInfo = new StringBuffer(); 


    for (EmployeesVO employeesVO : allEmployees) { employeesInfo.append(employeesVO.getLastName() + "<br/>");

     } 


    // response.setContentType("text/plain"); 

    //대부분 이렇게 쓰지만 현재는 <br/>로 인해 아래것을 사용함

    response.setContentType("text/html");

    response.setCharacterEncoding("UTF-8");

    PrintWriter out = null;

    try {

    out = response.getWriter();

    out.write(employeesInfo.toString());

    out.flush();

    }

    catch (IOException e) {

    throw new RuntimeException(e.getMessage(), e);

    }

    finally {

    if ( out != null ) {

    out.close();

    }

    }

    }



    01-2. Json

    porn.xml에 jackson-databind 를 추가


    @RequestMapping("/hr/ajax/json/employees")

    @ResponseBody

    public List<EmployeesVO> getAllEmployeesJSON() {

    return indexService.getAllEmployees();

    }




    02. Junit

    - 기능 하나하나의 개별적인 테스트를 하기 위함.


    porn.xml에서

    org.springframework spring-test 추가하기


    프로젝트 마우스 오른쪽버튼 - Properties - Java Build Path - Libraries - Add Libraries - Junit - Junit4 선택 후 Finish


    src/test/java에 biz, dao, service 패키지를 생성하고 class를 src/main/java와 똑같이 만든다




    @RunWith(SpringJUnit4ClassRunner.class)

    @ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/spring/rootContext.xml",

       "file:src/main/webapp/WEB-INF/spring/indexContext.xml"})

    public class IndexServiceImpl {

    private Logger logger = LoggerFactory.getLogger(IndexServiceImpl.class);

    @Autowired

    private IndexService indexService;

    @Before

    public void setUp() {

    logger.info("Before 입니다.............");

    // Test하기전 사전 작업을 위한 단계

    EmployeesVO newEmployee = new EmployeesVO();

    newEmployee.setEmployeesId("9999");

    newEmployee.setFirstName("TEST_FirstName");

    newEmployee.setLastName("TEST_LastName");

    newEmployee.setEmail("TEST_Email");

    newEmployee.setJobId("IT_PROG");

    indexService.addNewEmployee(newEmployee);

    }

    @After

    public void tearDown() {

    logger.info("After 입니다.........");

    indexService.deleteEmployeeId("9999");

    indexService.deleteEmployeeId("9998");

    }

    @Test

    public void getNowDateTime() {

    String nowDateTime = indexService.getNowDateTime();

    logger.info(nowDateTime);

    Assert.notNull(nowDateTime); // 값이 있느냐 없느냐를 판단하기위함

    }

    @Test

    public void getAllEmployees() {

    List<EmployeesVO> allEmployees = indexService.getAllEmployees();

    Assert.notNull(allEmployees);

    Assert.isTrue(allEmployees.size() > 0);

    }

    @Test

    public void getAllDepartments() {

    EmployeesVO employee = indexService.getEmployeeByEmployeeId("9999");

    Assert.notNull(employee);

    }

    @Test

    public void addNewEmployee() {

    EmployeesVO newEmployee = new EmployeesVO();

    newEmployee.setEmployeesId("9998");

    newEmployee.setFirstName("TEST_FirstName");

    newEmployee.setLastName("TEST_LastName");

    newEmployee.setEmail("TEST_Email2");

    newEmployee.setJobId("IT_PROG");

    boolean result = indexService.addNewEmployee(newEmployee);

    Assert.isTrue(result);

    EmployeesVO employee = indexService.getEmployeeByEmployeeId("9998");

    Assert.notNull(employee);

    Assert.isTrue(newEmployee.getEmployeesId().equals(employee.getEmployeesId()));

    Assert.isTrue(newEmployee.getLastName().equals(employee.getLastName()));

    }

    @Test(expected=DuplicateKeyException.class)

    public void addNewEmployeeDuplicatedId() {

    EmployeesVO newEmployee = new EmployeesVO();

    newEmployee.setEmployeesId("9999");

    newEmployee.setFirstName("TEST_FirstName");

    newEmployee.setLastName("TEST_LastName");

    newEmployee.setEmail("TEST_Email");

    newEmployee.setJobId("IT_PROG");

    indexService.addNewEmployee(newEmployee);

    } // 중복체크의 오류나는걸 테스트한다 성공시 중복체크가 잘 된다.









    반응형

    댓글

Designed by Tistory.