예를 들어 obj라는 객체에 class clazz 변수에 com.object.testClass 를 넣었을때
assertThat 으로 검증하려며 어떤식으로 해야 할까요?
전 assertThat(obj.getClazz(), is(com.object.testClass.class)); 식으로 해봤는데
요
assertEquals 로는 정상이라고 나오는데요...단지 텍스트로만 비교한건지... 잘모르겠습니다.
java.lang.AssertionError:
Expected: is an instance of com.object.testClass.class
got: <class com.object.testClass.class>
@Test
public void testHamcresIsEquals() throws Exception {
Name john = new Name("John", "Doe");
User userA = new User(john, "ia001");
assertThat(userA.getName().getClass(), is( Name.class ) );
}
실행결과는
java.lang.AssertionError:
Expected: is an instance of HamcrestTest$Name
got: <class HamcrestTest$Name>
@Test
public void testHamcresIsEquals() throws Exception {
Name john = new Name("John", "Doe");
//userA에는 위에서 만든 john이 들어가 있음
User userA = new User(john, "ia001");
// Class Type 비교
assertThat( userA.getName(), is( Name.class ) );
assertThat( john, is( Name.class ) );
//마찬가지로 Class Type 비교
Name jane = new Name("Jane", "Doe");
assertThat( jane, is( Name.class ) );
//객체 동일성 비교 확인
assertThat( userA.getName(), is( john ) );
assertThat( userA.getName(), is(not( jane ) ));
Name anotherJohn = new Name("John", "Doe");
assertThat( userA.getName(), is(not( anotherJohn ) ));
}
요약
hamcrest를 쓴다면 instanceof도 필요없습니다.
- getClass 대신 그냥 비교하세요.
끝~
:)
assertEquals( Name.class, userA.getName().getClass() );
assertEquals( Name.class, jane.getClass() );
assertEquals는 객체의 equals 비교를 하게 되어있습니다.
위 코드는 결과적으로 Class 클래스의 equals 비교를 한 셈입니다.
assertThat 의 is와는 또 다릅니다.
위 코드도 성공합니다.
:)
On 4월14일, 오후6시24분, 펭귄너구리 <door...@gmail.com> wrote:
> 아! assertEquals는 왜 성공하냐고요?
>
> assertEquals( Name.class, userA.getName().getClass() );
>
> assertEquals( Name.class, jane.getClass() );
>
> assertEquals는 객체의 equals 비교를 하게 되어있습니다.
>
> 위 코드는 결과적으로 Class 클래스의 equals 비교를 한 셈입니다.
>
> assertThat 의 is와는 또 다릅니다.
>
> 위 코드도 성공합니다.
>
> :)
>
> 2011년 4월 14일 오후 6:07, 펭귄너구리 <door...@gmail.com>님의 말:
> > 2011년 4월 14일 오후 4:52, 아름냥 <yabgar...@gmail.com>님의 말: