From 404fce84949176cfd3d636304a916c29068eba4c Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Thu, 23 Jun 2022 16:33:58 -0400 Subject: [PATCH] Fix `MongoDbAvailableRule` for `assumeTrue()` Currently, the `MongoDbAvailableRule` logs a warning about missed availability of the MongoDb server and returns from the `Statement.evaluate()` making the test not ignored but as passed * Fix the exception handling in the `MongoDbAvailableRule` for the `Statement` to call `Assume.assumeTrue()` instead of the plain logging to mark the test as ignored. This will make a JUnit test report looking correctly and also will let Sonar Cube to report test coverage as adequate **Cherry-pick to `5.5.x`** --- .../mongodb/rules/MongoDbAvailableRule.java | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/spring-integration-mongodb/src/test/java/org/springframework/integration/mongodb/rules/MongoDbAvailableRule.java b/spring-integration-mongodb/src/test/java/org/springframework/integration/mongodb/rules/MongoDbAvailableRule.java index 86e774d782..0282069b2c 100644 --- a/spring-integration-mongodb/src/test/java/org/springframework/integration/mongodb/rules/MongoDbAvailableRule.java +++ b/spring-integration-mongodb/src/test/java/org/springframework/integration/mongodb/rules/MongoDbAvailableRule.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,8 +18,7 @@ package org.springframework.integration.mongodb.rules; import java.util.concurrent.TimeUnit; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; +import org.junit.Assume; import org.junit.rules.MethodRule; import org.junit.runners.model.FrameworkMethod; import org.junit.runners.model.Statement; @@ -39,11 +38,10 @@ import com.mongodb.client.internal.MongoClientImpl; */ public final class MongoDbAvailableRule implements MethodRule { - private final Log logger = LogFactory.getLog(this.getClass()); - @Override public Statement apply(final Statement base, final FrameworkMethod method, final Object target) { return new Statement() { + @Override public void evaluate() throws Throwable { MongoDbAvailable mongoAvailable = method.getAnnotation(MongoDbAvailable.class); @@ -57,9 +55,10 @@ public final class MongoDbAvailableRule implements MethodRule { .first(); } catch (Exception e) { - logger.warn("MongoDb is not available. Skipping the test: " + - target.getClass().getSimpleName() + "." + method.getName() + "()"); - return; + Assume.assumeTrue( + "Skipping test due to MongoDb not being available on hosts: " + + settings.getClusterSettings().getHosts() + ":\n" + e, + false); } } base.evaluate();