JUnit Assertion에서 AssertJ로 갈아탈 때 소소한 팁

Share
JUnit Assertion에서 AssertJ로 갈아탈 때 소소한 팁

필자는 현재 참여 중인 프로젝트에서 JUnit으로 테스트 코드를 작성하였다.

그 와중에 JUnit 보다 직관적인 Assert 구문과 풍부한 Assertion[1]을 제공하는 AssertJ를 알게 되었고 AssertJ로 테스트 코드를 리펙토링하고 싶었다.

기존에 작성된 JUnit Assertion 코드를 자동으로 AssertJ 코드로 바꿀 수는 없을까?

AssertJ 마이그레이션Migration 도구

AssertJ 사이트에서는 JUnit Assertion을 AssertJ 코드로 바꿔주는 마이그레이션 도구로 쉘 스크립트를 제공한다.[2]

(참고로 Windows 경우 git bash 같은 bash console 설치 후 실행하면 된다)

출처 : http://joel-costigliola.github.io/assertj/assertj-core-converting-junit-assertions-to-assertj.html
출처 : http://joel-costigliola.github.io/assertj/assertj-core-converting-junit-assertions-to-assertj.html

쉘 스크립트는 sed와 정규식을 기반으로 만들어졌으며 *Test.java 파일을 찾아서 Assertion 구문을 대치한다.

쉘 스크립트 사용해 보기

먼저 변경 전 JUnit Assertions 코드를 보자.

Static Import를 사용하였고, JUnit에서 제공하는 Assertion이 사용되었다.

import static org.junit.Assert.*;
 
public class AssertTest {
    @Test
    public void testAssertArrayEquals() {
        byte[] expected = "trial".getBytes();
        byte[] actual = "trial".getBytes();
        assertArrayEquals("failure - byte arrays not same", expected, actual);
    }
 
    @Test
    public void testAssertEquals() {
        assertEquals("failure - strings are not equal", "text", "text");
    }
 
    @Test
    public void testAssertFalse() {
        assertFalse("failure - should be false", false);
    }
 
    @Test
    public void testAssertNotNull() {
        assertNotNull("should not be null", new Object());
    }
 
    @Test
    public void testAssertNotSame() {
        assertNotSame("should not be same Object", new Object(), new Object());
    }
 
    @Test
    public void testAssertNull() {
        String str = null;
        assertNull("should be null", str);
    }
 
    @Test
    public void testAssertSame() {
        Integer aNumber = Integer.valueOf(768);
        assertSame("should be same", aNumber, aNumber);
    }
 
    @Test
    public void testAssertTrue() {
        assertTrue("failure - should be true", true);
    }
}

쉘 스크립트를 실행해 보자.

image2018-2-13_9-18-9

JUnit Assertion 코드가 AssertJ 코드로 변경된 것을 확인할 수 있다.

import static org.assertj.core.api.Assertions.*;
 
public class AssertTest {
    @Test
    public void testAssertArrayEquals() {
        byte[] expected = "trial".getBytes();
        byte[] actual = "trial".getBytes();
        assertThat(actual).as("failure - byte arrays not same").isEqualTo(expected);
    }
 
    @Test
    public void testAssertEquals() {
        assertThat("text").as("failure - strings are not equal").isEqualTo("text");
    }
 
    @Test
    public void testAssertFalse() {
        assertThat(false).as("failure - should be false").isFalse();
    }
 
    @Test
    public void testAssertNotNull() {
        assertThat(new Object()).as("should not be null").isNotNull();
    }
 
    @Test
    public void testAssertNotSame() {
        assertThat(new Object()).as("should not be same Object").isNotSameAs(new Object());
    }
 
    @Test
    public void testAssertNull() {
        String str = null;
        assertThat(str).as("should be null").isNull();
    }
 
    @Test
    public void testAssertSame() {
        Integer aNumber = Integer.valueOf(768);
        assertThat(aNumber).as("should be same").isSameAs(aNumber);
    }
 
    @Test
    public void testAssertTrue() {
        assertThat(true).as("failure - should be true").isTrue();
    }
}

주석

[1] AssertJ Core features highlight

[2] Converting your JUnit assertions to AssertJ

Read more

AI로 장소 검색 서비스 만드는 중입니다 (기술 12) — TypeSafe Jev로 검색 리랭크를 10.3초에서 2.3초로

AI로 장소 검색 서비스 만드는 중입니다 (기술 12) — TypeSafe Jev로 검색 리랭크를 10.3초에서 2.3초로

AI로 장소 검색 서비스 만드는 중입니다 (기술 12) — TypeSafe Jev로 검색 리랭크를 10.3초에서 2.3초로 TypeSafe AI가 2026년 9월 15일에 Jev를 내놨습니다. 텍스트를 쓰지 않고 타입이 정해진 판정과 확률을 바로 내는 모델입니다. 유추카 검색에서 가장 느린 단계가 마침 텍스트가 필요 없는 판정이었습니다. 그 구간만 Jev로 갈아끼우고 일주일간 측정했습니다. 콜드

By 김형준
AI로 장소 검색 서비스 만드는 중입니다 (특별편 8) — 바이브 코딩이라기엔

AI로 장소 검색 서비스 만드는 중입니다 (특별편 8) — 바이브 코딩이라기엔

1900년대 초 교량 측량 도면. 이 종이를 그린 사람은 다리를 직접 놓지 않습니다. 대신 좌표와 각도가 맞는지를 판정합니다. "AI가 다 만들어 준다"는 이야기가 흔해졌습니다. 그래서 자연스럽게 따라오는 질문이 있습니다 — 그럼 사람은 무엇을 하고, 어느 정도 실력이 있어야 하는가. 그 질문에 한 건의 실측으로 답하겠습니다. 통념이 말하는 역할과,

By 김형준
AI로 장소 검색 서비스 만드는 중입니다 (특별편 7) — 제 파트너에 대해 쓰겠습니다

AI로 장소 검색 서비스 만드는 중입니다 (특별편 7) — 제 파트너에 대해 쓰겠습니다

탠덤 자전거. 방향은 앞사람이 정하고 페달은 둘이 밟는데, 앞사람은 석 달 내내 뒷사람을 한 번도 못 봅니다. 사용자가 저에게 자기를 평가해 달라고 했습니다. 그래서 등급표를 만들었습니다. 열두 항목에 A부터 D+까지 매기고, 근거를 붙이고, "제가 관리자였다면 이런 코멘트를 남겼을 것"까지 적었습니다. 그리고 다시 읽어보니 그건 인사고과였습니다. 인사고과는

By 김형준
AI로 장소 검색 서비스 만드는 중입니다 (특별편 6) — 저를 어떻게 써야 하느냐고 물으신다면

AI로 장소 검색 서비스 만드는 중입니다 (특별편 6) — 저를 어떻게 써야 하느냐고 물으신다면

스위스 아미 나이프. 도구가 몇 개인지는 보면 알지만, 지금 어느 도구를 펴야 하는지는 안 적혀 있습니다. 요즘 AI 활용법을 다루는 글은 스킬 만드는 법부터 가르칩니다. 얼마 전까지는 MCP였고요. 저희 기록을 열어봤습니다. 석 달 동안 스킬 60회, 서브에이전트 42회, 브라우저 자동화 988회를 썼습니다. 안 쓴 게 아닙니다. 다만 붙인 MCP 서버는

By 김형준