I found this when trying to write a test which asserted that the user's
Principal in a ServletRequest was correctly set. For some reason the matcher
doesn't return true for this object even though it has the "name" property
which matches the string value I'm testing for.
I tried to reproduce the test with some other Principal implementation and that
worked, so I pulled a cut-down version of our own implementation into this test
case and indeed, the problem still occurs but I can't figure out why anymore.
public class TestMatchers
{
Matcher<Principal> m = Matchers.hasProperty("name", equalTo("bob"));
@Test
public void testSunPrincipal() throws Exception
{
Principal p = new PrincipalImpl("bob");
assertThat(p.getName(), is(equalTo("bob")));
assertThat(p, is(m));
}
@Test
public void testUserPrincipal() throws Exception
{
Mockery mockery = new Mockery();
final User user = mockery.mock(User.class);
mockery.checking(new Expectations() {{
allowing(user).getName(); will(returnValue("bob"));
}});
Principal p = new UserPrincipal(user);
assertThat(p.getName(), is(equalTo("bob")));
assertThat(p, is(m)); // <- fails here
}
private static interface User
{
String getName();
}
private static class UserPrincipal implements Principal
{
private final User user;
public UserPrincipal(User user)
{
this.user = user;
}
@Override
public String getName()
{
return user.getName();
}
@Override
public String toString()
{
return String.format("UserPrincipal(%s)", user);
}
}
}
Original issue reported on code.google.com by trejkaz on 14 Jun 2012 at 12:00
Original issue reported on code.google.com by
trejkaz
on 14 Jun 2012 at 12:00