如何为使用FieleDelay的Springboot调度程序编写单元测试柜

发布于 2025-01-29 12:57:01 字数 1191 浏览 6 评论 0原文

我已经使用春季调度程序安排了工作。需要帮助为此编写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 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文