0

How to test if class objects are equal using FEST assertThat(...) method?

Example:

import static org.fest.assertions.api.Assertions.assertThat; 

@Test
public void test() throws Exception {
    assertThat(findClass()).isEqualTo(Integer.class);
}

private Class<?> findClass() {
    // logic for finding a class object
    return String.class;
}

The above does not compile. The isEqualTo method complains. Is there some other method I should use to test equality between class objects?

etxalpo
  • 1,146
  • 1
  • 14
  • 25

2 Answers2

1

First of all you should use AssertJ; FEST is outdated by this package. But since AssertJ shares a similar syntax, the transition will be easy.

Second, well, with AssertJ, this works out of the box... This compiles for me:

// irrelevant code, then
    assertThat(findClass()).isEqualTo(Integer.class);
}

private Class<?> findClass()
{
    return Integer.class;
}

So, maybe you have another problem?

fge
  • 119,121
  • 33
  • 254
  • 329
  • No problem ;) Note that since `Class>` "instances" are singletons, you can use `.isSameAs()` – fge May 11 '14 at 20:32
1

If you're using assertJ, the proper way to do this is with isInstanceOf() method. Always use the more specific assertJ matchers when possible. They're more semantic, and give better error results.

http://joel-costigliola.github.io/assertj/core/api/org/assertj/core/api/Assert.html#isInstanceOf(java.lang.Class)

    assertThat(object).isInstanceOf(Foo.class);
Krzysztof Wolny
  • 10,576
  • 4
  • 34
  • 46
Jazzepi
  • 5,259
  • 11
  • 55
  • 81