[새싹] 현대IT&E 231129 기록 - Spring

최정윤·2023년 11월 30일
0

새싹

목록 보기
32/67

02. 스프링의 특징과 의존성 주입

2.1 스프링 프레임워크의 간략한 역사

  • 스프링 = 경량 프레임워크
    • 복잡한 구동 환경과 하드웨어적인 구성이 필요한 프레임워크의 반대되는 개념
    • 과거 J2EE 기술은 너무 복잡하고 방대해서 전체를 이해하고 개발하기 어려운점이 많아, 늑정 기능을 위주로 간단한 jar vk파일 등을 이용해서 모든 개발이 가능하도록 구성된 프레임 워크이다.

2.1.1 스프링 주요 특징

  • POJO 기반의 구성
  • 의존성 주입을 통한 객체 간의 관계 구성
  • AOP 지원
  • 편리한 MVC 구조
  • WAS의 종속적이지 않은 개발 환경

2.2 의존성 주입 테스트

  • 의존성 주입 구현
    • 생성자를 이용한 주입
    • setter 메소드를 이용한 주입
  • 설정방식
    • XML
    • 어노테이션
  • Lombok을 이용해서 setter 메서드를 자동 구현
  • pom.xml
    • Lombok 라이브러리 추가
    • spring-test 라이브러리 이용

STS 실습

코드

net.developia.spring02

HomeController.java

package net.developia.spring02;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {
	
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	
	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home(Locale locale, Model model) {
		logger.info("Welcome home! The client locale is {}.", locale);
		
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
		
		String formattedDate = dateFormat.format(date);
		
		model.addAttribute("serverTime", formattedDate );
		
		return "home";
	}
	
}

net.developia.spring02.controller

SampleController.java

package net.developia.spring02.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import lombok.extern.java.Log;
import net.developia.spring02.domain.SampleDTO;

@Controller
@RequestMapping("/sample/*")
@Log
@PropertySource("classpath:sample.properties")
public class SampleController {

	@RequestMapping("")
	public void basic() {
		log.info("basic........");
	}
	
	
	@RequestMapping(value="/basicGet", method= {RequestMethod.GET, RequestMethod.POST})
	public void basicGet() {
		log.info("basic get...........................");
	}
	
	@GetMapping("/basicOnlyGet")
	public void basicOnlyGet() {
		log.info("basic only get...........................");
	}
	
	@GetMapping("/ex06")
	public @ResponseBody SampleDTO ex06() {
		log.info("/ex06............");
		
		SampleDTO dto = new SampleDTO();
		dto.setAge(20);
		dto.setName("홍길동");
		
		return dto;
	}
	
	@GetMapping("/ex07")
	public ResponseEntity<String> ex07() {
		log.info("/ex07............");
		
		String msg = "{\"name\" : \"홍길동\"}";
		
		HttpHeaders header = new HttpHeaders();
		header.add("Content-Type", "application/json;charset=UTF-8");
		
		return new ResponseEntity<>(msg, header, HttpStatus.OK);
	}
	
	@GetMapping("/exUpload")
	public void exUpload() {
		log.info("/exUpload................................");
	}
	
