inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

실전! 스프링 부트와 JPA 활용1 - 웹 애플리케이션 개발

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

해결된 질문

2440

ss

작성한 질문수 1

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()
}

JPA java spring-boot spring 웹앱

답변 1

0

OMG

안녕하세요. 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()
}

 

 


감사합니다.

0

OMG

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

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

0

ss

답변 감사합니다. 

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

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

 

0

OMG

기존 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;

0

ss

감사합니다!

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;

0

OMG

오류가 나지 않는 것은

import org.assertj.core.api.Assertions;

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

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

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

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

0

ss

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

sdk 설정 오류

0

53

2

오탈자 - @Transactional

0

56

1

src/test/resources 테스트 경로 문제

0

50

1

상품 등록후 H2 db 출력 순서 바꿀 수 있나요?

0

64

1

MemberRepositoryTest 실행오류

0

81

1

boot 4.x >>> trasasction rolled back log & p6spy(영한님, 수업 자료 업데이트 해주시면 감사하겠습니다!!)

1

183

2

강의 마지막 QueryDSL 사용 부분 질문있습니다

1

142

2

클라이언트에서 isbn과 author 수정 요청을 한 경우에 대해 질문드립니다.

0

51

1

도메인 모델 패턴 vs 트랜잭션 스크립트 패턴

0

71

1

기본 생성자

0

60

1

h2 DB 연결시 jdbc url 변경 이유가 궁금합니다.

0

103

1

멤버서비스테스트 부분에서 막힙니다.

0

165

4

실무에서도 EntityManager를 이용해서 많이 작업하는 편일까요?

0

116

1

초반에 h2 다운로드 과정 꼭 필요한가요?

0

120

2

자신 필드에도 get으로 접근하는 이유가 있을까요?

0

114

1

24분 27초 연관관계 편의 메서드 위치

0

113

1

단건 주문만 가능하게 한건 의도한 부분이신가요?

0

109

2

빌드 툴, Gradle

0

59

1

h2연결은 된 것 같은데 엔티티 테이블까지 작성 후 확인해보아도 테이블이 안보입니다

0

77

2

Repository에서 EntityManager 주입 방식 차이

0

90

1

롬복과 사용자 정의 setter 메서드

0

73

1

주문 목록 조회 fetch join 질문드립니다

0

82

1

dirty checking 질문드립니다.

0

83

1

동시성 관련 질문입니다

0

75

1