未能释放模拟 - 莫科托

发布于 2025-01-30 18:59:15 字数 880 浏览 5 评论 0原文

我正在尝试从Mockito版本1.0.19版本升级到4.0.0,并使用Junit 5,因为我无法在旧版本的Mockito中模拟静态。我的错误是“无法发布模拟”错误。.

请让我知道,迁移时需要照顾什么。

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
    }

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
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蛮可爱 2025-02-06 18:59:15

尝试在测试类的顶部添加@extendwith(mockitoextension.class) mockitoAnnotations.initmocks(this); from setup method。

Try adding @ExtendWith(MockitoExtension.class) on top of the test class and removing MockitoAnnotations.initMocks(this); from setUp method.

狠疯拽 2025-02-06 18:59:15

如果您尝试使用Mockito模拟静态类,则首先需要以下依赖关系。如果您尝试在没有此依赖性的情况下模拟静态类,则会丢弃该错误

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

来模拟静态类

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

,然后通过使用是否使用POM中的正确依赖项

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

若有似无的小暗淡 2025-02-06 18:59:15

无法发布模拟,当您的依赖项不对齐时可能会发生。由于您使用的是Spring Boot,因此请确保不要撞到主要的Mockito版本,而是使用Spring-boot-starter-starter-test和正确的Spring Boot Parent版本,该版本将带来包括Mockito在内的一组依赖关系。

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.

空名 2025-02-06 18:59:15

就我而言,我在Android上遇到了类似的错误。原因是以下问题: https://github.com/mockito/mockito/mockito/mockito/mockito/missues/23022

我按照问题所建议的:可辩论的发行版。

In my case I faced this similar error on Android. The reason was the following issue: https://github.com/mockito/mockito/issues/2302

I work around it as suggested in the issue: debuggable release.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文