	@Value("${upload_path}") String uploadPath;
	@PostMapping("/exUploadPost")
	public void exUploadPost(ArrayList<MultipartFile> files) {
		files.forEach(file -> {
			log.info("----------------------");
			log.info("name : " + file.getOriginalFilename());
			log.info("size : " + file.getSize());
			try {
				if(file == null) throw new IOException();
				File filename = new File(uploadPath, file.getOriginalFilename());
				file.transferTo(filename);
			} catch (IOException e) {
				e.printStackTrace();
			}
		});
	}
//	SampleDTO로 해보기 
//	@GetMapping("/ex0b")
//	public @ResponseBody SampleDTO ex07b() {
//		log.info("/ex07b............");
//		
//		SampleDTO dto = new SampleDTO();
//		dto.setAge(20);
//		dto.setName("홍길동");
//		
//		return dto;
//		
//		String msg = "{\"name\" : \"홍길동\"}";
//		
//		HttpHeaders header = new HttpHeaders();
//		header.add("Content-Type", "application/json;charset=UTF-8");
//		
//		return new ResponseEntity<>(msg, header, HttpStatus.OK);
//	}

/*	@GetMapping("/ex01")
	public String ex01(SampleDTO dto) {
		log.info("" + dto);
		
		return "ex01";
	}
	
	@GetMapping("/ex02")
	public String ex02(@RequestParam("name") String name, @RequestParam("age") int age) {
		log.info("name : " + name);
		log.info("age : " + age);
		
		return "ex02";	
	}
	
	@GetMapping("/ex02List")
	public String ex02List(@RequestParam("ids") ArrayList<String> ids) {
		log.info("ids : " + ids.get(0));
		
		return "ex02List";
	}
	
	@GetMapping("/ex02Array")
	public String ex02Array(@RequestParam("ids") String[] ids) {
		log.info("array ids : " + Arrays.toString(ids));
		
		return "ex02Array";
	}
	
	@GetMapping("/ex02Bean")
	public String ex02Bean(SampleDTOList list) {
		log.info("list dtos : " + list);
		
		return "ex02Bean";
	}
	
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");
		binder.registerCustomEditor(java.util.Date.class, new CustomDateEditor(dateFormat,  false));
		
	}
	
	@GetMapping("/ex03")
	public String ex03(TodoDTO todo) {
		log.info("todo : " + todo);
		
		return "ex03";
	}
	
	@GetMapping("/ex04")
	public String ex04(SampleDTO dto, @ModelAttribute("page") int page) {
		log.info("dto : " + dto);
		log.info("page : " + page);
		
		return "/sample/ex04";
	}
	
	
	@GetMapping("/ex07")
	public ResponseEntity<String> ex07(){
		log.info("/ex07...........................");
		
		String msg = "{\"name\" : \"홍똥\"}";
		
		HttpHeaders header = new HttpHeaders();
		header.add("Content-type", "application/json;charset=UTF-8");
		
		return new ResponseEntity<>(msg, header, HttpStatus.OK);
	}
	
	@GetMapping("/exUpload")
	public void exUpload() {
		log.info("/exUpload................................");
	}
	
	@PostMapping("/exUploadPost")
	public void exUploadPost(ArrayList<MultipartFile> files) {
		files.forEach(file -> {
			log.info("----------------------");
			log.info("name : " + file.getOriginalFilename());
			log.info("size : " + file.getSize());
		});
	}*/
}

SampleRestController.java

package net.developia.spring02.controller;

import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.log;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.java.Log;
import net.developia.spring02.domain.SampleDTO;

@RestController
@RequestMapping("/restful")
@Log
public class SampleRestController {
	@GetMapping("/ex06")
	public @ResponseBody SampleDTO ex06() {
		log.info("/ex06............");
		
		SampleDTO dto = new SampleDTO();
		dto.setAge(20);
		dto.setName("홍길동");
		
		return dto;
	}
}

net.developia.spring02.domain

SampleDTO.java

package net.developia.spring02.domain;

import lombok.Data;

@Data
public class SampleDTO {
	private String name;
	private int age;
}

net.deveopia.spring02.mapper

TimeMapper.java

package net.developia.spring02.mapper;

import org.apache.ibatis.annotations.Select;

public interface TimeMapper {
	@Select("SELECT sysdate FROM dual")
	public String getTime();
	
	public String getTime2();
}

net.developia.spring02.sample

Chef.java

package net.developia.spring02.sample;

import org.springframework.stereotype.Component;

import lombok.Data;

@Component
@Data
public class Chef {

}

Restaurant.java

package net.developia.spring02.sample;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import lombok.Data;
import lombok.Setter;

@Component
@Data
public class Restaurant {

	@Autowired
	private Chef chef;
}

UserDTO.java

package net.developia.spring02.sample;

import lombok.Data;

