About the AssertJ

About the AssertJ

테스트 코드 작성 시 가장 많이 쓰게 될 AssertJ에 대한 정리

 

📜 참고 - AssertJ Docs

 

AssertJ Overview


테스트 코드 가독성을 높이고 테스트의 유지보수를 더 쉽게 하기 위해 제작된 라이브러리이다.

JUnit이나 TestNG 또는 기타 테스트 프레임워크와 함께 사용할 수 있다.

JUnit에서 사용하기 위해서는 다음과 같은 Java 버전이 필요하다.

 

  • AssertJ 3.x - Java 8 이상 필요
  • AssertJ 2.x - Java 7 이상 필요
  • AssertJ 1.x - Java 6 이상 필요

 

AssertJ 3.x는 2.x의 모든 기능을 포함하며 람다와 같은 Java 8의 일부 기능을 지원한다.

 

AssertJ 인사말


제작자가 여가시간에 사이드 프로젝트로 만들었으며

프로젝트에 대한 기여는 언제든지 환영한다고 한다. (오픈 소스라는 뜻)

기여를 하더라도 제작자의 생활이 있기에 빠른 피드백을 주기 힘들 수 있지만,

항상 빠른 피드백을 주기 위해 최선을 다할 것이라고 한다.

 

AssertJ 시작하기


<dependency>
  <groupId>org.assertj</groupId>
  <artifactId>assertj-core</artifactId>
  <!-- use 2.9.1 for Java 7 projects -->
  <version>3.11.1</version>
  <scope>test</scope>
</dependency>

 

Maven을 사용할 경우 위와 같이 dependency를 포함시켜준다.

 

testCompile("org.assertj:assertj-core:3.11.1")

 

Gradle을 사용할 경우는 위의 코드를 추가해준다.

(Spring Boot은 기본으로 포함시켜준다)

 

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

 

AssertJ를 사용하기 위해선 항상 위의 패키지를 import 해줘야 한다.

 

assertThat(objectUnderTest). // code completion -> assertions specific to objectUnderTest

 

assertThat을 입력하고 테스트 중인 객체를 매개변수로 전달한 후 .을 입력하면 IDE에 자동완성이 뜰 것이다.

여기서 전달하는 매개변수는 Actually, 실제로 나오는 값이며 뒤에 작성되는 값은 Expected, 기댓값이다.

 

 

assertEquals(expected, actual);

 

JUnit의 위와 같은 단정문은

 

assertThat(actual).isEqualTo(expected);

 

AssertJ에서 이처럼 치환될 수 있다.

(영어처럼 읽히므로 가독성 증가)

혹은

 

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

assertThat(result, is(10));

 

Hamcrest Matchers를 import 하여 위와 같이 사용할 수도 있다.

아래는 AssertJ의 사용 예이다.

 

Converting JUnit assertions to AssertJ assertions on files matching pattern : *Test.java

 1 - Replacing : assertEquals(0, myList.size()) ............... by : assertThat(myList).isEmpty()
 2 - Replacing : assertEquals(expectedSize, myList.size()) .... by : assertThat(myList).hasSize(expectedSize)
 3 - Replacing : assertEquals(expectedDouble, actual, delta) .. by : assertThat(actual).isCloseTo(expectedDouble, within(delta))
 4 - Replacing : assertEquals(expected, actual) ............... by : assertThat(actual).isEqualTo(expected)
 5 - Replacing : assertArrayEquals(expectedArray, actual) ..... by : assertThat(actual).isEqualTo(expectedArray)
 6 - Replacing : assertNull(actual) ........................... by : assertThat(actual).isNull()
 7 - Replacing : assertNotNull(actual) ........................ by : assertThat(actual).isNotNull()
 8 - Replacing : assertTrue(logicalCondition) ................. by : assertThat(logicalCondition).isTrue()
 9 - Replacing : assertFalse(logicalCondition) ................ by : assertThat(logicalCondition).isFalse()
10 - Replacing : assertSame(expected, actual) ................. by : assertThat(actual).isSameAs(expected)
11 - Replacing : assertNotSame(expected, actual) .............. by : assertThat(actual).isNotSameAs(expected)

12 - Replacing JUnit static imports by AssertJ ones, at this point you will probably need to :
12 --- optimize imports with your IDE to remove unused imports
12 --- add "import static org.assertj.core.api.Assertions.within;" if you were using JUnit number assertions with deltas

 

그 외에 람다나 스트림 등의 assert도 굉장히 많은데,

이건 너무 많아서 추후에 또 정리할 일이 있을 것 같다.

 


© 2022. All rights reserved.