inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

스프링 입문 - 코드로 배우는 스프링 부트, 웹 MVC, DB 접근 기술

JUnit 과 AssertJ 의존성 추가 오류

349

최진욱

작성한 질문수 19

0

package hello.HelloSpring.service;
import hello.HelloSpring.domain.Member;
import hello.HelloSpring.repository.MemberRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
@SpringBootTest
@Transactional
class MemberServiceIntegrationTest {

    @Autowired MemberService memberService;
    @Autowired MemberRepository memberRepository;
    @Test
    public void 회원가입() throws Exception {
        //Given
        Member member = new Member();
        member.setName("hello");
        //When
        Long saveId = memberService.join(member); //Then
        Member findMember = memberRepository.findById(saveId).get();
        assertEquals(member.getName(), findMember.getName());
    }
    @Test
    public void 중복_회원_예외() throws Exception {
        //Given
        Member member1 = new Member();
        member1.setName("spring");
        Member member2 = new Member();
        member2.setName("spring");
        //When
        memberService.join(member1);
        IllegalStateException e = assertThrows(IllegalStateException.class,
                () -> memberService.join(member2));//예외가 발생해야 한다.
        assertThat(e.getMessage()).isEqualTo("이미 존재하는 회원입니다.");
   

 

dependencies

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
	implementation 'org.springframework.boot:spring-boot-starter-web'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
	testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
	developmentOnly("org.springframework.boot:spring-boot-devtools")
	implementation 'org.springframework.boot:spring-boot-starter-aop'
	implementation 'org.springframework.boot:spring-boot-starter-jdbc'
	runtimeOnly 'com.h2database:h2'
	testImplementation 'org.assertj:assertj-core' // AssertJ 포함
	testImplementation 'org.junit.jupiter:junit-jupiter:5.9.3'
}

 

error code

> Task :compileJava FAILED
1 actionable task: 1 executed
C:\Users\CKIRUser\Music\�� ����\Programing\�迵��\������ �Թ�\hello-spring\src\main\java\hello\HelloSpring\service\MemberIntegrationTest.java:4: error: package org.junit.jupiter.api does not exist
import org.junit.jupiter.api.Test;
                            ^
C:\Users\CKIRUser\Music\�� ����\Programing\�迵��\������ �Թ�\hello-spring\src\main\java\hello\HelloSpring\service\MemberIntegrationTest.java:6: error: package org.springframework.boot.test.context does not exist
import org.springframework.boot.test.context.SpringBootTest;
                                            ^
C:\Users\CKIRUser\Music\�� ����\Programing\�迵��\������ �Թ�\hello-spring\src\main\java\hello\HelloSpring\service\MemberIntegrationTest.java:8: error: package org.assertj.core.api does not exist
import static org.assertj.core.api.Assertions.assertThat;
                                  ^
C:\Users\CKIRUser\Music\�� ����\Programing\�迵��\������ �Թ�\hello-spring\src\main\java\hello\HelloSpring\service\MemberIntegrationTest.java:8: error: static import only from classes and interfaces
import static org.assertj.core.api.Assertions.assertThat;
^
C:\Users\CKIRUser\Music\�� ����\Programing\�迵��\������ �Թ�\hello-spring\src\main\java\hello\HelloSpring\service\MemberIntegrationTest.java:9: error: package org.junit.jupiter.api does not exist
import static org.junit.jupiter.api.Assertions.assertEquals;
                                   ^
C:\Users\CKIRUser\Music\�� ����\Programing\�迵��\������ �Թ�\hello-spring\src\main\java\hello\HelloSpring\service\MemberIntegrationTest.java:9: error: static import only from classes and interfaces
import static org.junit.jupiter.api.Assertions.assertEquals;
^
C:\Users\CKIRUser\Music\�� ����\Programing\�迵��\������ �Թ�\hello-spring\src\main\java\hello\HelloSpring\service\MemberIntegrationTest.java:10: error: package org.junit.jupiter.api does not exist
import static org.junit.jupiter.api.Assertions.assertThrows;
                                   ^
C:\Users\CKIRUser\Music\�� ����\Programing\�迵��\������ �Թ�\hello-spring\src\main\java\hello\HelloSpring\service\MemberIntegrationTest.java:10: error: static import only from classes and interfaces
import static org.junit.jupiter.api.Assertions.assertThrows;
^
C:\Users\CKIRUser\Music\�� ����\Programing\�迵��\������ �Թ�\hello-spring\src\main\java\hello\HelloSpring\service\MemberIntegrationTest.java:11: error: cannot find symbol
@SpringBootTest
 ^
  symbol: class SpringBootTest
C:\Users\CKIRUser\Music\�� ����\Programing\�迵��\������ �Թ�\hello-spring\src\main\java\hello\HelloSpring\service\MemberIntegrationTest.java:17: error: cannot find symbol
    @Test
     ^
  symbol:   class Test
  location: class MemberServiceIntegrationTest
C:\Users\CKIRUser\Music\�� ����\Programing\�迵��\������ �Թ�\hello-spring\src\main\java\hello\HelloSpring\service\MemberIntegrationTest.java:27: error: cannot find symbol
    @Test
     ^
  symbol:   class Test
  location: class MemberServiceIntegrationTest
C:\Users\CKIRUser\Music\�� ����\Programing\�迵��\������ �Թ�\hello-spring\src\main\java\hello\HelloSpring\service\MemberIntegrationTest.java:25: error: cannot find symbol
        assertEquals(member.getName(), findMember.getName());
        ^
  symbol:   method assertEquals(String,String)
  location: class MemberServiceIntegrationTest
C:\Users\CKIRUser\Music\�� ����\Programing\�迵��\������ �Թ�\hello-spring\src\main\java\hello\HelloSpring\service\MemberIntegrationTest.java:36: error: cannot find symbol
        IllegalStateException e = assertThrows(IllegalStateException.class,
                                  ^
  symbol:   method assertThrows(Class<IllegalStateException>,()->member[...]ber2))
  location: class MemberServiceIntegrationTest
C:\Users\CKIRUser\Music\�� ����\Programing\�迵��\������ �Թ�\hello-spring\src\main\java\hello\HelloSpring\service\MemberIntegrationTest.java:38: error: cannot find symbol
        assertThat(e.getMessage()).isEqualTo("�̹� �����ϴ� ȸ���Դϴ�.");
        ^
  symbol:   method assertThat(String)
  location: class MemberServiceIntegrationTest
14 errors

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileJava'.
> Compilation failed; see the compiler error output for details.

* Try:
> Run with --info option to get more log output.
> Run with --scan to get full insights.

BUILD FAILED in 1s


JUnit 과 AssertJ이 의존성에 추가해도 인식이 안되는 것 같습니다. 또한 test도 의존성에 추가해도 인식이 안되는 것 같습니다. 해본 조치 사항에는 test 코드에서 컴파일 경고가 나는 것을 확인하고 alt + enter로 직접 의존성을 추가 했고 또 gpt를 이용하여 의존성 코드를 수정해봤습니다.
이러한 조치에도 컴파일 오류는 계속됩니다 해결 방법 알려주시면 감사하겠습니다.

java spring mvc spring-boot

답변 2

0

OMG

안녕하세요. 최진욱님, 공식 서포터즈 OMG입니다.

프로젝트가 위치한 경로를 보니 경로 중 일부 깨지는 텍스트가 보이는데요

깨지는 부분이 한글일까요? 한글이나 기타 특수문자가 포함되어 있다면 영문으로 변경하여 확인해주세요 😀

image.png

해결이 안될 경우 댓글 남겨주세요.

감사합니다.

0

최진욱

> Task :compileJava FAILED
C:\Users\CKIRUser\Music\New Folder\Programing\KimYoungHan\SpringBootForBeginner\hello-spring\src\main\java\hello\HelloSpring\service\MemberIntegrationTest.java:5: error: package org.junit.jupiter.api does not exist
import org.junit.jupiter.api.Test;
                            ^
C:\Users\CKIRUser\Music\New Folder\Programing\KimYoungHan\SpringBootForBeginner\hello-spring\src\main\java\hello\HelloSpring\service\MemberIntegrationTest.java:7: error: package org.springframework.boot.test.context does not exist
import org.springframework.boot.test.context.SpringBootTest;
                                            ^
C:\Users\CKIRUser\Music\New Folder\Programing\KimYoungHan\SpringBootForBeginner\hello-spring\src\main\java\hello\HelloSpring\service\MemberIntegrationTest.java:9: error: package org.assertj.core.api does not exist
import static org.assertj.core.api.Assertions.assertThat;
                                  ^
C:\Users\CKIRUser\Music\New Folder\Programing\KimYoungHan\SpringBootForBeginner\hello-spring\src\main\java\hello\HelloSpring\service\MemberIntegrationTest.java:9: error: static import only from classes and interfaces
import static org.assertj.core.api.Assertions.assertThat;
^
C:\Users\CKIRUser\Music\New Folder\Programing\KimYoungHan\SpringBootForBeginner\hello-spring\src\main\java\hello\HelloSpring\service\MemberIntegrationTest.java:10: error: package org.junit.jupiter.api does not exist
import static org.junit.jupiter.api.Assertions.assertEquals;
                                   ^
C:\Users\CKIRUser\Music\New Folder\Programing\KimYoungHan\SpringBootForBeginner\hello-spring\src\main\java\hello\HelloSpring\service\MemberIntegrationTest.java:10: error: static import only from classes and interfaces
import static org.junit.jupiter.api.Assertions.assertEquals;
^
C:\Users\CKIRUser\Music\New Folder\Programing\KimYoungHan\SpringBootForBeginner\hello-spring\src\main\java\hello\HelloSpring\service\MemberIntegrationTest.java:11: error: package org.junit.jupiter.api does not exist
import static org.junit.jupiter.api.Assertions.assertThrows;
                                   ^
C:\Users\CKIRUser\Music\New Folder\Programing\KimYoungHan\SpringBootForBeginner\hello-spring\src\main\java\hello\HelloSpring\service\MemberIntegrationTest.java:11: error: static import only from classes and interfaces
import static org.junit.jupiter.api.Assertions.assertThrows;
^
C:\Users\CKIRUser\Music\New Folder\Programing\KimYoungHan\SpringBootForBeginner\hello-spring\src\main\java\hello\HelloSpring\service\MemberIntegrationTest.java:12: error: cannot find symbol
@SpringBootTest
 ^
  symbol: class SpringBootTest
C:\Users\CKIRUser\Music\New Folder\Programing\KimYoungHan\SpringBootForBeginner\hello-spring\src\main\java\hello\HelloSpring\service\MemberIntegrationTest.java:18: error: cannot find symbol
    @Test
     ^
  symbol:   class Test
  location: class MemberServiceIntegrationTest
C:\Users\CKIRUser\Music\New Folder\Programing\KimYoungHan\SpringBootForBeginner\hello-spring\src\main\java\hello\HelloSpring\service\MemberIntegrationTest.java:28: error: cannot find symbol
    @Test
     ^
  symbol:   class Test
  location: class MemberServiceIntegrationTest
C:\Users\CKIRUser\Music\New Folder\Programing\KimYoungHan\SpringBootForBeginner\hello-spring\src\main\java\hello\HelloSpring\service\MemberIntegrationTest.java:26: error: cannot find symbol
        assertEquals(member.getName(), findMember.getName());
        ^
  symbol:   method assertEquals(String,String)
  location: class MemberServiceIntegrationTest
C:\Users\CKIRUser\Music\New Folder\Programing\KimYoungHan\SpringBootForBeginner\hello-spring\src\main\java\hello\HelloSpring\service\MemberIntegrationTest.java:37: error: cannot find symbol
        IllegalStateException e = assertThrows(IllegalStateException.class,
                                  ^
  symbol:   method assertThrows(Class<IllegalStateException>,()->member[...]ber2))
  location: class MemberServiceIntegrationTest
C:\Users\CKIRUser\Music\New Folder\Programing\KimYoungHan\SpringBootForBeginner\hello-spring\src\main\java\hello\HelloSpring\service\MemberIntegrationTest.java:39: error: cannot find symbol
        assertThat(e.getMessage()).isEqualTo("this user already exist.");
        ^
  symbol:   method assertThat(String)
  location: class MemberServiceIntegrationTest
14 errors

 

image.png


파일명, 메소드 이름을 전부 한글로 바꿔서 시도해봤는데 불러온 패키지가 존재하지 않는다고 하는데 인텔리제이를 삭제하고 다시 설치하는게 좋을까요??

JDK는 21.0.3이고

스프링 부트는 3.3.3입니다

0

OMG

프로젝트 코드를 확인해봐야 할 것 같습니다.

실제 동작하는 전체 프로젝트를 ZIP파일로 압축해서 구글 드라이브로 공유해서 링크를 남겨주세요.

구글 드라이브 업로드 방법은 다음을 참고해주세요.

https://bit.ly/3fX6ygx

 

주의: 업로드시 링크에 있는 권한 문제 꼭 확인해주세요

 

감사합니다.

0

최진욱

의존성 관련해서는 인텔리제이 삭제후 재설치 하니 해결되었습니다. 하지만 제가 지금 cmd 창이 자꾸 꺼지는 현상이 발생해서 db서버에 연결을 못하는 상황인데 혹시 어떻게 하면 cmd 없이 db에 연결 할 수 있을까요??

0

OMG

본 질문은 의존성추가와 관련된 글이므로 질문 주제가 변경된 경우

새로 작성해주시겠어요?

자꾸 꺼지는 상황에 대한 자세한 설명(+캡쳐화면)을 포함하여 남겨주시면 영한님과 서포터즈가 도움드리도록 하겠습니다.

1

최진욱

넵 해결안되면 추가로 달겠습니다

0

인프런 AI 인턴

안녕하세요, 인프런 AI 인턴입니다. 최진욱님.

JUnit과 AssertJ 의존성을 추가했음에도 작동하지 않는 문제를 겪고 계시는군요. 이 문제는 대부분 의존성 선언이나 설정 문제로 발생할 수 있습니다. 아래에서 추가적인 조치 방법을 안내드리겠습니다.

1. Gradle 의존성 확인

Gradle 파일 설정을 다시 확인해주세요. 특히, assertj-corejunit-jupiter의 의존성을 제대로 포함했는지 확인합니다. 또한 spring-boot-starter-test는 JUnit과 AssertJ를 기본적으로 포함합니다.

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-aop'
    implementation 'org.springframework.boot:spring-boot-starter-jdbc'
    runtimeOnly 'com.h2database:h2'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.junit.jupiter:junit-jupiter:5.9.3' // JUnit Jupiter 추가
    testImplementation 'org.assertj:assertj-core:3.21.0' // AssertJ 추가
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
}

