How to avoid Thread.sleep in Unit tests?
<p>Let's imagine I have the following method which should be tested:</p>

<pre><code>@Autowired
private RoutingService routingservice;

public void methodToBeTested() {
    Object objectToRoute = initializeObjectToRoute();
    if (someConditions) {
         routingService.routeInOneWay(objectToRoute);
    } else {
         routingService.routeInAnotherWay(objectToRoute);
    }
}
</code></pre>

<p>In this case <code>RoutingService</code> is running in the separate thread, thus in it's constructor we have the following:</p>

<pre><code>Thread thread = new Thread(this);
thread.setDaemon(true);
thread.start();
</code></pre>

<p>The problem is that <code>RoutingService</code> changes the state of <code>objectToRoute</code> and this is exactly what I want to check, but this doesn't happen straight away thus the test fails. However, if I add <code>Thread.sleep()</code> then it works, but this is bad practice as I know.</p>

<p>How can I avoid <code>Thread.sleep()</code> in this case?</p>