The real question is, how do you deal with this case in your non-test code? What does the API of method()
guarantee to its callers about when result.value
will be set?
Bear that in mind strongly when writing tests - the purpose is to assert that the class and its methods behave as they advertise. Sometimes, working out what the advertised interface is can be half of the challenge.
In a situation like this, I would strongly recommend that your Result
object behave like a Future
, in that its get()
method blocks until the result is available. (Alternatively, give it a waitFor()
method or similar).
If your method doesn't provide any spceific guarantees or blocking calls, all you can really do in the test is to keep checking the value every x seconds in a loop, putting an @Timeout
on the test to ensure that the value is set in a "reasonable" time. But this is all a client would be able to do too, so
- it's a very valid test;
- it highlights that the interface isn't very usable for clients and modifying it would be a nice idea.