
✏️ Junit이란?
TestFramework로 Test의 자동화를 도와주는 프레임워크이다.
TDD(Test-Driven-Development)라고도 불리며 '테스트 주도 개발'이라는 의미를 가진다.
Junit이라는 테스트 프레임워크를 이용하여 테스트를 자동화하면 테스트할 코드가 많아도 일괄적으로 실행하여
어떤 테스트가 실패했고 성공했는지 한 번에 여부를 확인할 수 있다는 이점을 지닌다.
예제를 통해서 학습해보자.
아래의 코드는 root-context.xml이다.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/springbasic?useUnicode=true&characterEncoding=utf8"></property>
<property name="username" value="kkk"></property>
<property name="password" value="000000"></property>
</bean>
</beans>
아래의 코드는 DBConnectionTest.java이다.
public class DBConnectionTest2Test {
@Test
public void springJdbcConnectionTest() throws Exception {
ApplicationContext ac = new GenericXmlApplicationContext("file:src/main/webapp/WEB-INF/spring/**/root-context.xml");
DataSource ds = ac.getBean(DataSource.class);
Connection conn = ds.getConnection(); // 데이터베이스의 연결을 얻는다.
System.out.println("conn = " + conn);
assertTrue(conn!=null); // 괄호 안의 조건식이 true면, 테스트 성공, 아니면 실패
}
}
참고로 테스트시 조건은
1️⃣@Test 어노테이션을 메소드 위에 선언하여 해당 메소드가 테스트의 대상임을 명시한다.
2️⃣메소드는 public void여야 한다.
위의 테스트 시나리오는 다음과 같다.
root-context.xml에 작성된 mySql과 connection을 맺는 dataSource빈을 테스트 코드에서 context를 생성하여 getBean을 통해 읽어온다.
그 후 dataSource 빈을 활용하여 connection을 맺고 assertTrue()를 통해 connection이 null 이 아니면 테스트를 통과한다.
테스트를 통과하면 아래와 같이 Test passed가 뜬다.

하지만 위의 코드에는 조금 효율성이 떨어지는 측면이 있다.
테스트를 진행할 때마다 ApplicationContext를 생성하므로 성능적으로 다소 떨어진다.
위의 코드를 @Runwith과 @ContextConfiguration으로 개선하면 아래와 같다.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"file:src/main/webapp/WEB-INF/spring/**/root-context.xml"})
public class DBConnectionTest2Test {
@Autowired
DataSource ds;
@Test
public void springJdbcConnectionTest() throws Exception {
Connection conn = ds.getConnection(); // 데이터베이스의 연결을 얻는다.
System.out.println("conn = " + conn);
assertTrue(conn!=null); // 괄호 안의 조건식이 true면, 테스트 성공, 아니면 실패
}
위의 두 어노테이션을 사용하기 위해서는 pom.xml에 Spring TestContext Framework의 의존성 주입이 있어야 한다.
또한 Junit의 버전은 4.12 이상을 사용하여야 한다.
@RunWith은 지정한 클래스(SpringJUnit4ClassRunner)를 이용해서 AplicationContext를 자동으로 생성해주는 역할을 한다.
@ContextConfiguration은 root-context.xml 즉 테스트에 사용할 xml 설정 파일 위치 명시하여 준다.
이 두 어노테이션을 활용함으로써 개선하기 이전처럼 AplicationContext를 따로 생성하고 getBean을 작성할 필요 없이 @Autowired를 통해 DataSource를 주입받아 사용한다.
또한 개선 이전에는 ApplicationContext를 실행할 때마다 생성하였지만 하나의 ac를 계속 재사용하기 때문에 성능적으로도 이점이 있다.
'Spring' 카테고리의 다른 글
| (Spring)Transaction이란? (0) | 2022.09.26 |
|---|---|
| (Spring)@GetMapping & @PostMapping (0) | 2022.09.04 |
| (Spring)@WebDataBinder (0) | 2022.09.04 |
| (Spring)@ModelAttribute (0) | 2022.09.04 |
| (Spring)@RequestParam (0) | 2022.09.04 |