Question: So I have a method that starts five threads. I want to write a unit test just to check that the five threads have been started. How do I do that? Sample codes are much appreciated.

Answer: 
Instead of writing your own method to start threads, why not use an [`Executor`](http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/Executor.html), which can be injected into your class? Then you can easily test it by passing in a dummy `Executor`.

**Edit**: Here's a simple example of how your code could be structured:

```
public class ResultCalculator {
    private final ExecutorService pool;
    private final List<Future<Integer>> pendingResults;

    public ResultCalculator(ExecutorService pool) {
        this.pool = pool;
        this.pendingResults = new ArrayList<Future<Integer>>();
    }

    public void startComputation() {
        for (int i = 0; i < 5; i++) {
            Future<Integer> future = pool.submit(new Robot(i));
            pendingResults.add(future);
        }
    }

    public int getFinalResult() throws ExecutionException {
        int total = 0;
        for (Future<Integer> robotResult : pendingResults) {
            total += robotResult.get();
        }
        return total;
    }
}

public class Robot implements Callable<Integer> {
    private final int input;

    public Robot(int input) {
        this.input = input;
    }

    @Override
    public Integer call() {
        // Some very long calculation
        Thread.sleep(10000);

        return input * input;
    }
}

```

And here's how you'd call it from your `main()`:

```
public static void main(String args) throws Exception {
    // Note that the number of threads is now specified here
    ExecutorService pool = Executors.newFixedThreadPool(5);
    ResultCalculator calc = new ResultCalculator(pool);
    try {
        calc.startComputation();
        // Maybe do something while we're waiting
        System.out.printf("Result is: %d\n", calc.getFinalResult());
    } finally {
        pool.shutdownNow();
    }
}

```

And here's how you'd test it (assuming JUnit 4 and Mockito):

```
@Test
@SuppressWarnings("unchecked")
public void testStartComputationAddsRobotsToQueue() {
    ExecutorService pool = mock(ExecutorService.class);
    Future<Integer> future = mock(Future.class);
    when(pool.submit(any(Callable.class)).thenReturn(future);

    ResultCalculator calc = new ResultCalculator(pool);
    calc.startComputation();

    verify(pool, times(5)).submit(any(Callable.class));
}

```

Note that all this code is just a sketch which I have not tested or even tried to compile yet. But it should give you an idea of how the code can be structured.
Rather than saying you are going to "test the five threads have been started", it would be better to step back and think about what the five threads are actually supposed to *do*. Then test to make sure that that "something" is actually being done.

If you really just want to test that the threads have been started, there are a few things you could do. Are you keeping references to the threads somewhere? If so, you could retrieve the references, count them, and call `isAlive()` on each one (checking that it returns `true`).

I believe there is some method on some Java platform class which you can call to find how many threads are running, or to find all the threads which are running in a `ThreadGroup`, but you would have to search to find out what it is.

**More thoughts in response to your comment**

If your code is as simple as `new Thread(runnable).start()`, I wouldn't bother to test that the threads are actually starting. If you do so, you're basically just testing that the Java platform works (it does). If your code for initializing and starting the threads is more complicated, I would stub out the `thread.start()` part and make sure that the stub is called the desired number of times, with the correct arguments, etc.

Regardless of what you do about that, I would *definitely* test that the task is completed correctly when running in multithreaded mode. From personal experience, I can tell you that **as soon** as you start doing *anything* remotely complicated with threads, it is devilishly easy to get subtle bugs which only show up under certain conditions, and perhaps only occasionally. Dealing with the complexity of multithreaded code is a very slippery slope.

Because of that, if you can do it, I would *highly* recommend you do more than just simple unit testing. Do stress tests where you run your task with many threads, on a multicore machine, on very large data sets, and make sure all the answers are exactly as expected.

Also, although you are expecting a performance increase from using threads, I *highly* recommend that you benchmark your program with varying numbers of threads, to make sure that the desired performance increase is actually achieved. Depending on how your system is designed, it's possible to wind up with concurrency bottlenecks which may make your program hardly faster with threads than without. In some cases, it can even be *slower*!