コード例 #1
0
  @Test
  public void importMockInReusableVerificationBlock() {
    new ReusableExpectations() {};

    dependency.notifyBeforeSave();
    dependency.save();

    new ReusableVerifications() {};
  }
コード例 #2
0
  @Test(expected = UnexpectedInvocation.class)
  public void verifyAllWithUnverifiedReplayOnSameInstance(final Dependency mock2) {
    mock.editABunchMoreStuff();
    mock2.editABunchMoreStuff();

    new FullVerifications(mock2) {
      {
        mock.editABunchMoreStuff();
      }
    };
  }
コード例 #3
0
  @Test
  public void verifyAllInvocationsToOnlyOneOfTwoMockedTypes(AnotherDependency mock2) {
    exerciseCodeUnderTest();
    mock2.doSomething();

    new FullVerifications(mock) {
      {
        mock.prepare();
        mock.setSomething(anyInt);
        minTimes = 1;
        maxTimes = 2;
        mock.editABunchMoreStuff();
        mock.save();
        times = 1;
      }
    };

    new FullVerifications(mock.getClass()) {
      {
        mock.prepare();
        mock.setSomething(anyInt);
        minTimes = 1;
        maxTimes = 2;
        mock.editABunchMoreStuff();
        mock.save();
        times = 1;
      }
    };
  }
コード例 #4
0
  @Test
  public void importMockFromReusableExpectationBlock() {
    new ReusableExpectations() {};

    dependency.notifyBeforeSave();
    dependency.save();

    new VerificationsInOrder() {
      Dependency dep;

      {
        dep.notifyBeforeSave();
        unverifiedInvocations();
      }
    };
  }
コード例 #5
0
  @Test
  public void importLocalMocksFromPreviousExpectationsBlock() {
    Expectations exp =
        new Expectations() {
          @NonStrict Dependency mockDependency;

          @Mocked("run")
          @Capturing
          Runnable runnable;
        };

    dependency.editABunchMoreStuff();
    new Runnable() {
      public void run() {}
    }.run();

    new FullVerifications() {
      Runnable runnable;
      Dependency mock;

      {
        runnable.run();
        times = 1;
        mock.editABunchMoreStuff();
      }
    };
  }
コード例 #6
0
  @Test
  public void verifyAllInvocationsToOneOfTwoMocksInIteratingBlock(AnotherDependency mock2) {
    mock2.doSomething();
    mock.setSomething(123);
    mock.save();
    mock2.doSomethingElse(1);
    mock.setSomething(45);
    mock.save();
    mock2.doSomethingElse(2);

    new FullVerifications(2, mock) {
      {
        mock.setSomething(anyInt);
        mock.save();
      }
    };
  }
コード例 #7
0
  @Test
  public void verifyAllInvocationsWithReplayOnSameInstance(final Dependency mock2) {
    mock2.editABunchMoreStuff();

    new FullVerifications(mock2) {
      {
        mock2.editABunchMoreStuff();
      }
    };
  }
コード例 #8
0
  @Test(expected = MissingInvocation.class)
  public void verifyAllWithReplayOnDifferentInstanceWhenShouldBeSame(final Dependency mock2) {
    mock2.editABunchMoreStuff();

    new FullVerificationsInOrder(mock2) {
      {
        mock.editABunchMoreStuff();
      }
    };
  }
コード例 #9
0
  @Test
  public void verifyAllInvocationsToSubclassMethods(final SubDependency mock2) {
    mock.prepare();
    mock2.getValue();

    new FullVerifications(1, mock2.getClass()) {
      {
        mock2.getValue();
      }
    };
  }
コード例 #10
0
  @Test(expected = UnexpectedInvocation.class)
  public void verifyAllInvocationsToSubclassMethods_whenNotVerified(final SubDependency mock2) {
    mock.prepare();
    mock2.getValue();

    new FullVerificationsInOrder(1, mock2.getClass()) {
      {
        mock.prepare();
      }
    };
  }
コード例 #11
0
  @Test
  public void verifyNoInvocationsOnOneOfTwoMockedDependenciesBeyondThoseRecordedAsExpected(
      final AnotherDependency mock2) {
    new NonStrictExpectations() {
      {
        mock.setSomething(anyInt);
        minTimes = 1;
        mock2.doSomething();
        times = 1;
      }
    };

    mock.prepare();
    mock.setSomething(1);
    mock.setSomething(2);
    mock.save();
    mock2.doSomething();

    new FullVerifications(mock2) {};
  }
コード例 #12
0
  @Test(expected = UnexpectedInvocation.class)
  public void unverifiedStaticInvocationForSpecifiedSubclassInstance(final SubDependency mock2) {
    mock2.getValue();
    Dependency.staticMethod("test");

    new FullVerificationsInOrder(1, mock2) {
      {
        mock2.getValue();
      }
    };
  }
コード例 #13
0
  @Test(expected = UnexpectedInvocation.class)
  public void verifyAllInvocationsToMethodsOfBaseClassAndOfSubclass_whenSubclassMethodNotVerified(
      final SubDependency mock2) {
    mock.prepare();
    mock2.getValue();

    new FullVerifications(mock2) {
      {
        mock.prepare();
      }
    };
  }
コード例 #14
0
  @Test
  public void verifyNoInvocationsOccurredOnMockedDependencyWithOneHavingOccurred(
      AnotherDependency mock2) {
    mock2.doSomething();
    mock.editABunchMoreStuff();

    try {
      new FullVerifications(mock) {};
      fail();
    } catch (UnexpectedInvocation e) {
      assertTrue(e.getMessage().contains("editABunchMoreStuff()"));
    }
  }
コード例 #15
0
  @Test
  public void importLocalMockFromPreviousNonStrictExpectationsBlock() {
    new NonStrictExpectations() {
      Dependency mock;

      {
        mock.notifyBeforeSave();
        result = true;
      }
    };

    dependency.editABunchMoreStuff();
    assertTrue(dependency.notifyBeforeSave());

    new Verifications() {
      Dependency mock;

      {
        mock.editABunchMoreStuff();
      }
    };
  }
コード例 #16
0
  @Test
  public void verifyAllInvocationsToInheritedMethods(SubDependency mock2) {
    mock.prepare();
    mock2.getValue();

    new FullVerificationsInOrder(1, mock) {
      {
        mock.prepare();
      }
    };
    new FullVerificationsInOrder(Dependency.class) {
      {
        mock.prepare();
      }
    };
  }
コード例 #17
0
  @Test
  public void verifyStaticInvocationForSpecifiedMockInstance(final AnotherDependency mock2) {
    mock2.doSomething();
    AnotherDependency.staticMethod();
    mock2.doSomethingElse(1);
    mock.editABunchMoreStuff();
    mock2.doSomethingElse(2);

    new FullVerificationsInOrder(mock2) {
      {
        mock2.doSomething();
        AnotherDependency.staticMethod();
        mock2.doSomethingElse(anyInt);
        mock2.doSomethingElse(anyInt);
      }
    };
  }
コード例 #18
0
 {
   mock.save();
   maxTimes = 1;
 }
コード例 #19
0
 {
   mock.notifyBeforeSave();
   result = true;
 }
コード例 #20
0
 private void exerciseCodeUnderTest() {
   mock.prepare();
   mock.setSomething(123);
   mock.editABunchMoreStuff();
   mock.save();
 }