如何在不依赖 Pivotal GemFire 缓存的情况下启动 Spring 启动应用程序

How to start Spring Boot app without depending on Pivotal GemFire cache

我有一个 Spring 引导应用程序,其中配置了 Pivotal GemFire ClientCache 实例及其相应的域对象。我也在使用 Spring Boot Test 进行单元测试。对于通过 class 或 Maven 构建执行的每个测试用例,如果 GemFire 缓存已关闭,Spring ApplicationContext 将无法加载。

如何在不依赖 GemFire 缓存的情况下启动 Spring 启动应用程序?

对于您的单元测试,请使用不同的配置文件。说application-ut.yaml并要求spring不要使用任何缓存实现库:

application-ut.yaml(添加以下条目并删除您为 Gemfire 配置的任何实现)

spring.cache.type : simple

我不确定我是否完全理解您的意思...

"For every test case execution, either through class or Maven build, Spring ApplicationContext fails to load if the GemFire cache is down."

您是否为测试中的每个测试用例(方法)重新创建 ClientCache 实例 class?

如果是这样,那么这可能会很棘手,因为即使在调用 ClientCache.close() 之后,GemFire 可能还没有完全 "closed" 并释放 ClientCache 实例使用的所有资源。但是,通常这不会阻止 Spring ApplicationContext 在随后的测试用例执行中被重新创建。它通常只会导致后续测试失败,因为 ClientCache 实例变脏或陈旧,保留了先前(或最后一次)测试用例执行的旧状态。

您是否也在测试用例方法中使用 Spring 的 @DirtiesContext

通常,每个测试 class 循环 ApplicationContext 和 GemFire 缓存实例(例如 ClientCache)是明智的,其中每个测试用例方法在测试 class 将使用相同的 ApplicationContextClientCache 实例;这是最理想的。

至此,我有两件事要与您分享:

  1. 首先,看看新的Spring Boot for Apache Geode/Pivotal GemFire project. Documentation is here. I announced这个项目近一个月前的可用性。该项目采用 "client-side" 视角来构建 Spring 使用 Pivotal GemFire 的引导应用程序。也就是说,它默认为您提供一个自动配置的 ClientCache 实例。

  2. 具体来说,请查看 Spring Boot for Pivotal GemFire 的 测试套件,从 here 开始。几乎所有这些测试 classes 都使用 ClientCache 实例并测试 Pivotal GemFire 的各个方面,例如 CQ 或安全性等。

  3. 在某些测试classes中,我使用了"mock"ClientCache实例(例如,这个test class, and this test configuration in particular). However, in many other cases, I used a live GemFire ClientCache instance, for example or this test class, which is interesting since this test class even launches a server用于ClientCache 要连接到的实例(测试本身)。

  4. Spring Boot for Apache Geode/Pivotal GemFire中的所有测试协调逻辑由另一个新项目提供,Spring Test for Apache Geode/Pivotal GemFire. Unfortunately, Spring Test for Apache Geode/Pivotal GemFire is still largely a WIP, so does not have documentation yet. However, I have used this new test project extensively to test Spring Boot for Apache Geode/Pivotal GemFire. You will see its presence in the extension classes, like ForkingClientServerIntegrationTestsSupport,等等。

总而言之,使用新的 Spring Boot for Pivotal GemFire & Spring 测试 Pivotal GemFire 项目作为编写更有效的单元和集成测试的指南。

最后,如果您有一个示例 GitHub 存储库重现您的问题,我可以帮助您指明正确的方向。

希望对您有所帮助!

此致, 约翰