@Data
public class UserDTO {
	private String usr_id;
	private String usr_name;
	private String usr_pw;
}

net/developia/spring02/mapper

TimeMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="net.developia.spring02.mapper.TimeMapper">

	<select id="getTime2" resultType="string">
	SELECT to_char(sysdate,'YYYY/MM/DD HH24:MI:SS') 
	FROM dual
	</select>
</mapper>

net/log4jdbc.log4j2.properties

log4jdbc.spylogdelegator.name=net.sf.log4jdbc.log.slf4j.Slf4jSpyLogDelegator

net/sample.properties

upload_path=/Users/jeong-yoon/dev/upload/tmp

net.developia.spring02.persistence

DataSourceTests.java

package net.developia.spring02.persistence;

import static org.junit.Assert.fail;

import java.sql.Connection;

import javax.sql.DataSource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import lombok.extern.log4j.Log4j;

@WebAppConfiguration
@RunWith(SpringRunner.class)
@ContextConfiguration("file:**/*-context.xml")
@Log4j
public class DataSourceTests {
	
	@Autowired
	private DataSource dataSource;
	
	@Test
	public void testConnection() {
		try (Connection con = dataSource.getConnection()){
			log.info(con);
		} catch(Exception e) {
			fail(e.getMessage());;
		}
	}
}

JDBCTests.java

package net.developia.spring02.persistence;

import static org.junit.Assert.fail;

import java.sql.Connection;
import java.sql.DriverManager;

import org.junit.Test;

import lombok.extern.log4j.Log4j;

