assertThat()으로 null 여부 확인하기
모하니?/Coding : 2008/07/30 14:34
JUnit 4.4 전에는 assertNotNull(dao); 이런식으로 확인했었습니다.
그러나 4.4부터 assertThat()을 사용해서 다음과 같이 테스트 할 수 있습니다.
assertThat(dao, is(notNullValue()));
여기서 is를 빼면 어법이 좀 그렇긴 하지만.. 그래도 동작합니다.
assertThat(dao, notNullValue());
처음엔 이렇게 작성했었습니다.
assertThat(dao, is(not(null));
하지만 위에 코드는 안 됩니다. null이 값이 아니기 때문에, 값 비교 하는데 null 때문에 NullPointerException이 발생합니다.
Maven을 사용하고 계시다면..
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.4</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.1</version>
</dependency>
이렇게 라이브러리를 추가해주신 다음에 해야 합니다. static import를 추가해두면 편하겠죠.
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
그러나 4.4부터 assertThat()을 사용해서 다음과 같이 테스트 할 수 있습니다.
assertThat(dao, is(notNullValue()));
여기서 is를 빼면 어법이 좀 그렇긴 하지만.. 그래도 동작합니다.
assertThat(dao, notNullValue());
처음엔 이렇게 작성했었습니다.
assertThat(dao, is(not(null));
하지만 위에 코드는 안 됩니다. null이 값이 아니기 때문에, 값 비교 하는데 null 때문에 NullPointerException이 발생합니다.
Maven을 사용하고 계시다면..
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.4</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.1</version>
</dependency>
이렇게 라이브러리를 추가해주신 다음에 해야 합니다. static import를 추가해두면 편하겠죠.
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
'모하니? > Coding' 카테고리의 다른 글
| JavaMail - POP3로 메일 읽어오기 (0) | 2008/08/06 |
|---|---|
| JavaMail 에러 잡기 - javax.mail.MessagingException: [EOF] (0) | 2008/08/06 |
| Mockito - Verifying exact number of invocations / at least once / never (0) | 2008/07/31 |
| Mockito - How about some stubbing? (0) | 2008/07/31 |
| 5시에 들어온 미션~ (0) | 2008/07/30 |
| assertThat()으로 null 여부 확인하기 (0) | 2008/07/30 |
| 하이버네이트 Criteria 다루기 - 중복일까 아닐까 (2) | 2008/07/30 |
| log4j 설정 파일 위치를 명시적으로 설정하고 싶을 때.. (4) | 2008/07/23 |
| 바코드를 출력 및 읽기 코딩할 때 주의할 것 (0) | 2008/07/23 |
| 서비스 계층 다이어트 시키기 (0) | 2008/07/15 |
| 상태 기반 테스트란? (0) | 2008/07/08 |





