Is this a system
test or a unit test? Are you verifying the functionality at the endpoint or that the endpoint is reachable and clients able to receive a response?
If you are just trying to test functionality, then you would be doing yourself a favor by testing at a finer grained level, without the network involved. If you are testing your configuration and infrastructure setup, then use a loop with some kind of counter for number of attempts to connect.