Is there a way to validate multithreaded methods execution on thread other than main thread through Mockito?

45 views Asked by At

I am not able fetch the Thread details for the method which is running in a thread other than main thread.

I m trying to add a Unit test case to check if a particular method is running in a different thread other than the main.

AtomicReference<Thread> threadRef;
Service service = mock(Service.class);
lenient()
    .when(service.doSomething(Mockito.anyString()))
    .thenAnswer(invocation->
    {
        threadRef.set(Thread.currentThread());
        return null;
    });

I m sure that service.doSomething() is running in different thread(pool-35-thread-1). But somehow threadRef is never populated in that way. Can someone please help?

It worked for me after I did below changes to my code:

Thread newThread;

Service service = mock(Service.class);
Mockito.
.when(service.doSomething(Mockito.anyString()))
    .thenAnswer(invocation->
     {    newThread=Thread.currentThread();
          return Response.ok().build();
     });

newThread variable updated with the actual thread where doSomething() was running

0

There are 0 answers