admin管理员组

文章数量:1122846

There are smiliar questions to this but they don't quite cover the answer I need, so could anyone help a real noob to Java and Mockito? I have code as part of a larger method in a class like so:

private final ExecutorService executorService; //(also declared in constructor)


executorService.submit(() -> {
      someCool.stuffInHere
});

And I would like to unit test it with something along the lines of

@Mock
ExecutorService executorServiceMock

doAnswer(invocationOnMock -> {
    testSomeCoolStuffInHere;
}).when(executorServiceMock).submit(() -> any())

Is that a correct way to go about it? I'm trying that but I keep getting an error along the lines of:

executorServiceMock.submit(CLassName$$LongLambdaAddress) at ClassName
has following stubbings with different arguments

1. executorServiceMock.submit(CLassName$$ADifferentLongLambdaAddress)

Thanks for looking. hopefully there's enough info there. I'm hoping it's something simple.

There are smiliar questions to this but they don't quite cover the answer I need, so could anyone help a real noob to Java and Mockito? I have code as part of a larger method in a class like so:

private final ExecutorService executorService; //(also declared in constructor)


executorService.submit(() -> {
      someCool.stuffInHere
});

And I would like to unit test it with something along the lines of

@Mock
ExecutorService executorServiceMock

doAnswer(invocationOnMock -> {
    testSomeCoolStuffInHere;
}).when(executorServiceMock).submit(() -> any())

Is that a correct way to go about it? I'm trying that but I keep getting an error along the lines of:

executorServiceMock.submit(CLassName$$LongLambdaAddress) at ClassName
has following stubbings with different arguments

1. executorServiceMock.submit(CLassName$$ADifferentLongLambdaAddress)

Thanks for looking. hopefully there's enough info there. I'm hoping it's something simple.

Share Improve this question asked Nov 21, 2024 at 18:45 gilbogilbo 31 bronze badge 4
  • 1 I would recommend not mocking an executor service at all, but perhaps using a simple direct executor, and validating the effects of the submitted logic. – Louis Wasserman Commented Nov 21, 2024 at 18:54
  • I'm not sure what prevents you from deducing an answer from the very similiar question stackoverflow.com/questions/64466093/… – SpaceTrucker Commented Nov 21, 2024 at 20:56
  • This question is similar to: How to mock ExecutorService call using Mockito. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – SpaceTrucker Commented Nov 21, 2024 at 20:57
  • I hope you're not trying to test the ExecutorService. – forty-two Commented Nov 21, 2024 at 22:40
Add a comment  | 

2 Answers 2

Reset to default 0

You are not submitting () -> any(). You are submitting a Callable<T> or a Runnable (the latter in your case, because you don't return a value).

So if you really want to stub the calls on the executor service (instead of using one of the existing implementations that help with testing, e.g. Runnable::run is a handy Executor to use in a test), you must use the correct argument matcher.

Your code is effectively equivalent to:

doAnswer(invocationOnMock -> {
    testSomeCoolStuffInHere;
}).when(executorServiceMock).submit(eq(() -> null))

Instead, you want to match a runnable:

doAnswer(invocationOnMock -> {
    testSomeCoolStuffInHere;
}).when(executorServiceMock).submit(any(Runnable.class))

(or any<Runnable>() – this will match null as well)

import static org. Mockito. Mockito.*;  
import static org. j unit. Assert.*;  

import org. j unit .Before;  
import org. j unit. Test;  
import org. Mockito. Mock;  
import org. Mockito. Mockito Annotations;  

import java. util. concurrent. Executor Service;  

public class Your Class Test {  

    @Mock  
    private Executor Service executor Service Mock;  

    private Your Class your Class; // Replace with your actual class that uses Executor Service  

    @Before  
    public void set Up() {  
        Mockito Annotations. in it Mocks(this);  
        your Class = new Your Class(executor Service Mock); // Inject the mock  
    }  

    @Test  
    public void test Executor Service Submit() {  
        // Arrange  
        do Answer(invocation -> {  
            Runnable = invocation .get Argument(0);  
            runnable. run(); // Execute the runnable directly  
            return null; // Return null as submit() returns a Future<?>  
        }).when(executorServiceMock).submit(any(Runnable.class));  

        // Act  
        your Class .submit To Executor(); // Call the method that uses the Executor Service  

        // Assert  
        // Verify interactions or assert conditions based on the logic within the runnable  
        // For example:  
        assert True("Your condition here", some Condition); // Replace with your actual condition  
    }  
}

本文标签: javaTesting ExecutorService with MockitoStack Overflow