JAVA-8405: reducing logging for tutorials-build-job

This commit is contained in:
chaos2418
2021-11-18 11:19:23 +05:30
parent 4ca8e7ef23
commit a62c0f3c8b
81 changed files with 715 additions and 126 deletions

View File

@@ -20,12 +20,12 @@ public class RegisterExtensionSampleExtension implements BeforeAllCallback, Befo
@Override
public void beforeAll(ExtensionContext extensionContext) throws Exception {
logger.info("Type {} In beforeAll : {}", type, extensionContext.getDisplayName());
logger.debug("Type {} In beforeAll : {}", type, extensionContext.getDisplayName());
}
@Override
public void beforeEach(ExtensionContext extensionContext) throws Exception {
logger.info("Type {} In beforeEach : {}", type, extensionContext.getDisplayName());
logger.debug("Type {} In beforeEach : {}", type, extensionContext.getDisplayName());
}
public String getType() {

View File

@@ -1,41 +1,45 @@
package com.baeldung.junit5;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.RepetitionInfo;
import org.junit.jupiter.api.TestInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class RepeatedTestAnnotationUnitTest {
private static final Logger LOGGER = LoggerFactory.getLogger(RepeatedTestAnnotationUnitTest.class);
@BeforeEach
void beforeEachTest() {
System.out.println("Before Each Test");
LOGGER.debug("Before Each Test");
}
@AfterEach
void afterEachTest() {
System.out.println("After Each Test");
System.out.println("=====================");
LOGGER.debug("After Each Test");
LOGGER.debug("=====================");
}
@RepeatedTest(3)
void repeatedTest(TestInfo testInfo) {
System.out.println("Executing repeated test");
LOGGER.debug("Executing repeated test");
assertEquals(2, Math.addExact(1, 1), "1 + 1 should equal 2");
}
@RepeatedTest(value = 3, name = RepeatedTest.LONG_DISPLAY_NAME)
void repeatedTestWithLongName() {
System.out.println("Executing repeated test with long name");
LOGGER.debug("Executing repeated test with long name");
assertEquals(2, Math.addExact(1, 1), "1 + 1 should equal 2");
}
@RepeatedTest(value = 3, name = RepeatedTest.SHORT_DISPLAY_NAME)
void repeatedTestWithShortName() {
System.out.println("Executing repeated test with long name");
LOGGER.debug("Executing repeated test with long name");
assertEquals(2, Math.addExact(1, 1), "1 + 1 should equal 2");
}
@@ -46,7 +50,7 @@ public class RepeatedTestAnnotationUnitTest {
@RepeatedTest(3)
void repeatedTestWithRepetitionInfo(RepetitionInfo repetitionInfo) {
System.out.println("Repetition #" + repetitionInfo.getCurrentRepetition());
LOGGER.debug("Repetition # {}", repetitionInfo.getCurrentRepetition());
assertEquals(3, repetitionInfo.getTotalRepetitions());
}
}

View File

@@ -2,7 +2,20 @@ package com.baeldung.junit5.conditional;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.*;
import org.junit.jupiter.api.condition.DisabledForJreRange;
import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable;
import org.junit.jupiter.api.condition.DisabledIfSystemProperty;
import org.junit.jupiter.api.condition.DisabledOnJre;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.EnabledForJreRange;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
import org.junit.jupiter.api.condition.EnabledOnJre;
import org.junit.jupiter.api.condition.EnabledOnOs;
import org.junit.jupiter.api.condition.JRE;
import org.junit.jupiter.api.condition.OS;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
@@ -11,64 +24,66 @@ import java.lang.annotation.Target;
public class ConditionalAnnotationsUnitTest {
private static final Logger LOGGER = LoggerFactory.getLogger(ConditionalAnnotationsUnitTest.class);
@Test
@EnabledOnOs({OS.WINDOWS, OS.MAC})
public void shouldRunBothWindowsAndMac() {
System.out.println("runs on Windows and Mac");
LOGGER.debug("runs on Windows and Mac");
}
@Test
@DisabledOnOs(OS.LINUX)
public void shouldNotRunAtLinux() {
System.out.println("will not run on Linux");
LOGGER.debug("will not run on Linux");
}
@Test
@EnabledOnJre({JRE.JAVA_10, JRE.JAVA_11})
public void shouldOnlyRunOnJava10And11() {
System.out.println("runs with java 10 and 11");
LOGGER.debug("runs with java 10 and 11");
}
@Test
@EnabledForJreRange(min = JRE.JAVA_8, max = JRE.JAVA_13)
public void shouldOnlyRunOnJava8UntilJava13() {
System.out.println("runs with Java 8, 9, 10, 11, 12 and 13!");
LOGGER.debug("runs with Java 8, 9, 10, 11, 12 and 13!");
}
@Test
@DisabledForJreRange(min = JRE.JAVA_14, max = JRE.JAVA_15)
public void shouldNotBeRunOnJava14AndJava15() {
System.out.println("Shouldn't be run on Java 14 and 15.");
LOGGER.debug("Shouldn't be run on Java 14 and 15.");
}
@Test
@DisabledOnJre(JRE.OTHER)
public void thisTestOnlyRunsWithUpToDateJREs() {
System.out.println("this test will only run on java8, 9, 10 and 11.");
LOGGER.debug("this test will only run on java8, 9, 10 and 11.");
}
@Test
@EnabledIfSystemProperty(named = "java.vm.vendor", matches = "Oracle.*")
public void onlyIfVendorNameStartsWithOracle() {
System.out.println("runs only if vendor name starts with Oracle");
LOGGER.debug("runs only if vendor name starts with Oracle");
}
@Test
@DisabledIfSystemProperty(named = "file.separator", matches = "[/]")
public void disabledIfFileSeperatorIsSlash() {
System.out.println("Will not run if file.sepeartor property is /");
LOGGER.debug("Will not run if file.sepeartor property is /");
}
@Test
@EnabledIfEnvironmentVariable(named = "GDMSESSION", matches = "ubuntu")
public void onlyRunOnUbuntuServer() {
System.out.println("only runs if GDMSESSION is ubuntu");
LOGGER.debug("only runs if GDMSESSION is ubuntu");
}
@Test
@DisabledIfEnvironmentVariable(named = "LC_TIME", matches = ".*UTF-8.")
public void shouldNotRunWhenTimeIsNotUTF8() {
System.out.println("will not run if environment variable LC_TIME is UTF-8");
LOGGER.debug("will not run if environment variable LC_TIME is UTF-8");
}
// Commented codes are going to work prior JUnit 5.5
@@ -76,13 +91,13 @@ public class ConditionalAnnotationsUnitTest {
@Test
// @EnabledIf("'FR' == systemProperty.get('user.country')")
public void onlyFrenchPeopleWillRunThisMethod() {
System.out.println("will run only if user.country is FR");
LOGGER.debug("will run only if user.country is FR");
}
@Test
// @DisabledIf("java.lang.System.getProperty('os.name').toLowerCase().contains('mac')")
public void shouldNotRunOnMacOS() {
System.out.println("will not run if our os.name is mac");
LOGGER.debug("will not run if our os.name is mac");
}
@Test
@@ -97,14 +112,14 @@ public class ConditionalAnnotationsUnitTest {
engine = "nashorn",
reason = "Self-fulfilling: {result}")*/
public void onlyRunsInFebruary() {
System.out.println("this test only runs in February");
LOGGER.debug("this test only runs in February");
}
@Test
/*@DisabledIf("systemEnvironment.get('XPC_SERVICE_NAME') != null " +
"&& systemEnvironment.get('XPC_SERVICE_NAME').contains('intellij')")*/
public void notValidForIntelliJ() {
System.out.println("this test will run if our ide is INTELLIJ");
LOGGER.debug("this test will run if our ide is INTELLIJ");
}
@Target(ElementType.METHOD)
@@ -117,7 +132,7 @@ public class ConditionalAnnotationsUnitTest {
@ThisTestWillOnlyRunAtLinuxAndMacWithJava9Or10Or11
public void someSuperTestMethodHere() {
System.out.println("this method will run with java9, 10, 11 and Linux or macOS.");
LOGGER.debug("this method will run with java9, 10, 11 and Linux or macOS.");
}
@Target(ElementType.METHOD)
@@ -129,6 +144,6 @@ public class ConditionalAnnotationsUnitTest {
@RepeatedTest(2)
@CoinToss
public void gamble() {
System.out.println("This tests run status is a gamble with %50 rate");
LOGGER.debug("This tests run status is a gamble with %50 rate");
}
}

View File

@@ -3,11 +3,16 @@ package com.baeldung.junit5.templates;
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.Properties;
public class DisabledOnQAEnvironmentExtension implements ExecutionCondition {
private static final Logger LOGGER = LoggerFactory.getLogger(DisabledOnQAEnvironmentExtension.class);
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
Properties properties = new Properties();
@@ -16,7 +21,7 @@ public class DisabledOnQAEnvironmentExtension implements ExecutionCondition {
.getResourceAsStream("application.properties"));
if ("qa".equalsIgnoreCase(properties.getProperty("env"))) {
String reason = String.format("The test '%s' is disabled on QA environment", context.getDisplayName());
System.out.println(reason);
LOGGER.debug(reason);
return ConditionEvaluationResult.disabled(reason);
}
} catch (IOException e) {

View File

@@ -1,6 +1,15 @@
package com.baeldung.junit5.templates;
import org.junit.jupiter.api.extension.*;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.AfterTestExecutionCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.BeforeTestExecutionCallback;
import org.junit.jupiter.api.extension.Extension;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestTemplateInvocationContext;
import org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.stream.Stream;
@@ -9,6 +18,8 @@ import static java.util.Arrays.asList;
public class UserIdGeneratorTestInvocationContextProvider implements TestTemplateInvocationContextProvider {
private static final Logger LOGGER = LoggerFactory.getLogger(UserIdGeneratorTestInvocationContextProvider.class);
@Override
public boolean supportsTestTemplate(ExtensionContext extensionContext) {
return true;
@@ -44,13 +55,13 @@ public class UserIdGeneratorTestInvocationContextProvider implements TestTemplat
new BeforeTestExecutionCallback() {
@Override
public void beforeTestExecution(ExtensionContext extensionContext) {
System.out.println("BeforeTestExecutionCallback:Disabled context");
LOGGER.debug("BeforeTestExecutionCallback:Disabled context");
}
},
new AfterTestExecutionCallback() {
@Override
public void afterTestExecution(ExtensionContext extensionContext) {
System.out.println("AfterTestExecutionCallback:Disabled context");
LOGGER.debug("AfterTestExecutionCallback:Disabled context");
}
});
}
@@ -72,13 +83,13 @@ public class UserIdGeneratorTestInvocationContextProvider implements TestTemplat
new BeforeEachCallback() {
@Override
public void beforeEach(ExtensionContext extensionContext) {
System.out.println("BeforeEachCallback:Enabled context");
LOGGER.debug("BeforeEachCallback:Enabled context");
}
},
new AfterEachCallback() {
@Override
public void afterEach(ExtensionContext extensionContext) {
System.out.println("AfterEachCallback:Enabled context");
LOGGER.debug("AfterEachCallback:Enabled context");
}
});
}