merge upstream
This commit is contained in:
+119
@@ -0,0 +1,119 @@
|
||||
package com.baeldung.conditional;
|
||||
|
||||
import org.junit.jupiter.api.RepeatedTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.condition.*;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
public class ConditionalAnnotationsUnitTest {
|
||||
@Test
|
||||
@EnabledOnOs({OS.WINDOWS, OS.MAC})
|
||||
public void shouldRunBothWindowsAndMac() {
|
||||
System.out.println("runs on Windows and Mac");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisabledOnOs(OS.LINUX)
|
||||
public void shouldNotRunAtLinux() {
|
||||
System.out.println("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");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisabledOnJre(JRE.OTHER)
|
||||
public void thisTestOnlyRunsWithUpToDateJREs() {
|
||||
System.out.println("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");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisabledIfSystemProperty(named = "file.separator", matches = "[/]")
|
||||
public void disabledIfFileSeperatorIsSlash() {
|
||||
System.out.println("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");
|
||||
}
|
||||
|
||||
@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");
|
||||
}
|
||||
|
||||
@Test
|
||||
@EnabledIf("'FR' == systemProperty.get('user.country')")
|
||||
public void onlyFrenchPeopleWillRunThisMethod() {
|
||||
System.out.println("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");
|
||||
}
|
||||
|
||||
@Test
|
||||
@EnabledIf(value = {
|
||||
"load('nashorn:mozilla_compat.js')",
|
||||
"importPackage(java.time)",
|
||||
"",
|
||||
"var thisMonth = LocalDate.now().getMonth().name()",
|
||||
"var february = Month.FEBRUARY.name()",
|
||||
"thisMonth.equals(february)"
|
||||
},
|
||||
engine = "nashorn",
|
||||
reason = "Self-fulfilling: {result}")
|
||||
public void onlyRunsInFebruary() {
|
||||
System.out.println("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");
|
||||
}
|
||||
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Test
|
||||
@DisabledOnOs({OS.WINDOWS, OS.SOLARIS, OS.OTHER})
|
||||
@EnabledOnJre({JRE.JAVA_9, JRE.JAVA_10, JRE.JAVA_11})
|
||||
@interface ThisTestWillOnlyRunAtLinuxAndMacWithJava9Or10Or11 {
|
||||
}
|
||||
|
||||
@ThisTestWillOnlyRunAtLinuxAndMacWithJava9Or10Or11
|
||||
public void someSuperTestMethodHere() {
|
||||
System.out.println("this method will run with java9, 10, 11 and Linux or macOS.");
|
||||
}
|
||||
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@DisabledIf("Math.random() >= 0.5")
|
||||
@interface CoinToss {
|
||||
}
|
||||
|
||||
@RepeatedTest(2)
|
||||
@CoinToss
|
||||
public void gamble() {
|
||||
System.out.println("This tests run status is a gamble with %50 rate");
|
||||
}
|
||||
}
|
||||
@@ -54,6 +54,18 @@
|
||||
<artifactId>spring-data-jpa</artifactId>
|
||||
<version>LATEST</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>${junit.jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.awaitility</groupId>
|
||||
<artifactId>awaitility</artifactId>
|
||||
<version>${awaitility.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@@ -70,6 +82,8 @@
|
||||
<properties>
|
||||
<!-- testing -->
|
||||
<hamcrest.version>2.0.0.0</hamcrest.version>
|
||||
<awaitility.version>3.1.6</awaitility.version>
|
||||
<junit.jupiter.version>5.4.0</junit.jupiter.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.config;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
@Configuration
|
||||
@EnableScheduling
|
||||
@ComponentScan("com.baeldung.scheduled")
|
||||
public class ScheduledConfig {
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.scheduled;
|
||||
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
@Component
|
||||
public class Counter {
|
||||
private final AtomicInteger count = new AtomicInteger(0);
|
||||
|
||||
@Scheduled(fixedDelay = 5)
|
||||
public void scheduled() {
|
||||
this.count.incrementAndGet();
|
||||
}
|
||||
|
||||
public int getInvocationCount() {
|
||||
return this.count.get();
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.scheduled;
|
||||
|
||||
import com.baeldung.config.ScheduledConfig;
|
||||
import org.awaitility.Duration;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.mock.mockito.SpyBean;
|
||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
||||
|
||||
import static org.awaitility.Awaitility.await;
|
||||
import static org.mockito.Mockito.atLeast;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
@SpringJUnitConfig(ScheduledConfig.class)
|
||||
public class ScheduledAwaitilityIntegrationTest {
|
||||
|
||||
@SpyBean private Counter counter;
|
||||
|
||||
@Test
|
||||
public void whenWaitOneSecond_thenScheduledIsCalledAtLeastTenTimes() {
|
||||
await()
|
||||
.atMost(Duration.ONE_SECOND)
|
||||
.untilAsserted(() -> verify(counter, atLeast(10)).scheduled());
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.scheduled;
|
||||
|
||||
import com.baeldung.config.ScheduledConfig;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@SpringJUnitConfig(ScheduledConfig.class)
|
||||
public class ScheduledIntegrationTest {
|
||||
|
||||
@Autowired Counter counter;
|
||||
|
||||
@Test
|
||||
public void givenSleepBy100ms_whenGetInvocationCount_thenIsGreaterThanZero() throws InterruptedException {
|
||||
Thread.sleep(100L);
|
||||
|
||||
assertThat(counter.getInvocationCount()).isGreaterThan(0);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user