공부

AssertJ, JUnitSoftAssertions

콩줄기 2019. 1. 18. 17:06
AssertJ

AssertJ, JUnitSoftAssertions

  • 설치 : build.gradle
testCompile('org.assertj:assertj-core:3.10.0')

 

 

테스트

  • Fruit 객체 생성
public class Fruit {
    private String name;
    private String color;
    private int price;
    ...
}

 

  • Test 객체 생성

    • strawberry와 apple은 이름, 가격이 다름. 색상은 같음
import org.assertj.core.api.JUnitSoftAssertions;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

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

public class FruitTest {
    private Fruit strawberry;
    private Fruit apple;

    @Rule
    public JUnitSoftAssertions softly = new JUnitSoftAssertions();

    @Before
    public void setUp() throws Exception {
        strawberry = new Fruit("strawberry", "red", 1000);
        apple = new Fruit("banana", "red", 2000);
    }
}

 

  • AssertThat()
    @Test
    public void AssertThat() {
        assertThat(strawberry.getName()).isEqualTo(apple.getName());
        assertThat(strawberry.getColor()).isEqualTo(apple.getColor());
        assertThat(strawberry.getPrice()).isEqualTo(apple.getPrice());
    }
org.junit.ComparisonFailure: 
Expected :"banana"
Actual   :"strawberry"
 <Click to see difference>

 

  • JUnitSoftAssertions()
    @Test
    public void JUnitSoftAssertions() {
        softly.assertThat(strawberry.getName()).isEqualTo(apple.getName());
        softly.assertThat(strawberry.getColor()).isEqualTo(apple.getColor());
        softly.assertThat(strawberry.getPrice()).isEqualTo(apple.getPrice());

    }
org.junit.ComparisonFailure: 
Expected :"banana"
Actual   :"strawberry"
 <Click to see difference>

org.junit.ComparisonFailure: 
Expected :2000
Actual   :1000
 <Click to see difference>

  • AssertThat()은 검증에 실패하면 거기서 검사 끝!
  • JUnitSoftAssertions()을 사용하면 도중에 검증이 실패해도 나머지 검사가 수행됨.