如何为使用FieleDelay的Springboot调度程序编写单元测试柜
我已经使用春季调度程序安排了工作。需要帮助为此编写UnitTestCaes。 嘲笑和调用调度程序方法如何处理此等待时间。.我们真的需要等待这么长时间的
@Configuration
@EnableScheduling
public class RefreshDataScheduler {
@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
}
测试:
public class RefreshTableSchedulerTest {
@Mock
ConfigRepository configRepository;
@InjectMocks
RefreshDataScheduler refreshDataScheduler;
@BeforeEach
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
}
@Test
public void testExecuteWithConfigData() {
A a = getConfigData();
Mockito.when(configRepository.findById(REFRESH_TABLE)).thenReturn(Optional.of(a));
refreshDataScheduler.execute();
}
private A getConfigData() {
byte[] bytes = Utils.asBytes(UUID.randomUUID());
A a = new A();
a.setLastRefreshTime(System.currentTimeMillis());
a.setRefreshTable(REFRESH_TABLE);
return a;
}
}
}
I have scheduled job using spring schedulers. Need help to write UnitTestcsaes for this.
On mocking and calling scheduler method how to handle this waiting time.. Do we really need to wait for such long time
@Configuration
@EnableScheduling
public class RefreshDataScheduler {
@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
}
TEST:
public class RefreshTableSchedulerTest {
@Mock
ConfigRepository configRepository;
@InjectMocks
RefreshDataScheduler refreshDataScheduler;
@BeforeEach
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
}
@Test
public void testExecuteWithConfigData() {
A a = getConfigData();
Mockito.when(configRepository.findById(REFRESH_TABLE)).thenReturn(Optional.of(a));
refreshDataScheduler.execute();
}
private A getConfigData() {
byte[] bytes = Utils.asBytes(UUID.randomUUID());
A a = new A();
a.setLastRefreshTime(System.currentTimeMillis());
a.setRefreshTable(REFRESH_TABLE);
return a;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论