问题
I got some strange behaviour of my unit tests.
My simple Page looks like:
public class RegistrationPage extends BasePage {
public RegistrationPage(IModel<Service> model) {
isUserLoggedIn();
add(new RegistrationPanel("registration", model));
}
public RegistrationPage() {
isUserLoggedIn();
}
/**
* Checks if a user is logged in.
* If not, the user will be redirected to the LoginPage.
*
*/
public void isUserLoggedIn() {
if (!getSession().isSignedIn()) {
// redirectToInterceptPage(new LoginPage());
setResponsePage(new LoginPage());
}
}
}
Now i want to test this page in 2 simple tests: First i want to check if the page renders if no user is logged:
@Test
public void testRegistrationPage() {
wicketTester.startPage(RegistrationPage.class);
// it will redirect to the LoginPage
wicketTester.assertRenderedPage(LoginPage.class);
}
I would expect the test will pass. Correct! Because of my isUserLoggedIn() method, it's recognized that no user is logged in and so it will be redirected to the LoginPage.
Now the point is i want to check my RegistrationPage with a model. Ok, next test:
@Test
public void testRegistrationPage() {
wicketTester.startPage(new RegistrationPage(new Model<Service>(new Service("test", "test", "test"))));
// it will redirect to the LoginPage
wicketTester.assertRenderedPage(LoginPage.class);
}
What i expected? I expected the same like in the test before. That means the LoginPage should be rendered... but its not!
junit.framework.AssertionFailedError: classes not the same, expected 'class LoginPage', current 'class RegistrationPage'
It looks like the startPage(Page page) method creates and uses a valid session. But my session only accepts a user with admin/admin credentials.
What is the difference between those two startPage(..) methods (class, page)? It's a bit strange, same test with only one difference: using a model. And then it fails.
Can anyone give advices?
EDIT:
In my session i used SpringSecurity to authenticate through ldap. But in my testcases i have a repository with 2 hardcoded users. admin/admin and user/user. But i thought a session gets only costructed if someone authenticates correctly. I dont authenticate in my testcases. so why there is an "authenticated" session?!
来源:https://stackoverflow.com/questions/16517872/wicket-unit-testing-strange-behaviour-of-startpagepage-page-and-startpagecl