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이 사용되었다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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 코드로 변경된 것을 확인할 수 있다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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


Popit은 페이스북 댓글만 사용하고 있습니다. 페이스북 로그인 후 글을 보시면 댓글이 나타납니다.