안녕하세요 질문이 있어서....리....

39 views
Skip to first unread message

아름냥

unread,
Apr 14, 2011, 3:52:49 AM4/14/11
to TDDBook-QNA
객체에 클래스를 집어넣었는데요 ..

예를 들어 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>


아름냥

unread,
Apr 14, 2011, 3:52:24 AM4/14/11
to TDDBook-QNA

펭귄너구리

unread,
Apr 14, 2011, 5:07:39 AM4/14/11
to tddbo...@googlegroups.com
클래스 타입을 비교하시고 싶으셨던건지요?

설명해 주신 내용을 유추해 코드를 작성해 보았습니다.

   @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>

...

Name 클래스가 equals를 구현하지 않았다고 가정했을 때
다음 테스트 케이스는 성공하는 테스트입니다.

    @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 대신 그냥 비교하세요.


끝~

:)


2011년 4월 14일 오후 4:52, 아름냥 <yabg...@gmail.com>님의 말:

펭귄너구리

unread,
Apr 14, 2011, 5:24:10 AM4/14/11
to tddbo...@googlegroups.com
아! assertEquals는 왜 성공하냐고요?

        assertEquals( Name.class, userA.getName().getClass() );

        assertEquals( Name.class, jane.getClass() );


assertEquals는 객체의 equals 비교를 하게 되어있습니다.

위 코드는 결과적으로 Class 클래스의 equals 비교를 한 셈입니다.


assertThat 의 is와는 또 다릅니다.

위 코드도 성공합니다.

:)


2011년 4월 14일 오후 6:07, 펭귄너구리 <doo...@gmail.com>님의 말:

아름냥

unread,
Apr 14, 2011, 6:57:14 AM4/14/11
to TDDBook-QNA
아 그렇군요..ㅎㅎ 항상 빠른답변 감사합니다. 수고하셔요..^^

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>님의 말:

Yangareum

unread,
Apr 14, 2011, 6:09:20 AM4/14/11
to tddbo...@googlegroups.com
아항 그런뜻이 있었군요 항상 감사합니다 ㅎㅎ

2011. 4. 14. 오후 6:24 펭귄너구리 <doo...@gmail.com> 작성:

Reply all
Reply to author
Forward
0 new messages