小编Aks*_*r T的帖子

在测试中排除应用程序事件侦听器?

我在解决这个问题时遇到了问题。

我在应用程序中使用缓存,并使用监听器在应用程序启动时加载它。

@EventListener(ApplicationReadyEvent.class)
public void LoadCache() {
    refreshCache();
}

public void refreshCache() {
    clearCache(); // clears cache if present
    populateCache();
}
public void populateCache() {
    // dao call to get values to be populated in cache
    List<Game> games = gamesDao.findAllGames();
    // some method to populate these games in cache.
}
Run Code Online (Sandbox Code Playgroud)

当我运行应用程序时,这一切正常。然而,当我运行测试用例时,会出现问题,LoadCache()在运行安装程序时会调用该测试用例。我不希望在执行测试时运行它。

这是一个示例测试用例

@RunWith(SpringRunner.class)
@SpringBootTest(classes = GameServiceApplication.class)
public class GameEngineTest {
    @Test
    public void testSomeMethod() {
        // some logic
    }
}
Run Code Online (Sandbox Code Playgroud)

events junit caching spring-boot

5
推荐指数
1
解决办法
2670
查看次数

标签 统计

caching ×1

events ×1

junit ×1

spring-boot ×1