@Log4j
public class JDBCTests {
	static {
		try {
			Class.forName("oracle.jdbc.OracleDriver");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void testConnection() {
		
		try(Connection con = DriverManager.getConnection(
				"jdbc:oracle:thin:@localhost:1521/xepdb1",
				"ace",
				"ace"
				)){
			log.info(con);	
		} catch(Exception e) {
			fail(e.getMessage());
		}
	}
}

TimeMapperTests.java

package net.developia.spring02.persistence;

import static org.junit.Assert.fail;

import java.sql.Connection;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import lombok.extern.log4j.Log4j;
import net.developia.spring02.mapper.TimeMapper;

@WebAppConfiguration
@RunWith(SpringRunner.class)
@ContextConfiguration("file:**/*-context.xml")
@Log4j
public class TimeMapperTests {
	
	@Autowired
	private TimeMapper timeMapper;
	
	@Test
	public void testConnection() {
		log.info(timeMapper.getClass().getName());
		log.info(timeMapper.getTime());
		log.info(timeMapper.getTime2());
	}
}

net.developia.spring02.sample

SampleTests.java

package net.developia.spring02.sample;

import static org.junit.Assert.assertNotNull;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import lombok.extern.log4j.Log4j;
@WebAppConfiguration
@RunWith(SpringRunner.class)
@ContextConfiguration("file:**/*-context.xml")
@Log4j
public class SampleTests {
	@Autowired
	private Restaurant restaurant;
	
	@Test
	public void testExist() {
		assertNotNull(restaurant);
		
		log.info(restaurant);
		log.info("--------------------");
		log.info(restaurant.getChef());
	}
	
    @Test
    public void testUsrDTO() throws  Exception {
       UserDTO userDTO = new UserDTO();
       userDTO.setUsr_id("next");
       userDTO.setUsr_name("신해철");
       userDTO.setUsr_pw("1234");
       log.info(userDTO.toString());
       assertNotNull(userDTO);
    }
    
    @Test(timeout = 2000)
    public void create_usrDTO() {
        UserDTO userDTO = new UserDTO();
        try {
         Thread.sleep(1000);
      } catch (InterruptedException e) {
         e.printStackTrace();
      }
        assertNotNull(userDTO);
        log.info("생성완료~!");
    }

    @Test
    //@Disabled
    @Ignore
    public void create_usrDTO_2() {
       log.info("일단 실행하지 않음");
    }

    //@BeforeAll
    @BeforeClass
    public static void beforeClass() {
       log.info("가장 처음 1번 실행");
    }

    //@AfterAll
    @AfterClass
    public static void afterClass() {
       log.info("가장 마지막 1번 실행");
    }

    //@BeforeEach //주석은 JUnit5
    @Before
    public void before() {
       log.info("각 메소드 테스트 전 실행");
    }

    //@AfterEach
    @After
    public void after() {
       log.info("각 메소드 테스트 후 실행");
    }
}

src/test/resources

log4j.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

	<!-- Appenders -->
	<appender name="console" class="org.apache.log4j.ConsoleAppender">
		<param name="Target" value="System.out" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%-5p: %c - %m%n" />
		</layout>
	</appender>
	
	<!-- Application Loggers -->
	<logger name="net.developia.spring02">
		<level value="info" />
	</logger>
	
	<!-- 3rdparty Loggers -->
	<logger name="org.springframework.core">
		<level value="info" />
	</logger>	
	
	<logger name="org.springframework.beans">
		<level value="info" />
	</logger>
	
	<logger name="org.springframework.context">
		<level value="info" />
	</logger>

	<logger name="org.springframework.web">
		<level value="info" />
	</logger>


	<logger name="jdbc.audit">
		<level value="warn" />
	</logger>
	<logger name="jdbc.resultset">
		<level value="warn" />
	</logger>
	<logger name="jdbc.connection">
		<level value="warn" />
	</logger>
	
	
	<!-- Root Logger -->
	<root>
		<priority value="info" />
		<appender-ref ref="console" />
	</root>
	
</log4j:configuration>

src/main/webapp/WEB-INF/spring

appServlet/servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:beans="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

	<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
	
	<!-- Enables the Spring MVC @Controller programming model -->
	<annotation-driven />

	<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
	<resources mapping="/resources/**" location="/resources/" />

	<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
	<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<beans:property name="prefix" value="/WEB-INF/views/" />
		<beans:property name="suffix" value=".jsp" />
	</beans:bean>
	
	<context:component-scan base-package="net.developia.spring02.controller" />
</beans:beans>

root-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
	xmlns:c="http://www.springframework.org/schema/c"
	xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
		http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
	
	<!-- Root Context: defines shared resources visible to all other web components -->

	<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig"
		p:driverClassName="net.sf.log4jdbc.sql.jdbcapi.DriverSpy"
		p:jdbcUrl="jdbc:log4jdbc:oracle:thin:@localhost:1521/xepdb1"
		p:username="ace"
		p:password="ace" />
	
	<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close"
		c:configuration-ref="hikariConfig" />
		
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
		p:dataSource-ref="dataSource" />

	<mybatis-spring:scan base-package="net.developia.spring02.mapper"/>
	<context:component-scan base-package="net.developia.spring02.sample" />
	
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="defaultEncoding" value="utf-8"></property>
		<property name="maxUploadSize" value="104857560"></property>
		<property name="maxUploadSizePerFile" value="2097152"></property>
		<property name="uploadTempDir" value="file:/Users/jeong-yoon/dev/upload/tmp"></property>
		<property name="maxInMemorySize" value="10485756"></property>
	</bean>
</beans>

src/main/webapp/WEB-INF/views

sample/exUpload.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form method="post" enctype="multipart/form-data">
<div>
	<input type='file' name='files'>
</div>
<div>
	<input type='file' name='files'>
</div>
<div>
	<input type='file' name='files'>
</div>
<div>
	<input type='file' name='files'>
</div>
<div>
	<input type='file' name='files'>
</div>
<div>
	<input type='submit'>
</div>
</form>
</body>
</html>

home.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
	<title>Home</title>
</head>
<body>
<h1>
	Hello world!  
</h1>

<P>  The time on the server is ${serverTime}. </P>
</body>
</html>

src/main/webapp/WEB-INF

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

	<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/spring/root-context.xml</param-value>
	</context-param>
	
	<!-- Creates the Spring Container shared by all Servlets and Filters -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- Processes application requests -->
	<servlet>
		<servlet-name>appServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
		
	<servlet-mapping>
		<servlet-name>appServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

실행화면

http://localhost:8080/sample/exUpload



추가 내용

  1. Fast : 테스트는 완전히 자동화 되어야 하고 [빠르게] 결과 확인이 가능해야 한다.
  2. Independent : 테스트는 다른 테스트에 의존하지 않고 [독립적으로] 테스트 가능해야 한다.
  3. Repeatable : 운영환경외의 환경에서도 실행 가능해야 하고 [반복 실행]이 가능해야 한다.
  4. Self-validation : 테스트는 true, false중 하나의 값을 결과로 [자가 검증] 한다.
    두 가지 외에 직접 실행해야 확인이 되는 주관적인 결과를 배제하기 위해서이다.
  5. Timely : 단위테스트는 제품 코드를 구현하기 전에 작성하는 것이 좋다.
    (논쟁의 여지가 있음)

스프링 테스트 추가

      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-test</artifactId>
         <version>${org.springframework-version}</version>
      </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

단위테스트 도구 JUnit4

      <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>4.12</version>
      </dependency>

단위테스트 도구 JUnit5

      <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
      <dependency>
         <groupId>org.junit.jupiter</groupId>
         <artifactId>junit-jupiter-api</artifactId>
         <version>5.7.2</version>
         <scope>test</scope>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.mockito/mockito-junit-jupiter -->
      <dependency>
         <groupId>org.mockito</groupId>
         <artifactId>mockito-junit-jupiter</artifactId>
         <version>3.10.0</version>
         <scope>test</scope>
      </dependency>

@WebAppConfiguration 추가

JUnit 4 애노테이션
@Test
: 메소드 위에 선언되면 이 메소드는 테스트 대상 메소드임을 의미
@Test(timeout=5000)
: 이 테스트 메소드가 결과를 반환하는데 5,000밀리 초를 넘긴다면 이 테스트는 실패입니다.
( 시간단위는 밀리 초 )
@Test(expected=RuntimeException.class)
: 이 테스트 메소드는 RuntimeException이 발생해야 테스트가 성공, 그렇지 않으면 실패입니다.
@BeforeClass, @AfterClass
: 해당 테스트 클래스에서 딱 한 번씩만 수행되도록 지정하는 어노테이션 입니다.
@Before, @After
: 해당 테스트 클래스 안에 메소드들이 테스트 되기 전과 후에 각각 실행되게 지정하는
어노테이션 입니다. ( 테스트할 메소드가 호출될때 실행 됨 )

JUnit 4 메소드
fail(), fail("message") : (메세지와 함께)테스트를 실패
assertEquals(a,b) : 객체 a와 b의 값이 같은지 확인
assertArrayEquals(a,b) : 객체 a와 b의 배열이 동일한지 확인
assertNotSame(a,b) : 객체 a와 b가 같은 객체 아님을 확인
assertSame(a,b) : 객체 a와 b가 같은 객체임을 확인
assertNotNull(a) : a객체가 null이 아님을 확인
assertTrue(a) : a가 참인지 확인
assertFalse(a) : a가 거짓인지 확인
assertThat(a, matcher) : a를 만족하는 조건인지 확인

테스트 디렉토리에 테스트 클래스를 만들고 실행

package net.developia.board.dto;

import static org.junit.Assert.assertNotNull;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.log;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RunWith(SpringRunner.class)
@ContextConfiguration(locations = {"classpath:**/*-context.xml"})
public class UserDTOTest {
    @Test
    public void testUsrDTO() throws  Exception {
       UserDTO userDTO = new UserDTO();
       userDTO.setUsr_id("next");
       userDTO.setUsr_name("신해철");
       userDTO.setUsr_pw("1234");
       log.info(userDTO.toString());
       assertNotNull(userDTO);
    }
}
  1. 스프링 프레임워크에서 테스트
package net.developia.board.dto;

import static org.junit.Assert.assertNotNull;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.jupiter.api.DisplayName;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RunWith(SpringRunner.class)
@ContextConfiguration(locations = {"classpath:**/*-context.xml"})
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
// @DisplayName("JUnit 예제")   
public class UserDTOTest {
   @Test
    public void testUsrDTO() throws  Exception {
       UserDTO userDTO = new UserDTO();
       userDTO.setUsr_id("next");
       userDTO.setUsr_name("신해철");
       userDTO.setUsr_pw("1234");
       log.info(userDTO.toString());
       assertNotNull(userDTO);
   }
   
    @Test(timeout = 2000)
    @DisplayName("유저DTO 생성")
    public void create_usrDTO() {
        UserDTO userDTO = new UserDTO();
        try {
         Thread.sleep(3000);
      } catch (InterruptedException e) {
         e.printStackTrace();
      }
        assertNotNull(userDTO);
        log.info("생성완료~!");
    }

    @Test
    //@Disabled
    @Ignore
    public void create_usrDTO_2() {
       log.info("일단 실행하지 않음");
    }

    //@BeforeAll
    @BeforeClass
    public static void beforeClass() {
       log.info("가장 처음 1번 실행");
    }

    //@AfterAll
    @AfterClass
    public static void afterClass() {
       log.info("가장 마지막 1번 실행");
    }

    //@BeforeEach //주석은 JUnit5
    @Before
    public void before() {
       log.info("각 메소드 테스트 전 실행");
    }

    //@AfterEach
    @After
    public void after() {
       log.info("각 메소드 테스트 후 실행");
    }
}
  1. 부트에서 테스트
package net.developia.board.dto;

import static org.junit.Assert.assertNotNull;

import org.junit.Test;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RunWith(SpringRunner.class)
@ContextConfiguration(locations = {"classpath:**/*-context.xml"})
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
@DisplayName("JUnit Example")
public class UserDTOTest {
   @Test
    public void testUsrDTO() throws  Exception {
       UserDTO userDTO = new UserDTO();
       userDTO.setUsr_id("next");
       userDTO.setUsr_name("신해철");
       userDTO.setUsr_pw("1234");
       log.info(userDTO.toString());
       assertNotNull(userDTO);
   }
   
    @Test
    @DisplayName("Example")
    public void create_new_usrDTO() {
        UserDTO userDTO = new UserDTO();
        assertNotNull(userDTO);
        log.info("생성완료");
    }

    @Test
    @Disabled
    public void create_new_usrDTO_again() {
       log.info("일단 실행하지 않음");
    }

    @BeforeAll
    public static void beforeAll() {
       log.info("가장 처음 1번 실행");
    }

    @AfterAll
    public static void afterAll() {
       log.info("가장 마지막 1번 실행");
    }

    @BeforeEach
    public void beforeEach() {
       log.info("각 메소드 테스트 전 실행");
    }

    @AfterEach
    public void afterEach() {
       log.info("각 메소드 테스트 후 실행");
    }
}

-> @BeforeAll이나 @AfterAll은 반드시 static void 타입으로 한다.


prefix/suffix 적용 servlet-context
Article list- controller
지금 전부 net,developia.spring01. 위에 있어서 Article만 실행하기 위해서
<context:component-scan base-package="net.developia.spring01.article" />
@RequestMapping(value="/article/list", method= {RequestMethod.GET, RequestMethod.POST})
-> Controller에 대해서는 @Controller
요청에 대해서는 Mapping 해줘야 한다.
나머지 article에 대해서도 해주기
articleservice는 service를
articledao는 repository
insert와 insertaction 합치기
insertaction 삭제
// @GetMapping("/insert")
// public ModelAndView insert(HttpServletRequest request, HttpServletResponse response) {
//
// return new ModelAndView("article/insert");
// }

->

@GetMapping("/insert")
public String insert() {

return "article/insert"; //이게 View가 된다.
}

이거 이렇게 바꾼다

profile
개발 기록장

0개의 댓글