未能释放模拟 - 莫科托
我正在尝试从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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试在测试类的顶部添加
@extendwith(mockitoextension.class)
mockitoAnnotations.initmocks(this); from setup method。Try adding
@ExtendWith(MockitoExtension.class)
on top of the test class and removingMockitoAnnotations.initMocks(this);
fromsetUp
method.如果您尝试使用Mockito模拟静态类,则首先需要以下依赖关系。如果您尝试在没有此依赖性的情况下模拟静态类,则会丢弃该错误
来模拟静态类
,然后通过使用是否使用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
Then mock the static class by using
Also make sure whether you are using the correct dependencies in your pom
无法发布模拟
,当您的依赖项不对齐时可能会发生。由于您使用的是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 usespring-boot-starter-test
and correct version of Spring Boot parent that will bring aligned set of dependencies including Mockito.就我而言,我在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.