コード例 #1
0
  @Test
  public void pickingCorrectViewInterface() {

    ViewWithMulitpleInterfaces view = new ViewWithMulitpleInterfaces();
    TestNullObjectPresenter presenter = new TestNullObjectPresenter();

    presenter.attachView(view);
    Assert.assertNotNull(presenter.getView());
    Assert.assertTrue(view == presenter.getView());

    presenter.detachView(false);
    Assert.assertNotNull(presenter.getView());
    Assert.assertFalse(presenter.getView() == view);

    // Invoke methods on proxy
    presenter.getView().showFoo(new TestData());
    presenter.getView().showThat();
  }
コード例 #2
0
  @Test
  public void testAttachDetach() {

    TestNullObjectPresenter presenter = new TestNullObjectPresenter();

    try {
      // NullPointer exception should be thrown
      presenter.getView();
      Assert.fail("Nullpointer Exception should be thrown but haven't");
    } catch (NullPointerException e) {
      // Expected exception
    }

    TestView view =
        new TestView() {
          @Override
          public void showFoo(TestData data) {}

          @Override
          public void showThat() {}
        };

    presenter.attachView(view);
    Assert.assertNotNull(presenter.getView());
    Assert.assertTrue(presenter.getView() == view);

    // Test with retainInstance == false
    presenter.detachView(false);
    Assert.assertNotNull(presenter.getView());
    Assert.assertTrue(presenter.getView() != view); // Null Object view

    // Reattach real view
    presenter.attachView(view);
    Assert.assertNotNull(presenter.getView());
    Assert.assertTrue(presenter.getView() == view);

    // Test with retainInstance == true
    presenter.detachView(true);
    Assert.assertNotNull(presenter.getView());
    Assert.assertTrue(presenter.getView() != view); // Null Object view
  }