> ## Content Index
> Fetch the complete content index at: https://www.popit.kr/llms.txt
> Use this file to discover other available public pages before exploring further.

# JUnit Assertion에서 AssertJ로 갈아탈 때 소소한 팁
- URL: https://www.popit.kr/tips-migrating-junit-assertion-to-assertj/
- Published: 2018-02-14T01:12:11.000Z
- Updated: 2018-02-14T01:20:02.000Z
- Author: 유 영모
- Tags: #소프트웨어, Java, migration, AssertJ, JUnit

필자는 현재 참여 중인 프로젝트에서 [JUnit](https://junit.org/junit4/?ref=popit.kr)으로 테스트 코드를 작성하였다.

그 와중에 JUnit 보다 직관적인 Assert 구문과 풍부한 Assertion\[1\]을 제공하는 [AssertJ](http://joel-costigliola.github.io/assertj/index.html?ref=popit.kr)를 알게 되었고 AssertJ로 테스트 코드를 리펙토링하고 싶었다.

기존에 작성된 [JUnit Assertion](https://github.com/junit-team/junit4/wiki/Assertions?ref=popit.kr) 코드를 자동으로 AssertJ 코드로 바꿀 수는 없을까?

# AssertJ 마이그레이션Migration 도구

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

(참고로 Windows 경우 [git bash](https://gitforwindows.org/?ref=popit.kr) 같은 bash console 설치 후 실행하면 된다)

![출처 : http://joel-costigliola.github.io/assertj/assertj-core-converting-junit-assertions-to-assertj.html](https://www.popit.kr/content/images/wp/2018/02/image2018-2-13_9-0-38-600x101.png)

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

쉘 스크립트는 [sed](https://ko.wikipedia.org/wiki/Sed%5F%28%EC%9C%A0%ED%8B%B8%EB%A6%AC%ED%8B%B0%29?ref=popit.kr)와 정규식을 기반으로 만들어졌으며 \*Test.java 파일을 찾아서 Assertion 구문을 대치한다.

# 쉘 스크립트 사용해 보기

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

[Static Import](https://docs.oracle.com/javase/7/docs/technotes/guides/language/static-import.html?ref=popit.kr)를 사용하였고, JUnit에서 제공하는 Assertion이 사용되었다.  

```java
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](https://www.popit.kr/content/images/wp/2018/02/image2018-2-13_9-18-9-600x225.png)

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

```java
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](http://joel-costigliola.github.io/assertj/assertj-core-features-highlight.html?ref=popit.kr)

\[2\] [Converting your JUnit assertions to AssertJ](http://joel-costigliola.github.io/assertj/assertj-core-converting-junit-assertions-to-assertj.html?ref=popit.kr)