I'm currently running Hamcrest 1.3RC on top of JUnit 4 on top of Eclipse Helios, and there's just one thing that bothers me about Hamcrest: The error messages are the wrong way around. Instead of "Expected: < expectedvalue >, but was: < actualvalue >", I get "Expected: < actualvalue> , but was: < expectedvalue >".
I mean, it's not a big thing, but come on ^^ Has really noone of the Hamcrest developers, who are doing such a great job in every other way, noticed this? Or is this an error unique to my environment? Just tell me if you've got it too or don't have it or better even,开发者_C百科 you know a way to fix this bug.
I tried it with both Hamcrest 1.2 and 1.3RC, but neither did it correctly. TIA for any kind of hint.
Some code to illustrate the issue (names are partly german, I hope it doesn't matter):
Produkt p2 = pdao.getProdukt("Kekse");
assertNotNull(p2);
assertEquals(p2.getName(), "Kekse");
assertThat(p2.getPreis().doubleValue(), closeTo(2.57, 0.01));
assertEquals(p2.getFuellmenge(), 200);
assertEquals(p2.getFuelleinheit(), "G");
assertEquals(p2.isUeber18(), false);
assertEquals(p2.isAktiv(), true);
[EDIT2] Using Hamcrest exclusively solved the problem. I'm gonna avoid the assertEquals(...,...) thing from now on in favor of the assertThat(... is(...)).
I use Hamcrest for both Java and PHP and do not have this issue. I suspect that you're passing the expected value before the actual value which is the old xUnit way of asserting things. Hamcrest opts for a more readable structure.
Here is the simplified declaration for MatcherAssert.assertThat()
:
void assertThat(T actual, Matcher<T> matcher)
Pass the actual value followed by a matcher relating it to the expected value. You can optionally pass a more descriptive message before the actual value.
void assertThat(String reason, T actual, Matcher<T> matcher)
Here are a few examples:
assertThat(add(2, 4), is(6));
assertThat($fruit->hasSeeds(), is(true));
assertThat($fruit->getColor(), containsString('red'));
Always include source code in your question. It increases your chances of being answered and--more importantly--answered correctly. ;)
Read the API docs:
http://www.junit.org/apidocs/org/junit/Assert.html
All the JUnit assertXxx methods have expectedValue first, actualValue second. You're simply calling the method with the parameters in the wrong order.
Try
assertEquals("Kekse", p2.getName());
and you'll be fine.
That's good advice in general though: read the documentation before using an API ;)
精彩评论