How to Make a Junit Test Wait

How can I wait for a future to finish during a test if it wasn't called from the test directly?

the method I'm testing isn't asynchronous

Congratulations, your tests found a bug.

this is your method after fixing the bug:

Future<void> methodToTest() async {
apiResult = await apiCall();
}

How to wait for X milliseconds in an Android Test?

The problem was that I was doing the sleep on the wrong thread. This is my working solution:

    fun start() {
// Test Thread
val linearAnimation = LinearAnimation()

InstrumentationRegistry.getInstrumentation().runOnMainSync {
// Ui Thread
linearAnimation.start("Name", 0f, 1f, ::setValueTester)
}

// Sleep the Test thread until the UI thread is done
Thread.sleep(2000)
assertEquals(1, currentValue)

}

Thread.sleep in JUnit test case

There are two problems with using (real) time in test cases:

  1. It is never really deterministic. Especially when you are looking for high precision, testcases will succeed 95% of the time. But sometimes they fail, these are the hardest types of failure to debug. Note that when using Thread.sleep() with in a multithreaded test case this is even more difficult.
  2. Test cases with sleeps take long to run, after a while this will make running your full testset cumbersome.

If you must, your way is acceptable. But there are other options:

Don't use a real clock. Instead use a fake (mocked/stubbed) clock that you can control from your testcase:

@Test(expected = IllegalStateException.class)
public void testCallAfterTimeout() {
MyObject o= new MyObject();
// Example function that you could make
advanceClock(1000, TimeUnit.MILLISECONDS)
o.call();
}

In your object you have to inject a clock. MyObject could look like this:

class MyObject
{

public MyObject()
{
this(new Clock());
}

// Protected so only the test case can access it
protected MyObject(Clock clock)
{
// Save clock as local variable, store creation time etc.
}
}

In Java 8 a mechanism for this is provided, see for instance LocalDate.now(). But you can also implement your own quiet easily.

JUnit wait before proceeding

That is as much as you can do with a void return type. So basically you do a long pooling here and check if it has been really deleted.

Other things I can think off is may be a database trigger that would work on delete - no idea if it is possible though; but even if it is - for tests this would require quite a lot of work. Another thing is that may be you can do that in a separate thread and your main thread get updated whenever the result comes - but again, for tests this sounds like an overkill, to me.

Another small suggestion is Thread.onSpinWait (since java-9) - read it's documentation to see how it might help (a bit).

How to execute JUnit TestSuite with some delay between each TestCase?

I think you're not getting an answer (at least not one you want) to this question because it is generally considered bad practice to rely upon these mechanics in a test suite. Your tests will become very difficult to maintain if you have these types of reliance upon each other, and can (most likely will) cause you difficulty in debugging them later.

You could possibly abstract this first test out, and this second test could perhaps extend it. This second test will then use the common functionality in its setup (perhaps with different data). This should allow you to run the tests concurrently since each will then be an atomic entity.



Related Topics



Leave a reply



Submit