-1

I have a class with a method which somtimes uses notifyAll().

class A{
    public void method(*some args*) //sometime i call for notifyAll()
}

I want to build a test to see if class A has called for notifyAll()

@Test
public void test(){
    A.method
    boolean isNotified
}

how can I do it?

  • Testing asynchronous stuff is very hard, what does your code actually look like? Who waits for whom, who notifies whom? – luk2302 Mar 04 '22 at 08:31
  • The method has to modify the status of the class in a way that you can observe it from outside. If it doesnt do that, you have to create a mock or spy of `A` using frameworks, such as Mockito. For a more detailed answer, we have to see your actual code and the real context in which this method is executed. Voting to close, missing details. – Zabuzard Mar 04 '22 at 08:31
  • With Mockito, for example, you can do `A spy = spy(new A());` and then `verify(spy).method(any())` and similar. Hard to guess whether this is what you are looking for though. – Zabuzard Mar 04 '22 at 08:55
  • Can you actually spy `notifyAll()`, considering it's a final method? – Andy Turner Mar 04 '22 at 09:02

1 Answers1

1

Couple of ways

  1. Verify it has been called by checking the effects of potential call - eg some object did change its state as a result and it is verifiable
  2. Spy on instance that is supposed to have method called - you can then check if the method have been called directly
  3. Use observable pattern and add 'observe' behavior to the test case.

probably much more.

Antoniossss
  • 31,590
  • 6
  • 57
  • 99