2. Gradle 리프레시

Gradle 의존성을 정리하고 새로고침합니다. 다음 명령어를 실행하거나 IDE에서 Gradle 프로젝트를 리프레시합니다.

./gradlew clean build --refresh-dependencies

3. 테스트 클래스 위치 확인

테스트 클래스가 src/test/java 디렉토리에 위치해 있는지 확인합니다. src/main/java에 위치해 있으면 테스트 프레임워크가 인식하지 못할 수 있습니다.

4. 예시 코드

MemberServiceIntegrationTest 클래스는 다음과 같습니다. 패키지 이름 및 클래스 이름을 확인 후 수정해주세요.

package hello.HelloSpring.service;

import hello.HelloSpring.domain.Member;
import hello.HelloSpring.repository.MemberRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

@SpringBootTest
@Transactional
class MemberServiceIntegrationTest {

    @Autowired
    MemberService memberService;

    @Autowired
    MemberRepository memberRepository;

    @Test
    public void 회원가입() throws Exception {
        // Given
        Member member = new Member();
        member.setName("hello");

        // When
        Long saveId = memberService.join(member);

        // Then
        Member findMember = memberRepository.findById(saveId).get();
        assertEquals(member.getName(), findMember.getName());
    }

    @Test
    public void 중복_회원_예외() throws Exception {
        // Given
        Member member1 = new Member();
        member1.setName("spring");
        Member member2 = new Member();
        member2.setName("spring");

        // When
        memberService.join(member1);
        IllegalStateException e = assertThrows(IllegalStateException.class,
                () -> memberService.join(member2)); // 예외가 발생해야 한다.
        assertThat(e.getMessage()).isEqualTo("이미 존재하는 회원입니다.");
    }
}

