Skip to content

Commit

Permalink
Merge pull request #4 from peterklijn/instanceof-matcher-fix
Browse files Browse the repository at this point in the history
Fixed incomplete instanceOf assertion
  • Loading branch information
peterklijn authored Apr 3, 2017
2 parents 73990e5 + 299a5a0 commit e14c715
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
3 changes: 3 additions & 0 deletions oleaster-matcher/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ expect(person).toBeNotNull();
// check if null
person = null;
expect(person).toBeNull();

// check if instance of
expect("Hello World!").toBeInstanceOf(String.class);
```

For comparing Objects [ObjectMatcher](https://github.com/mscharhag/oleaster/blob/master/oleaster-matcher/src/main/java/com/mscharhag/oleaster/matcher/matchers/ObjectMatcher.java) will be used.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void toBeNotNull() {
* @param expectedClass
*/
public void toBeInstanceOf(Class<?> expectedClass) {
expectTrue(value.getClass().equals(expectedClass), "Expected '%s' to be instance of '%s'", this.value, expectedClass.getName());
expectTrue(expectedClass.isInstance(value), "Expected '%s' to be instance of '%s'", this.value, expectedClass.getName());
}

protected T getValue() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,36 @@ public class ObjectMatcherTest {{

it("is ok if the value is instance of the expected class", () -> {
new ObjectMatcher<>("foo").toBeInstanceOf(String.class);

});

it("is ok if the value is of the same class", () -> {
final Animal john = new Animal("John");
new ObjectMatcher<>(john).toBeInstanceOf(Animal.class);
});

it("is ok if the value is a child of the parent class", () -> {
final Dog marie = new Dog("Marie");
new ObjectMatcher<>(marie).toBeInstanceOf(Animal.class);
});

it("is not ok if the value is a parent of the child class", () -> {
final Animal bo = new Animal("Bo");
expectAssertionError(() -> new ObjectMatcher<>(bo).toBeInstanceOf(Dog.class), "Expected '" + bo + "' to be instance of '" + Dog.class.getName() + "'");
});

});
});
}}
}
public static class Animal {
public final String name;
public Animal(final String name) {
this.name = name;
}
}
public static class Dog extends Animal {
public Dog(final String name) {
super(name);
}
}
}

0 comments on commit e14c715

Please sign in to comment.