JAVA WEB PROJECT/REFACTORING
[JUNIT] 스프링 테스트를 활용해 JUnit 테스트하기
ebson
2022. 8. 15. 14:52
스프링 테스트를 활용해 JUnit 테스트하기
- speing-test 의존성 추가
<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<!-- <version>5.2.12.RELEASE</version> -->
<version>${org.springframework-version}</version>
<!-- <scope>test</scope>-->
</dependency>
- JUnit4라이브러리 추가하기
- 이클립스에서 프로젝트 우클릭 > properties > java build path > Libraries > Add Library > JUnit > JUnit4
- 테스트 클래스 작성하기
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"file:src/main/webapp/WEB-INF/spring/root-context.xml",
"file:src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml"})
public class UserDAOJUnitTest {
…
}
- 테스트 성공여부 확인하기
- 주의사항
- 스프링 버전과 스프링 테스트 버전이 맞지 않으면 초기화 오류가 발생함
- JUnit 라이브러리를 지정된 러너 버전으로 추가하지 않으면 초기화 오류가 발생함