Skip to content

Tag: mockito

JUnitParams with Mockito

This could be a XY problem but I want to ask: I am using JUnitParams to be able to run my test method 10 times with different objects. The problem is that the injections don’t work (@Mock and @InjectMocks). I can use to mock the PersonService in the @Test method. But how do mock the “main” object that is annotated

Mock final class with Mockito 2

I’m removing Powermock from the project I’m currently working on, so I’m trying to rewrite some existing unitary test only with Mockito (mockito-core-2.2.28). When I run the test, I have the following error: org.mockito.exceptions.base.MockitoException: Cannot mock/spy class com.ExternalpackagePath.Externalclass Mockito cannot mock/spy because : final class I know that this question has already been asked (How to mock a final class

Handle anonymous class with Generics in Mockito

I am trying to write unit test using Powermockito for following method – I have written test method as – When I run this test I always get it failed saying – It looks like stub is not working as I always get “null” for this line – Is it because anonymous instantiation of TypeReference class? Answer Yes, it’s because

Mockito: mocking objects and adding to ArrayList

I am testing an enterprise level application using Mockito and JUnit. Here is the code for a method of adding a product to the offline repository class in a product offline-repository-class-test I have: This relies on the following methods in classes: The method it is testing in the ProductRepositoryOffline: InitialData The question I wish to ask is that in the

How to mock a autowired list of Spring beans?

I’ve read plenty of articles about how to mock Spring’s bean and their autowired fields. But there is nothing I could find about autowired lists of beans. Concrete problem I’ve a class called FormValidatorManager. This class loop through several validators which implements IFormValidator. I would like to test this class. But I can’t find a way to mock validators property.