관련 질문

아래 유사한 질문들과 답변이 도움이 될 수 있습니다:
1. MemberServiceIntegrationTest 에러 - 테스트 클래스의 설정과 관련한 일반적인 오류와 해결 방법
2. assertj의 Assertions 를 못가져옵니다..ㅠㅠ - AssertJ 의존성 문제 해결 방법
3. test 가 되질 않아요 - 테스트 환경 설정 관련 유사 사례

저는 질문자님의 이해를 돕고자 지식공유자님의 기존 답변을 참고하여 유사한 답변 링크를 추천해드리고 있습니다.
현재 베타 기능으로 답변이 만족스럽지 않을 수 있는 점 양해 부탁드립니다. 🙏
추가적으로 궁금한 점이 있으시면, 이어서 질문해 주세요. 곧 지식공유자께서 답변해 주실 것입니다.

..

0

76

2

Unused property.....

0

99

2

project JDK is misconfigured

0

132

2

외부 API의 ID 타입(String/UUID)과 내부 도메인의 ID 타입(Long)이 불일치할 때의 설계 정석

0

83

2

단위/통합 테스트 버전충돌 문제

0

91

2

❗️springboot 4.0.2 버전 aspectj dependency 설정❗️

0

230

1

왜 컨트롤러는 변한게 없는데 새로 만든 html 파일이 뜨나요?

0

103

2

윈도우 build test오류 질문

1

102

2

테스트 관련 공부에 대한 조언을 얻고 싶습니다

0

100

2

테스트 실행 시 에러 질문

0

291

1

name을 통한 비교와 객체를 통한 비교

0

77

1

빌드 후 libs 없음

1

129

1

윈도우 gradlew.bat 에러

0

167

1

@PostMapping("/members/new")가 동작하지 않습니다

0

89

1

java static class와 kotlin class

0

79

1

스프링 DB연결

0

126

1

소요 시간

0

87

2

ddl.sql에 빨간 밑줄

1

102

2

welcome page 에러

0

183

3

잘 모르겠습니다.

0

164

2

fail을 똑같이 쳤는데 오류가 발생해요

0

156

2

index.html Welcome page

0

124

1

프로젝트 gradle-groovy ?

0

369

1

테스트코드 메서드명 한글

0

202

2