Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@InjectMocks seems doesn't work since 5.2.0 in some cases #3221

Open
alexey-komarov opened this issue Jan 2, 2024 · 4 comments
Open

@InjectMocks seems doesn't work since 5.2.0 in some cases #3221

alexey-komarov opened this issue Jan 2, 2024 · 4 comments

Comments

@alexey-komarov
Copy link

Hey,
I'm working on migrating an old project from Java 8 to OpenJDK 21 plus upgrading all dependencies.
I have migrated some unit tests from Junit 4/Mockito 4 to latest junit-jupiter 5/Mockito 5.8 and some tests don't work as expected. I have tried to downgrade Mockito to check if there is a regression and surprisingly found it works with Mockito 5.1.1 as expected. I didn't dig into it further, but I was able to create a very simple isolated reproducible case, please see attached testmock.tar.gz (I use gradle 8.5 to build and run tests).
testmock.tar.gz

@alexey-komarov alexey-komarov changed the title @InjectMocks seems doesn't work since 5.1.1 in some cases @InjectMocks seems doesn't work since 5.2.0 in some cases Jan 2, 2024
@Saik2930
Copy link

Saik2930 commented Jan 7, 2024

@alexey-komarov I could see you are using ExtendWith(MockitoExtension.class) and again MockitoAnnotations.openMocks(this). When you are using ExtendWith, no need to use openMocks again as MockitoExtension automatically manages opening and closing mocks in the test class. You can try removing BeforeEach method and try once.

Also, using Autowired is also unnecessary in this scenario it seems for me

@alexey-komarov
Copy link
Author

@Saik2930
I tried, it doesn't help.

Also, I found it doesn't work even with 5.1.1 when I'm running with Spring Boot (in the attached example there is only Spring without Spring Boot for the simplicity).

I think the issue is that I'm trying to mock generic object in my base class:

public abstract class BaseTaskHandler<T extends QueueTask> implements QueueTaskHandler<T> {
    @Autowired
    protected QueueService<T> queueService;

because once I replaced it with

    abstract protected QueueService<QueueTask> getQueueService();

    @Override
    public void handleTask(T task) {
        logger.error("Handle task: {}", task);
        this.handleTaskInt(task);
        this.getQueueService().markTaskAsDone(task);
    }    

and added the required getter to the child TestTaskHandler class like this:

    @Autowired
    private QueueService<QueueTask> queueService;

    @Override
    protected QueueService<QueueTask> getQueueService() {
        return this.queueService;
    }

I was able to mock QueueService with

    @Mock
    private QueueService<QueueTask> queueService;

and everything started to work correctly.

@alin-a
Copy link

alin-a commented Jan 11, 2024

I think I also experience this problem. I am seeing NullPointerExceptions on tests that inject a generic mock into an abstract class. In my case Mockito 5.1.0 works fine. The problem occurs in 5.2.0 onwards.

I suspect #3019 might be also be related to this.

@melloware
Copy link

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants