anna
anna

Reputation: 755

JUnit Tests: how to test results from a Thread

I call a method passing a parameter. If this parameter is equal to something particular then a thread is started doing something repeatedly until it is stopped. In every repetition some values are changed.
Is there any way to check these values from JUnit?

Upvotes: 2

Views: 447

Answers (2)

TheBigS
TheBigS

Reputation: 501

If you are trying to test to see if each iteration modified the values appropriately, then call the iteration code with the expected inputs and test the expected outputs. Test each peice in isolation:

pseudo java code:

for each (file : files) {
    doSomething(file); // this updates some running totals or something
}

Then you want to write some unit tests that call your doSomething() on each input you want to test and see if the values update appropriately (mock where necessary). Then do an integration test where you let the thread spawn and check the resulting values.

Upvotes: 0

BeRecursive
BeRecursive

Reputation: 6366

If you are spawning threads you are not unit testing anymore - you are integration testing. Refactor your code so that the logic that changes this 'value' can be tested without the thread spawning. If it works without spawning a thread then it will work when spawning threads (I know I've set myself up for a lecture on that one... You will need to make sure you are properly synchronizing any potentially shared variables and don't have any code that could cause a deadlock).

Without seeing the code it is difficult to try to suggest ways to test it. However, you are definitely not unit testing if you are spawning threads.

Upvotes: 3

Related Questions