Java/Spring Boot JPA
[Trouble Shooting] assertThat method 를 못찾을 때
remoted
2024. 2. 1. 23:38
728x90
말 그대로 import 구문을 빠트렸을 때 발생할 수 있는 상태이다. 일반적으로는 assertion 을 좀더 가독성을 늘리기 위해서 Hamcrest matcher 와 사용되곤 하는데 JUnit 에 들어있다.
또한 Spring Boot test starter 에 의존성을 가진다
상단부에서 라이브러리를 납치해준다. IntelliJ 에서 import 하고 싶다고 마우스를 댓다간 못찾는 사태가 있다
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
JUnit 5(JUnit Jupiter)를 사용하는 경우에는 대게 JUnit 5의 Assertion 을 사용하고, 이 경우에는 직접적으로 assertThat method를 포함시키진 않는다.
하쥐만 Hamcrest matcher 를 import 구문을 상단에 포함시킴으로써 같이 사용할 수 있따.
예시
@Test
public void shouldLoginUserSuccessfully() throws Exception {
// ... setup for mockMvc.perform ...
MvcResult result = mockMvc.perform(/* your mockMvc perform call */).andReturn();
String responseBody = result.getResponse().getContentAsString();
// Use assertThat with a Hamcrest matcher
assertThat(responseBody, containsString("expectedToken"));
}
취향껏 추가해보자
JUnit 5를 사용하고 native assertion library 사용에서 말뚝을 박고 싶은 경우에는 Assertion.assertTrue 를 String.contains 와 합쳐서 쓸 수 있다.
import org.junit.jupiter.api.Assertions;
// Inside your test method
Assertions.assertTrue(responseBody.contains("expectedToken"), "Response body does not contain expected token");