스프링 부트 2.2x 버전 이후부터는 자동적으로 JUnit5가 들어간다.

 

내가 현재 듣고 있는 스프링 부트 강의는 예전 강의라 JUnit4를 사용하고 있는데 Junit5를 써보고 싶었고 현재 진행 중인 스프링 부트 프로젝트도 JUnit5를 쓸 생각이기 때문에 Junit5로 구글링해서 코드를 작성해봤다.

 

작성하면서 발견한 주요 차이점들을 적어보자면 @RunWith(SpringRunner.class)이 Junit5에선 @SpringBootTest에 포함되면서 생략 가능해졌다.

Junit4에서 사용하던 @Test(expected = 클래스명.class)이 사라지면서 

IllegalStateException thrown = assertThrows(IllegalStateException.class, () -> userService.join(user2));
assertEquals("이미 존재하는 회원입니다.", thrown.getMessage());

와 같은 코드를 사용해야했다.

 

UserService 검증 Juni4 코드

package simplebook.simpleshop.service;

import simplebook.simpleshop.Domain.User;
import simplebook.simpleshop.Repository.UserRepository;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class UserServiceTest {

 @Autowired
 UserService userService;
 @Autowired
 UserRepository userRepository;
 
 @Test
 public void 회원가입() throws Exception {
  //Given
  User user = new User();
  user.setUserName("bryn");
  //When
  Long saveId = userService.join(user);
  //Then
  assertEquals(user, userRepository.findOne(saveId));
 }
 
 @Test(expected = IllegalStateException.class)
 public void 중복_회원_예외() throws Exception {
 //Given
 User user1 = new User();
 user1.setUserName("bryn");
 User user2 = new User();
 user2.setUserName("bryn");
 //When
 userService.join(user1);
 userService.join(user2); //예외가 발생해야 한다.
 //Then
 fail("예외가 발생해야 한다.");
 }
}

 

UserService 검증 Junit5 코드

package simplebook.simpleshop.service;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import simplebook.simpleshop.Domain.User;
import simplebook.simpleshop.Repository.UserRepository;
import javax.transaction.Transactional;

@ExtendWith(SpringExtension.class)
@SpringBootTest
@Transactional
class UserServiceTest {

    @Autowired
    UserService userService;
    @Autowired
    UserRepository userRepository;

    @Test
    public void 회원가입() throws Exception {
        //Given
        User user = new User();
        user.setUserName("bryn");
        //When
        Long saveId = userService.join(user);
        //Then
        assertEquals(user, userRepository.findOne(saveId));
    }

    @Test
    public void 중복_회원_예외() throws Exception {
        //Given
        User user1 = new User();
        user1.setUserName("bryn");
        User user2 = new User();
        user2.setUserName("bryn");
        //When
        userService.join(user1);
        userService.join(user2); //예외가 발생해야 한다.
        //Then
        IllegalStateException thrown = assertThrows(IllegalStateException.class, () -> userService.join(user2));
        assertEquals("이미 존재하는 회원입니다.", thrown.getMessage());
    }

}

 

+추가)

위에서 쓰인 User domain과 다른 domain임을 참고바랍니다.

 

User 검증 Juni4 코드

import jpabook.simpleshop.domain.User;
import jpabook.simpleshop.repository.UserRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserRepositoryTest {
 @Autowired UserRepository userRepository;
 @Test
 @Transactional
 @Rollback(false)
 public void testUser() {
 
 User user = new User();
 user.setUsername("user1");
 
 Long savedId = userRepository.save(user);
 User findUser = userRepository.find(savedId);
 
 Assertions.assertThat(findUser.getId()).isEqualTo(user.getId());
 Assertions.assertThat(findUser.getUsername()).isEqualTo(user.getUsername());
 }
}

 

User 검증 Junit5 코드

package simplebook.simpleshop.User;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import javax.transaction.Transactional;

@ExtendWith(SpringExtension.class)
@SpringBootTest
class UserRepositoryTest {
    @Autowired UserRepository userRepository;

    @Transactional
    @Test
    public void testUser() throws Exception{
        User user=new User();
        user.setUserName("user1");
        
        Long saveId = userRepository.save(user);
        User findUser = userRepository.find(saveId);
        
        Assertions.assertThat(findUser.getId()).isEqualTo(user.getId());
        Assertions.assertThat(findUser.getUserName()).isEqualTo(user.getUserName());
    }
}

 

Junit5 테스트 코드를 통해 UserRepository가 정상적으로 작동함을 확인했다.

 

+ Recent posts