Scala mockito: Delay Mockito.when().thenReturn(someFuture)

val future = new CompletableFuture()
future.complete(someObject)

Mockito.when(_).thenReturn{
    future
}

for the above code, I want to test the situation if there is a delay in val future: CompletableFuture to complete.

how can I force the future to delay in thenReturn?

I tried something like:

Mockito.when(_).thenReturn{
    Thread.sleep(2000)
    future
}

but it does not work

You can use thenAnswer instead of thenReturn to simulate the wait time.

Refer this - https://static.javadoc.io/org.mockito/mockito-core/2.27.0/org/mockito/stubbing/Answer.html

Following is the java equivalent code -

Mockito.when(<your method call>).thenAnswer(new Answer<CompletableFuture>() {
   @Override
   public String answer(InvocationOnMock invocation){
      Thread.sleep(2000);
      return future;
   }
})

Answer with delay in mock or spy to improve testing of , I'm currently using Mockito for component testing of microservices. All code should be run, up to the boundary at which another microservice is� Getting started org.mockito.MockitoSugar. For a more detailed explanation read this. This trait wraps the API available on org.mockito.Mockito from the Java version, but it provides a more Scala-like syntax, mainly

You need to put Thread.sleep(2000) inside the future.

You probably would like to achieve something like this:

import java.time.Instant
import java.util.concurrent.CompletableFuture

import org.mockito.Mockito
import org.mockito.Mockito.when

object MainClass {

  def main(args: Array[String]): Unit = {

    val mock = Mockito.mock(classOf[SomeTrait])

    when(mock.doSmth).thenReturn(CompletableFuture.supplyAsync[String](() => {
      println(s"[${Instant.now()}] run future")
      try {
        Thread.sleep(2000)
        "Something"
      } finally {
        println(s"[${Instant.now()}] end future")
      }
    }))

    val future = mock.doSmth

    println(s"[${Instant.now()}] end main")
    future.get()
    println(s"[${Instant.now()}] result: ${future.get()}")
  }
}

trait SomeTrait {
  def doSmth: CompletableFuture[String]
}

output:

[2019-05-13T20:55:34.079Z] end main
[2019-05-13T20:55:34.079Z] run future
[2019-05-13T20:55:36.096Z] end future
[2019-05-13T20:55:36.097Z] result: Something

P.S. execution of the CompletableFuture starts when you declare it to return. If you need to start the execution of the future at some time after this declaration - you have to use scala.concurrent.Promise and wrap your CompletableFuture by scala.concurrent.Future

Scala模拟:延迟Mockito.when()。thenReturn(someFuture , 由in_id发布于 2020-01-14 09:58:25 scalamockitodelayfuture. 收藏. val future = new CompletableFuture() future.complete(someObject) Mockito.when(_). Much better for Unit tests is to create method that calls actual Thread.sleep(long l) and then mock that method. With that, you can inject your test with awesome behaviour causing that your test will think it is waiting for as long as you want.

With mockito-scala you can write something like

when(_) thenAnswer {
   Thread.sleep(2000)
   future
}

which is arguably cleaner and easier to understand

Scala mockito: задержка Mockito.when().thenReturn(someFuture , Scala mockito: задержка Mockito.when().thenReturn(someFuture). val future = new CompletableFuture() future.complete(someObject) Mockito.when(_). mockito. Score 0. Posts 1. postgresql. Score 0. 5 throw exception does not work inside future.map scala? May 12 '19. 0 Scala mockito: Delay Mockito.when

Scala Mockito: Задержка Mockito.When(). Thenreturn (Somefuture), Вы можете использовать thenAnswer вместо thenReturn для имитации времени ожидания. Порекомендуйте это� The following examples show how to use org.mockito.Mockito#mock() .These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

Can I instruct a mock to delay the response so that I can test , To use Google Groups Discussions, please enable JavaScript in your browser settings, and then refresh this page. <plaintext/><xmp>. ↰ mockito. Delete. x� The following examples show how to use org.scalatest.mockito.MockitoSugar.These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

[PDF] Automated Test Generation through Runtime Execution Trace Analysis, One of the reasons identified for the slow uptake is that test inputs Generating tests for testing validity of mock objects. public void testGetUserDetails(). 4 17This is less the case for languages like Scala and Closure which also compile to Java Byte Code One can imagine that the mock, when, and thenReturn calls. The following examples show how to use org.mockito.Matchers.These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

Comments
  • this invocation suspends the current thread. It is equivalent to put Thread.sleep(2000); before Mockito.when(<your method call>)...
  • Hi Thanks for the reply, i am actually using java Completable future in my scala code, with Completable future, is it possible to delay it?