• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    해결됨

assertj의 Assertions 를 못가져옵니다..ㅠㅠ

22.03.30 10:36 작성 조회수 1.56k

0

assertj의 assertThat을 사용하시는데...저는 임포트가 안됩니다.

아래는 MemberRepositoryTest 소스입니다.

package jpabook.jpashop;

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 static org.junit.Assert.*;
import static org.assertj.core.api.Assertions;


@RunWith(SpringRunner.class)
@SpringBootTest
public class MemberRepositoryTest {

@Autowired MemberRepository memberRepository;

@Test
public void testMember() throws Exception {
// given
Member member = new Member();
member.setUsername("memberA");

// when
Long saveId = memberRepository.save(member);
Member findMember = memberRepository.find(saveId);

// then

// Assertions.assertThat(findMember.getId()).isEqualTo(member.getId());
}
}

강의에서는 junit을 따로 gradle에 추가해주시지 않았지만 저는 RunWith 등의 오류가 나서

gradle에 junit을 추가해주었습니다.

같은 방식으로 assertj를 추가해주었는데.. Assertions를 사용하진 못하고 있습니다.

build.gradle 입니다.

plugins {
id 'org.springframework.boot' version '2.6.5'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}

group = 'jpabook'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

configurations {
compileOnly {
extendsFrom annotationProcessor
}
}

repositories {
mavenCentral()
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-devtools'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'junit:junit:4.12'
testImplementation 'org.assertj:assertj-core:3.21.0'
// testImplementation group: 'org.assertj', name: 'assertj-core', version: '3.21.0'
// implementation 'junit:junit:4.12'
}

tasks.named('test') {
useJUnitPlatform()
}

답변 1

답변을 작성해보세요.

0

OMG님의 프로필

OMG

2022.03.30

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

아래의 build.gradl을 사용해주세요.

임의로 추가한 테스트 의존성(버전이 명시된)은 기존 스프링부트에 포함되어 있는것과 충돌나서 잘 동작 안하더라구요. 

추가하고 우측 상단의 코끼리 버튼을 눌러 리로드 해주세요.

 

plugins {
id 'org.springframework.boot' version '2.4.1'
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
id 'java'
}

group = 'jpabook'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

configurations {
compileOnly {
extendsFrom annotationProcessor
}
}

repositories {
mavenCentral()
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'

compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'

annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
//JUnit4 추가
testImplementation("org.junit.vintage:junit-vintage-engine") {
exclude group: "org.hamcrest", module: "hamcrest-core"
}

}

test {
useJUnitPlatform()
}

 

 


감사합니다.

OMG님의 프로필

OMG

2022.03.30

추가하고 리로드하더라도 기존 의존성의 영향인지 잘 안될 수 있습니다.

변경하고 결과를 공유해주세요.

ss님의 프로필

ss

질문자

2022.03.30

답변 감사합니다. 

위에 답변주신 build.gradle로 적용하였습니다.

그래도 아래 그림과 같이 assertj의 Assertions를 사용하진 못하고 있습니다.

 

OMG님의 프로필

OMG

2022.03.30

기존 import문을 전부 삭제하시고, 아래 내용으로 해보시겠어요?

import jpabook.jpashop.domain.Member;
import org.assertj.core.api.Assertions;
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.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
ss님의 프로필

ss

질문자

2022.03.30

감사합니다!

import jpabook.jpashop.domain.Member를 제외한 import를 하였습니다.

아까와 같이

assertj의 Assertions이 뜨진 않지만 ㅠㅠ 오류로 뜨지 않습니다! 

혹시 이유를 알 수 있을까요?

import는 아래와 같이 하였습니다.

import org.assertj.core.api.Assertions;
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.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
OMG님의 프로필

OMG

2022.03.30

오류가 나지 않는 것은

import org.assertj.core.api.Assertions;

를 추가해서 그런것같습니다.

import 목록을 보시면 아시겠지만 org.assertj.core.api가 포함되어 있긴하지만, Assertions는 제외 된 것으로 보아 의존성이 꼬인 것 같습니다.

이거 해결하기가 쉽지 않아서요. 

프로젝트를 새로 생성하고 build.gradle은 올려드린 것으로 사용하되 프로젝트 코드를 복사 붙여넣기 해서 강의 수강하실 것을 권장드립니다. 

ss님의 프로필

ss

질문자

2022.03.30

빠른 답변 감사드립니다. 그렇게 해보겠습니다 :)