반응형
@RunWith(SpringRunner.class) 매핑 시 initializationError 발생
1) 발생
package jpabook.jpashop;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.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;
@RunWith(SpringRunner.class)
@SpringBootTest
public class MemberRepositoryTest {
@Autowired
MemberRepository memberRepository;
@Test @Transactional
public void testMember() throws Exception {
//given
Member member = new Member();
member.setUsername("memberA");
//when
Long savedId = memberRepository.save(member);
Member findMember = memberRepository.find(savedId);
//then
Assertions.assertThat(findMember.getId()).isEqualTo(member.getId());
Assertions.assertThat(findMember.getUsername()).isEqualTo(member.getUsername());
}
}
위의 코드를 실행한 결과 아래와 같은 에러가 발생
2) 원인
@RunWith는 Junit4일때 사용하는데, 현재 스프링 부트의 경우 JUnit5를 기본으로 사용해서 해당 코드를 제거해야 동작한다.
3) 해결
package jpabook.jpashop;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.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;
@SpringBootTest
public class MemberRepositoryTest {
@Autowired
MemberRepository memberRepository;
@Test @Transactional
public void testMember() throws Exception {
//given
Member member = new Member();
member.setUsername("memberA");
//when
Long savedId = memberRepository.save(member);
Member findMember = memberRepository.find(savedId);
//then
Assertions.assertThat(findMember.getId()).isEqualTo(member.getId());
Assertions.assertThat(findMember.getUsername()).isEqualTo(member.getUsername());
}
}
반응형
'Error > Spring Boot' 카테고리의 다른 글
[Spring Boot] Test 수행 시 NullPointerException 에러 (0) | 2023.07.13 |
---|---|
[Spring Boot] @Test(expected ~) 에러 (Junit4, Junit5) (0) | 2023.07.13 |
[Spring Boot] gradlew: BUILD FAILED (0) | 2023.07.12 |
[Spring Boot]쿼리 파라미터 로그 남기기, SQL 한줄 출력 해결 (0) | 2023.07.12 |
[Spring Boot] assertThat 호출 오류 해결 (0) | 2023.07.11 |