0

Is is possible to use awaitility to ensure a method wasn't called during the await?

I have tried using

@Test
void testThatSomeMethodWasntCalled(){
await().atMost(10, TimeUnit.SECONDS).untilAsserted(() -> someClass.someMethod())
}

But there doesn't appear to be a negative version of untilAsserted().

Currently I am using

@Test
void testThatSomeMethodWasntCalled(){
await().atMost(10, TimeUnit.SECONDS)
verify(someClass, never()).someMethod()
}

However the above feels like a glorfied Thread.sleep(...)

Any help would be appricated! Jonny

  • Would there be a side-effect if that unwanted method was invoked? If yes, check the side-effect does not exist. – Andrew S Nov 22 '22 at 13:38
  • The someClass is actually a mock so there’s no side effect – JDMDevelopement Nov 22 '22 at 15:47
  • The mock could set a boolean `wasMethodWasInvoked` to true if the method was invoked, then could `await().atMost(10, TimeUnit.SECONDS).untilAsserted(() -> !wasMethodWasInvoked)`. But the second example looks easier to read. – Andrew S Nov 22 '22 at 17:22
  • 1
    There was actually an issue with the `await().atMost(10, TimeUnit.SECONDS)`, it doesnt wait unless it has .until(...) so my test was actually not testing anything. The cleanest alternative that i have found is not using Awailtillity, but just using Mockitio. `verify(someClass, after(8000).never()).someMethod()` – JDMDevelopement Nov 24 '22 at 11:19

0 Answers0