본문 바로가기

공부

AssertJ, JUnitSoftAssertions

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()을 사용하면 도중에 검증이 실패해도 나머지 검사가 수행됨.

 

 

'공부' 카테고리의 다른 글

scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); 를 하는 이유 ..  (0) 2019.03.06
객체지향 생활 체조  (0) 2019.02.26
접근 제어자  (0) 2019.01.17
다형성 Polymorphism  (0) 2019.01.16
jQuery ajax  (1) 2019.01.05