Skip to main content
Fixed the Runnable method that is called
Source Link
Rob Spoor
  • 10k
  • 1
  • 28
  • 29

You could catch the exception from verify and rethrow it as TestAbortedException:

try {
  verify(mockObject).doSomething();
} catch (final MockitoAssertionError ex) {
  throw new TestAbortedException("Method doSomething not called", ex);
}

(Although I don't know why you'd want to not fail the test in this case. If the method call is not required, don't verify/assert it at all?)

Extract a utility method to make this logic reusable:

class VerificationAssumptions {
  private VerificationAssumptions() {}

  static void assumeVerify(final String message, final Runnable verification) {
    try {
      verification.runnablerun();
    } catch (final MockitoAssertionError ex) {
      throw new TestAbortedException(message, ex);
    }
  }
}

and then use:

VerificationAssumptions.assumeVerify(
  "Method doSomething not called",
  () -> verify(mockObject).doSomething());

(you might be able to implement a nicer API, such as assumeVerify(mockObject).doSomething(), but I'll leave this to the reader ;))

You could catch the exception from verify and rethrow it as TestAbortedException:

try {
  verify(mockObject).doSomething();
} catch (final MockitoAssertionError ex) {
  throw new TestAbortedException("Method doSomething not called", ex);
}

(Although I don't know why you'd want to not fail the test in this case. If the method call is not required, don't verify/assert it at all?)

Extract a utility method to make this logic reusable:

class VerificationAssumptions {
  private VerificationAssumptions() {}

  static void assumeVerify(final String message, final Runnable verification) {
    try {
      verification.runnable();
    } catch (final MockitoAssertionError ex) {
      throw new TestAbortedException(message, ex);
    }
  }
}

and then use:

VerificationAssumptions.assumeVerify(
  "Method doSomething not called",
  () -> verify(mockObject).doSomething());

(you might be able to implement a nicer API, such as assumeVerify(mockObject).doSomething(), but I'll leave this to the reader ;))

You could catch the exception from verify and rethrow it as TestAbortedException:

try {
  verify(mockObject).doSomething();
} catch (final MockitoAssertionError ex) {
  throw new TestAbortedException("Method doSomething not called", ex);
}

(Although I don't know why you'd want to not fail the test in this case. If the method call is not required, don't verify/assert it at all?)

Extract a utility method to make this logic reusable:

class VerificationAssumptions {
  private VerificationAssumptions() {}

  static void assumeVerify(final String message, final Runnable verification) {
    try {
      verification.run();
    } catch (final MockitoAssertionError ex) {
      throw new TestAbortedException(message, ex);
    }
  }
}

and then use:

VerificationAssumptions.assumeVerify(
  "Method doSomething not called",
  () -> verify(mockObject).doSomething());

(you might be able to implement a nicer API, such as assumeVerify(mockObject).doSomething(), but I'll leave this to the reader ;))

added 18 characters in body
Source Link
knittl
  • 269.3k
  • 59
  • 339
  • 405

You could catch the exception from verify and rethrow it as TestAbortedException:

try {
  verify(mockObject).doSomething();
} catch (final MockitoAssertionError ex) {
  throw new TestAbortedException("Method doSomething not called", ex);
}

(Although I don't know why you'd want to not fail the test in this case. If the method call is not required, don't verify/assert it at all?)

Extract a utility method to make this logic reusable:

class VerificationAssumptions {
  private VerificationAssumptions() {}

  static void ignoreassumeVerify(final String message, final Runnable verification) {
    try {
      verification.runnable();
    } catch (final MockitoAssertionError ex) {
      throw new TestAbortedException(message, ex);
    }
  }
}

and then use:

VerificationAssumptions.ignoreassumeVerify(
  "Method doSomething not called",
  () -> verify(mockObject).doSomething());

(you might be able to implement a nicer API, such as ignoreassumeVerify(mockObject).doSomething(), but I'll leave this to the reader ;))

You could catch the exception from verify and rethrow it as TestAbortedException:

try {
  verify(mockObject).doSomething();
} catch (final MockitoAssertionError ex) {
  throw new TestAbortedException("Method doSomething not called", ex);
}

(Although I don't know why you'd want to not fail the test in this case. If the method call is not required, don't verify/assert it at all?)

Extract a utility method to make this logic reusable:

class VerificationAssumptions {
  private VerificationAssumptions() {}

  static void ignore(final String message, final Runnable verification) {
    try {
      verification.runnable();
    } catch (final MockitoAssertionError ex) {
      throw new TestAbortedException(message, ex);
    }
  }
}

and then use:

VerificationAssumptions.ignore(
  "Method doSomething not called",
  () -> verify(mockObject).doSomething());

(you might be able to implement a nicer API, such as ignore(mockObject).doSomething(), but I'll leave this to the reader ;))

You could catch the exception from verify and rethrow it as TestAbortedException:

try {
  verify(mockObject).doSomething();
} catch (final MockitoAssertionError ex) {
  throw new TestAbortedException("Method doSomething not called", ex);
}

(Although I don't know why you'd want to not fail the test in this case. If the method call is not required, don't verify/assert it at all?)

Extract a utility method to make this logic reusable:

class VerificationAssumptions {
  private VerificationAssumptions() {}

  static void assumeVerify(final String message, final Runnable verification) {
    try {
      verification.runnable();
    } catch (final MockitoAssertionError ex) {
      throw new TestAbortedException(message, ex);
    }
  }
}

and then use:

VerificationAssumptions.assumeVerify(
  "Method doSomething not called",
  () -> verify(mockObject).doSomething());

(you might be able to implement a nicer API, such as assumeVerify(mockObject).doSomething(), but I'll leave this to the reader ;))

added 137 characters in body
Source Link
knittl
  • 269.3k
  • 59
  • 339
  • 405

You could catch the exception from verify and rethrow it as TestAbortedException:

try {
  verify(mockObject).doSomething();
} catch (final MockitoAssertionError ex) {
  throw new TestAbortedException("Method doSomething not called", ex);
}

(Although I don't know why you'd want to not fail the test in this case. If the method call is not required, don't verify/assert it at all?)

Extract a utility method to make this logic reusable:

class VerificationAssumptions {
  private VerificationAssumptions() {}

  static void ignore(final String message, final Runnable verification) {
    try {
      verification.runnable();
    } catch (final MockitoAssertionError ex) {
      throw new TestAbortedException(message, ex);
    }
  }
}

and then use:

VerificationAssumptions.ignore(
  "Method doSomething not called",
  () -> verify(mockObject).doSomething());

(you might be able to implement a nicer API, such as ignore(mockObject).doSomething(), but I'll leave this to the reader ;))

You could catch the exception from verify and rethrow it as TestAbortedException:

try {
  verify(mockObject).doSomething();
} catch (final MockitoAssertionError ex) {
  throw new TestAbortedException("Method doSomething not called", ex);
}

(Although I don't know why you'd want to not fail the test in this case. If the method call is not required, don't verify/assert it at all?)

Extract a utility method to make this logic reusable:

class VerificationAssumptions {
  private VerificationAssumptions() {}

  static void ignore(final String message, final Runnable verification) {
    try {
      verification.runnable();
    } catch (final MockitoAssertionError ex) {
      throw new TestAbortedException(message, ex);
    }
  }
}

and then use:

VerificationAssumptions.ignore("Method doSomething not called", () -> verify(mockObject).doSomething());

You could catch the exception from verify and rethrow it as TestAbortedException:

try {
  verify(mockObject).doSomething();
} catch (final MockitoAssertionError ex) {
  throw new TestAbortedException("Method doSomething not called", ex);
}

(Although I don't know why you'd want to not fail the test in this case. If the method call is not required, don't verify/assert it at all?)

Extract a utility method to make this logic reusable:

class VerificationAssumptions {
  private VerificationAssumptions() {}

  static void ignore(final String message, final Runnable verification) {
    try {
      verification.runnable();
    } catch (final MockitoAssertionError ex) {
      throw new TestAbortedException(message, ex);
    }
  }
}

and then use:

VerificationAssumptions.ignore(
  "Method doSomething not called",
  () -> verify(mockObject).doSomething());

(you might be able to implement a nicer API, such as ignore(mockObject).doSomething(), but I'll leave this to the reader ;))

Source Link
knittl
  • 269.3k
  • 59
  • 339
  • 405
Loading