IT/SpringBoot&AWS

[IntelliJ] error: no suitable method found for assertThat(String) 해결 방법

_하늘여우_ 2022. 5. 10. 15:47

출처 : 구글

 

출처 : 스프링 부트와 AWS로 혼자 구현하는 웹 서비스

앞서 build.gradle에 롬복 관련 dependencies를 수정 후 다시 테스트 코드를 돌리니.. 자! 또 에러가 발생했다!

error: no suitable method found for assertThat(String)

 

package com.jojoldu.book.springboot.web.dto;

import org.junit.Test;

import static org.junit.Assert.assertThat;

public class HelloResponseDtoTest {

    @Test
    public void 롬복_기능_테스트() {
        // given
        String name = "test" ;
        int amount = 1000 ;

        // when
        HelloResponseDto dto = new HelloResponseDto(name, amount) ;

        // then
        assertThat(dto.getName()).isEqualTo(name) ;
        assertThat(dto.getAmount()).isEqualTo(amount) ;
    }
}

 

코드에서 아래와 같이 import 를 잘못 설정한 것이 문제였다.

import static org.junit.Assert.assertThat;

 

Assertions 명령어에는 두 가지가 존재하는데, 

1) junit 라이브러리에서 지원하는 메소드

2) assertj 라이브러리에서 지원하는 메소드

 

junit에서 지원하는 메소드 중 assetion 객체는 assertThat 메소드를 가지고 있지 않기 때문에 위의 코드에서 에러가 발생하고(Cannot resolve method 'assertThat(String)'), 이를 assertj로 변경하면 에러가 해결된다!

import static org.assertj.core.api.Assertions.assertThat ;

 

참고: https://www.inflearn.com/questions/241439