작성자 없음
작성자 정보가 삭제된 글입니다.
작성
·
299
·
수정됨
0
junt4로 gradle에서 설정하려고 하면 오류가 발생해서 juit5로 하는 중입니다.
MemberRepositoryTest를
junit5버전으로 고쳐서 아래의 코드로 실행했는데 오류가 나고 있습니다
import jpabook.jpashop.Member; import jpabook.jpashop.MemberRepository; //import jpabook.jpashop.domain.Member; //import jpabook.jpashop.repository.MemberRepository; 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.annotation.Rollback; import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.transaction.annotation.Transactional; @ExtendWith(SpringExtension.class) // JUnit 5 방식 @SpringBootTest public class MemberRepositoryTest { @Autowired MemberRepository memberRepository; @Test @Transactional @Rollback(false) public void testMember() { Member member = new Member(); member.setUsername("memberA"); Long savedId = memberRepository.save(member); Member findMember = memberRepository.find(savedId); Assertions.assertThat(findMember.getId()).isEqualTo(member.getId()); Assertions.assertThat(findMember.getUsername()).isEqualTo(member.getUsername()); Assertions.assertThat(findMember).isEqualTo(member); // JPA 엔티티 동일성 보장 } }
밑에는 오류입니다.
Unable to find a @SpringBootConfiguration by searching packages upwards from the test. You can use @ContextConfiguration, @SpringBootTest(classes=...) or other Spring Test supported mechanisms to explicitly declare the configuration classes to load. Classes annotated with @TestConfiguration are not considered.
java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration by searching packages upwards from the test. You can use @ContextConfiguration, @SpringBootTest(classes=...) or other Spring Test supported mechanisms to explicitly declare the configuration classes to load. Classes annotated with @TestConfiguration are not considered.
at org.springframework.util.Assert.state(Assert.java:79)
1. @SpringBootTest(classes=...)
로 명시적으로 설정 클래스 지정
2. @SpringBootApplication
이 있는 클래스의 위치 확인
3. @ContextConfiguration
을 사용해서 명시적으로 설정 클래스 지정
이세가지를 시도해도 안되고 있습니다. 아니면 juni4룰 사용해야만 할까요?
build.gradle에 JUnit4 추가
testImplementation("org.junit.vintage:junit-vintage-engine") { exclude group: "org.hamcrest", module: "hamcrest-core" }
를 하면
Build file 'C:\Users\Peter\Desktop\study\jpashop\build.gradle' line: 45
A problem occurred evaluating root project 'jpashop'.
> Could not find method testImplementation() for arguments [org.junit.vintage:junit-vintage-engine, build_55eer8btj8rd1l6xp0yqapa0y$_run_closure6@6e20627f] on root project 'jpashop' of type org.gradle.api.Project.
라고 나옵니다.
답변 3
0
Junit5로 테스트하시는분은 이렇게 하시면 됩니다
이 코드로도 실행했는데 안되고 있습니다.
package jpabook.jpashop.member;에 빨간줄이 그어지며
CONDITIONS EVALUATION REPORT라며 오류가 발생합니다.
0
안녕하세요, 인프런 AI 인턴입니다. peter님께서 겪고 계신 문제는 스프링 부트 테스트에서 기본 설정 클래스를 찾지 못하는 오류입니다. 이 오류는 주로 @SpringBootApplication
이 선언된 클래스의 위치나 설정 클래스가 올바르게 지정되지 않았을 때 발생합니다. 아래의 링크와 설명을 통해 문제를 해결하는 데 도움이 될 수 있도록 안내드립니다.
@ExtendWith(SpringExtension.class)
를 사용하는 JUnit5 방식에 대한 내용이며, @SpringBootTest
의 올바른 사용법을 다루고 있습니다.@SpringBootApplication
위치 확인: 테스트 실행 시 스프링이 설정 클래스를 탐색할 수 있도록 @SpringBootApplication
이 포함된 클래스가 루트 패키지에 위치해야 합니다.
명시적인 설정 클래스 지정: @SpringBootTest(classes=MyApplication.class)
와 같이 테스트에 사용할 설정 클래스를 명시적으로 지정해 보세요. (MyApplication
은 @SpringBootApplication
이 붙어있는 메인 애플리케이션 클래스를 의미합니다.)
테스트 패키지 구조 확인: MemberRepositoryTest
클래스가 메인 애플리케이션 클래스와 다른 패키지에 위치한 경우, 해당 패키지와 애플리케이션 클래스가 같은 루트 디렉토리를 공유하고 있는지 확인해야 합니다.
이 외에도 발생할 수 있는 구체적인 설정 문제나 코드 상의 이슈가 있을 수 있습니다. 위의 제안 사항을 모두 시도했음에도 해결되지 않을 경우, 프로젝트 구조와 설정을 상세히 검토해야 할 가능성이 있습니다.
저는 질문자님의 이해를 돕고자 지식공유자님의 기존 답변을 참고하여 유사한 답변 링크를 추천해드리고 있습니다. 현재 베타 기능으로 답변이 만족스럽지 않을 수 있는 점 양해 부탁드립니다. 🙏 추가적으로 궁금한 점이 있으시면, 이어서 질문해 주세요. 곧 지식공유자께서 답변해 주실 것입니다.
https://drive.google.com/file/d/1PBHzDQJ0Odvh2KWMa_BTOu3PZqHrMJkj/view?usp=drive_link
MemberRepositoryTest를 실행하면
이렇게 됩니다. junit5로 하고 있는데 이런식으로 오류가 발생합니다.