Spring 사용을 위한 설정
- pom.xml 수정 - spring-context 추가
...
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>5.2.12.RELEASE</spring.version>
</properties>
...
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
...
- ApplicationConfig.java 추가
package org.edwith.webbe.calculatorcli;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages= {"org.edwith.webbe.calculatorcli"})
public class ApplicationConfig {
}
- CalculatorService.java 수정 - @Component 추가
package org.edwith.webbe.calculatorcli;
import org.springframework.stereotype.Component;
@Component
public class CalculatorService {
public int plus(int value1, int value2) {
return value1 + value2;
}
public int minus(int value1, int value2) {
return value1 - value2;
}
public int multiple(int value1, int value2) {
return value1 * value2;
}
public int divide(int value1, int value2) throws ArithmeticException {
return value1 / value2;
}
}
- Main.java 추가
package org.edwith.webbe.calculatorcli;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ApplicationConfig.class);
CalculatorService calculatorService = applicationContext.getBean(CalculatorService.class);
System.out.println(calculatorService.plus(10, 50));
}
}
테스트 클래스를 Spring Bean 컨테이너를 사용하도록 수정
- 기존 테스트 클래스는 테스트할 객체를 @Before가 붙은 메서드에서 초기화 하였다. (JUnit 참고)
- 하지만 Spring Bean 컨테이너 사용할 때는 개발자가 직접 인스턴스를 생성하면 안된다.
- Spring Bean 컨테이너가 Bean을 생성하고 관리하도록 하고, 그 Bean을 테스트 해야한다.
- 이를 위해 Spring은 특별한 기능을 제공한다.
package org.edwith.webbe.calculatorcli;
import org.junit.Assert;
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.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {ApplicationConfig.class})
public class CalculatorServiceTest {
@Autowired
CalculatorService calculatorService;
@Test
public void plus() throws Exception{
int value1 = 10;
int value2 = 5;
int result = calculatorService.plus(value1, value2);
Assert.assertEquals(15, result);
}
@Test
public void divide() throws Exception{
int value1 = 10;
int value2 = 5;
int result = calculatorService.divide(value1, value2);
Assert.assertEquals(2, result);
}
@Test
public void divideExceptionTest() throws Exception{
int value1 = 10;
int value2 = 0;
try {
calculatorService.divide(value1, value2);
}catch(ArithmeticException ae) {
Assert.assertTrue(true);
return;
}
Assert.assertTrue(false);
}
}