如何在不激活断路器的情况下对服务进行单元测试。

huangapple go评论102阅读模式
英文:

How to unit test a Service using Circuit Breaker without activating the Circuit Breaker

问题

我在我的服务类中添加了一个断路器(Circuit Breaker),但我注意到之后所有的单元测试都失败了。

  • 有没有一种好的方法来测试实现了**断路器(Circuit Breaker)**的服务,而不会破坏之前的单元测试?
  • 有没有一种方法可以在运行测试时模拟**断路器(Circuit Breaker)**或禁用它?
  • **断路器(Circuit Breaker)**应该只在集成测试中进行测试吗?

以下是我的代码:

  1. public class PersonService {
  2. private final CircuitBreakerFactory circuitBreakerFactory;
  3. public PersonService(CircuitBreakerFactory circuitBreakerFactory) {
  4. this.circuitBreakerFactory = circuitBreakerFactory;
  5. }
  6. public List<Item> getPersonItems() {
  7. return circuitBreakerConfiguration.create("CB_Service").run(() -> apiCall.getItems(), exception -> {
  8. log.error("An error occurs");
  9. throw new ItemServiceException("No Item available");
  10. });
  11. }
  12. }
英文:

I add a Circuit Breaker in my service class, but I noticed all my unit tests are failing after that.

  • Is there a good way to test a service implementing a Circuit Breaker without breaking previous unit tests
  • Is there a way to mock the Circuit Breaker or disable it while running my tests
  • Should Circuit Breaker should be test only in integration tests.

Here is my code:

  1. public class PersonService {
  2. private final CircuitBreakerFactory circuitBreakerFactory;
  3. public PersonService(CircuitBreakerFactory circuitBreakerFactory) {
  4. this.circuitBreakerFactory = circuitBreakerFactory;
  5. }
  6. public List&lt;Item&gt; getPersonItems() {
  7. return circuitBreakerConfiguration.create(&quot;CB_Service&quot;).run(() -&gt; apiCall.getItems(), exception -&gt; {
  8. log.error(&quot;An error occurs&quot;);
  9. throw new ItemServiceException(&quot;No Item available&quot;);
  10. });
  11. }
  12. }

答案1

得分: 1

你可能需要在测试中触发断路器后进行复位,特别是在你知道计数器正在增加的情况下。

我们使用resilience4j,它有一个reset()方法,详见 - https://resilience4j.readme.io/docs/examples#reset-circuitbreaker

我们将其用于@AfterEach测试中,CircuitBreakerUtil只是CircuitBreakerRegistry的包装器。

  1. @AfterEach
  2. public void stopCircuitBreakerTripping() {
  3. CircuitBreakerUtil.get("Breaker").reset();
  4. }
  1. <details>
  2. <summary>英文:</summary>
  3. You should probably reset the circuit breaker after tripping it in tests you know are incrementing the counter
  4. We use resilience4j and it has a `reset()` method see - https://resilience4j.readme.io/docs/examples#reset-circuitbreaker.
  5. We use it in the `@AfterEach` for tests CircuitBreakerUtil is just a wrapper for the [CircuitBreakerRegistry][1].
  6. @AfterEach
  7. public void stopCircuitBreakerTripping() {
  8. CircuitBreakerUtil.get(&quot;Breaker&quot;).reset();
  9. }
  10. [1]: https://resilience4j.readme.io/docs/examples#override-the-registrystore
  11. </details>

huangapple
  • 本文由 发表于 2023年7月3日 23:21:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76606121.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定