如何测试@scheduled in spring-而不是springboot

6mzjoqzu  于 2021-07-24  发布在  Java
关注(0)|答案(0)|浏览(225)

我发现这个问题有很多类似的主题,但我仍然没有一个解决我的问题的办法。我想在Spring5应用程序中测试我的调度程序,并计算它在一段时间内调用了多少次。
这是我的日程安排:

@Component
public class CancelVisitScheduler {

    @Scheduled(fixedDelay = 10000, initialDelay = 10000)
    public void execute() {
        // do some things
    }
}

我试着这样做:

import org.awaitility.Duration;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import static org.awaitility.Awaitility.await;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

class CancelVisitSchedulerTest {

    @Mock
    private CancelVisitScheduler cancelVisitScheduler;

    @BeforeEach
    void setUp() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    void shouldExecuteAfter10Seconds() {
        await().atMost(Duration.ONE_MINUTE).untilAsserted(() -> verify(cancelVisitScheduler, times(4)).execute());
    }
}

但我犯了这样的错误:

org.awaitility.core.ConditionTimeoutException: Assertion condition defined as a lambda expression in path....CancelVisitSchedulerTest 
Wanted but not invoked:
cancelVisitScheduler.execute();
-> at path....CancelVisitSchedulerTest.lambda$shouldExecuteAfter10Seconds$0(CancelVisitSchedulerTest.java:25)
Actually, there were zero interactions with this mock.
 within 60 seconds.

我找到的所有答案都是关于spring启动应用程序的,但我需要一个spring的解决方案。有什么简单的选择吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题