4

我有一个使用 Wicket Auth/Roles 登录用户和分配角色的网络应用程序。(http://wicket.apache.org/learn/projects/authroles.html

你也可以参考这个例子:http: //www.wicket-library.com/wicket-examples-6.0.x/authentication3/

我有很多网页,我想在测试我的网页之前登录我的应用程序。我的测试页面扩展了 WebAppTestBase。

这是我的 WebAppTestBase 代码:

public class WebAppTestBase {

  protected WicketTester wicketTester;

  private MyAuthenticatedWebApplication myAuthenticatedWebApplication = new MyAuthenticatedWebApplication();

  @Before
  public void setUp() {
    wicketTester = new WicketTester(myAuthenticatedWebApplication);

  }

  @After
  public void tearDown() {
    wicketTester.destroy();
  }
}

那么我如何设置 AuthenticatedWebSession 来验证我的用户,这样我就可以测试其他页面了。

问候,

4

1 回答 1

0

这可能是一个老问题,但我自己偶然发现了这个问题,并找到了一个可能对你有用的合理解决方案。

这很明显,但我花了一段时间才意识到这是做到这一点的方法。

public class MyPageTest  {

    private static WicketTester tester;

    public static final String VALID_ADMIN_USERNAME = "admin";
    public static final String VALID_ADMIN_PASSWORD = "1234";

    @BeforeClass
    public static void beforeTesting() {
        tester = new WicketTester(new MyTestApplication());

        /*
         * Setup your AuthenticatedWebSession to be able to resolve any objects 
         * it might be depening on. In my case this was an AuthChecker instance that 
         * I get from Guice dependency injection but this might be very different 
         * for you.
         */
        ((MyAuthenticatedWebSession)tester.getSession())
                .configureAuthChecker(MyTestApplication.testInjector()
                        .getInstance(AuthChecker.class));
    }

    @AfterClass
    public static void afterTesting() {
        tester.destroy();
    }

    @Test
    public void testAdminOptions() {
        // You could consider doing this in a separate @Before annotated method.
        // This is basically where the magic happens and the user is logged in.
        ((AuthenticatedWebSession)tester.getSession()).signIn(
                VALID_ADMIN_USERNAME, 
                VALID_ADMIN_PASSWORD);

        // When the user is logged in, you can just start from a page that normally
        // requires authentication.
        tester.startPage(OverviewPage.class);
        tester.assertVisible("myPanel");
    }

}
于 2019-07-30T10:38:48.247 回答