Home > database >  Failed to release mocks - Mockito
Failed to release mocks - Mockito

Time:05-20

I am trying to upgrade from Mockito version 1.0.19 to 4.0.0 and using Junit 5, since I am unable to mock static in older version of mockito. I am getting "Failed to Release mocks" error..

Please let me know , what all needs to be taken care while migrating.

public class RefreshTableSchedulerTest {

    @Mock
    ConfigRepository configRepository;

    @InjectMocks
    RandomScheduler randomScheduler;

    @BeforeEach
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        setReflectionUtils(randomScheduler);

    }

@Test
    public void testRefreshTableWithOutDelay() {
    // our trestcases

    }

RandomScheduler

@Configuration
@EnableScheduling
public class RandomScheduler {

@Scheduled(fixedDelayString = "${schedule.refresh.table.job.in.ms:1800000}")
    public void execute() {
    //fetch data from table A
    //inserts data to Table B using timestamps got from Table A
    //updates timestamp of Table A
    }

CodePudding user response:

If you are trying to mock static class using mockito, you will need the following dependency first. If you try to mock static class without this dependency it will throw that error

<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-inline</artifactId>
    <scope>test</scope>
</dependency>

Then mock the static class by using

MockedStatic<YourStaticClass> yourStaticClass = mockStatic(YourStaticClass.class)

Also make sure whether you are using the correct dependencies in your pom

CodePudding user response:

Failed to Release mocks can happen when your dependencies are not aligned. Since you are using Spring Boot, make sure to not bump major Mockito version, but rather use spring-boot-starter-test and correct version of Spring Boot parent that will bring aligned set of dependencies including Mockito.

  • Related