Skip to content

Commit

Permalink
Run more demos as part of the build (#791)
Browse files Browse the repository at this point in the history
Some demo methods and classes weren't correctly annotated with `@Test`
or `@Nested` respectively and were thus not executed as part of the
build even though they should've been. For tests/classes that were not
supposed to be executed, that was not always obvious.

This change fixes that.

PR: #791

Co-authored-by: Ilia Ilinykh <ilyasyoy@gmail.com>
  • Loading branch information
nipafx and IlyasYOY committed Nov 9, 2023
1 parent 4029e59 commit 504c77a
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 49 deletions.
1 change: 1 addition & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ The least we can do is to thank them and list some of their accomplishments here
==== 2023
* https://github.com/eeverman[Eric Everman] added `@RestoreSystemProperties` and `@RestoreEnvironmentVariables` annotations to the https://junit-pioneer.org/docs/system-properties/[System Properties] and https://junit-pioneer.org/docs/environment-variables/[Environment Variables] extensions (#574 / #700)
* https://github.com/meredrica[Florian Westreicher] contributed to the JSON argument source extension (#704 / #724)
* https://github.com/IlyasYOY[Ilya Ilyinykh] found unused demo tests (#791)
* https://github.com/petrandreev[Pёtr Andreev] added back support for NULL values to `@CartesianTestExtension` (#764 / #765)

==== 2022
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Locale;
import java.util.TimeZone;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

public class DefaultLocaleTimezoneExtensionDemo {
Expand Down Expand Up @@ -48,6 +49,7 @@ void test_with_language_and_country_and_vairant() {
}
// end::default_locale_language_alternatives[]

@Nested
// tag::default_locale_class_level[]
@DefaultLocale(language = "fr")
class MyLocaleTests {
Expand Down Expand Up @@ -80,6 +82,7 @@ void test_with_long_zone_id() {
}
// end::default_timezone_zone[]

@Nested
// tag::default_timezone_class_level[]
@DefaultTimeZone("CET")
class MyTimeZoneTests {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

public class DisableIfTestFailsExtensionDemo {

// these tests fail intentionally ~> no @Nested
// tag::disable_if_test_fails[]
@DisableIfTestFails
// this annotation ensures that tests are run in the order of
Expand All @@ -48,10 +49,11 @@ void test3() {
}
// end::disable_if_test_fails[]

// these tests fail intentionally ~> no @Nested
// tag::disable_if_test_not_on_assertions[]
@DisableIfTestFails(onAssertion = false)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
static class ThreeTestsWithSecondFailingWithUnconfiguredAssertionTestCase {
class ThreeTestsWithSecondFailingWithUnconfiguredAssertionTestCase {

@Test
@Order(1)
Expand All @@ -73,10 +75,11 @@ void test3() {
}
// end::disable_if_test_not_on_assertions[]

// these tests fail intentionally ~> no @Nested
// tag::disable_if_test_with_given_exception[]
@DisableIfTestFails(with = IOException.class)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
static class ThreeTestsWithSecondThrowingConfiguredExceptionTestCase {
class ThreeTestsWithSecondThrowingConfiguredExceptionTestCase {

@Test
@Order(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

package org.junitpioneer.jupiter;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

public class DisableUntilExtensionDemo {
Expand All @@ -30,6 +31,7 @@ void testWithReason() {
}
// end::disable_until_with_reason[]

@Nested
// tag::disable_until_at_class_level[]
@DisabledUntil(date = "2022-01-01")
class TestClass {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ void testClearAndSet() {
}
// end::environment_using_set_and_clear[]

@Nested
// tag::environment_using_at_class_level[]
@ClearEnvironmentVariable(key = "some variable")
class MyEnvironmentVariableTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.ArrayList;
import java.util.List;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

public class StdInOutExtensionDemo {
Expand Down Expand Up @@ -86,6 +87,7 @@ public void readLines() throws IOException {
}
// end::stdio_edge_cases_ExampleConsoleReader[]

@Nested
// tag::stdio_edge_cases_ConsoleReaderTest[]
class ConsoleReaderTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

package org.junitpioneer.jupiter;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

public class StopwatchExtensionDemo {
Expand All @@ -22,6 +23,7 @@ void test() {
}
// end::method[]

@Nested
// tag::class[]
@Stopwatch
class TestCases {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ void testClearingAndSettingProperty() {
}
// end::systemproperty_using_set_and_clear[]

@Nested
// tag::systemproperty_using_at_class_level[]
@ClearSystemProperty(key = "some property")
class MySystemPropertyTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ void deconstructFromArray(

}

static class Misc {
// these tests fail intentionally ~> no @Nested
class Failing {

// tag::use_object_mapper_example[]
@ParameterizedTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,55 +78,56 @@ void interceptMatches(String value) {
}
// end::disable_parameterized_different_rules_for_different_parameters_matches[]

class TheseTestsWillFailIntentionally {
// tag::disable_parameterized_contains_in_all_tokens[]
@DisableIfAllArguments(contains = "the")
@ParameterizedTest
@CsvSource(value = { "If the swift moment I entreat:;Tarry a while! You are so fair!",
"Then forge the shackles to my feet,;Then I will gladly perish there!",
"Then let them toll the passing-bell,;Then of your servitude be free,",
"The clock may stop, its hands fall still,;And time be over then for me!" }, delimiter = ';')
void disableAllContains(String line, String line2) {
// ...
}
// end::disable_parameterized_contains_in_all_tokens[]

// tag::disable_parameterized_contains_in_all_tokens[]
@DisableIfAllArguments(contains = "the")
@ParameterizedTest
@CsvSource(value = { "If the swift moment I entreat:;Tarry a while! You are so fair!",
"Then forge the shackles to my feet,;Then I will gladly perish there!",
"Then let them toll the passing-bell,;Then of your servitude be free,",
"The clock may stop, its hands fall still,;And time be over then for me!" }, delimiter = ';')
void disableAllContains(String line, String line2) {
// ...
}
// end::disable_parameterized_contains_in_all_tokens[]
// tag::disable_parameterized_contains_in_any_token[]
@DisableIfAnyArgument(contains = "Then")
@ParameterizedTest
@CsvSource(value = { "If the swift moment I entreat:;Tarry a while! You are so fair!",
"Then forge the shackles to my feet,;Then I will gladly perish there!",
"Then let them toll the passing-bell,;Then of your servitude be free,",
"The clock may stop, its hands fall still,;And time be over then for me!" }, delimiter = ';')
void disableAnyContains(String line, String line2) {
// ...
}
// end::disable_parameterized_contains_in_any_token[]

// tag::disable_parameterized_contains_in_any_token[]
@DisableIfAnyArgument(contains = "Then")
@ParameterizedTest
@CsvSource(value = { "If the swift moment I entreat:;Tarry a while! You are so fair!",
"Then forge the shackles to my feet,;Then I will gladly perish there!",
"Then let them toll the passing-bell,;Then of your servitude be free,",
"The clock may stop, its hands fall still,;And time be over then for me!" }, delimiter = ';')
void disableAnyContains(String line, String line2) {
// ...
}
// end::disable_parameterized_contains_in_any_token[]
// tag::disable_parameterized_contains_multiple_arguments[]
@DisableIfAnyArgument(contains = { "Then", "then" })
@ParameterizedTest
@CsvSource(value = { "If the swift moment I entreat:;Tarry a while! You are so fair!",
"Then forge the shackles to my feet,;Then I will gladly perish there!",
"Then let them toll the passing-bell,;Then of your servitude be free,",
"The clock may stop, its hands fall still,;And time be over then for me!" }, delimiter = ';')
void disableAnyContainsMultipleArguments(String line, String line2) {
// [...]
}
// end::disable_parameterized_contains_multiple_arguments[]

// tag::disable_parameterized_contains_multiple_arguments[]
@DisableIfAnyArgument(contains = { "Then", "then" })
@ParameterizedTest
@CsvSource(value = { "If the swift moment I entreat:;Tarry a while! You are so fair!",
"Then forge the shackles to my feet,;Then I will gladly perish there!",
"Then let them toll the passing-bell,;Then of your servitude be free,",
"The clock may stop, its hands fall still,;And time be over then for me!" }, delimiter = ';')
void disableAnyContainsMultipleArguments(String line, String line2) {
// [...]
}
// end::disable_parameterized_contains_multiple_arguments[]
// tag::disable_parameterized_matches_all_arguments[]
@DisableIfAllArguments(matches = ".*\\s[a-z]{3}\\s.*")
@ParameterizedTest
@CsvSource(value = { "If the swift moment I entreat:;Tarry a while! You are so fair!",
"Then forge the shackles to my feet,;Then I will gladly perish there!",
"Then let them toll the passing-bell,;Then of your servitude be free,",
"The clock may stop, its hands fall still,;And time be over then for me!" }, delimiter = ';')
void interceptMatchesAny(String line, String line2) {
// [...]
}
// end::disable_parameterized_matches_all_arguments[]

// tag::disable_parameterized_matches_all_arguments[]
@DisableIfAllArguments(matches = ".*\\s[a-z]{3}\\s.*")
@ParameterizedTest
@CsvSource(value = { "If the swift moment I entreat:;Tarry a while! You are so fair!",
"Then forge the shackles to my feet,;Then I will gladly perish there!",
"Then let them toll the passing-bell,;Then of your servitude be free,",
"The clock may stop, its hands fall still,;And time be over then for me!" }, delimiter = ';')
void interceptMatchesAny(String line, String line2) {
// [...]
}
// end::disable_parameterized_matches_all_arguments[]
// these tests fail intentionally ~> no @Nested
class TheseTestsWillFailIntentionally {

// tag::disable_parameterized_named_parameter_contains[]
@DisableIfArgument(name = "line2", contains = "swift")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,60 @@
import java.nio.file.Path;
import java.nio.file.Paths;

import org.junit.jupiter.api.Test;

public class ResourceExtensionDemo {

// tag::create_new_resources_demo[]
@Test
void test1(@New(TemporaryDirectory.class) Path tempDir) {
// Test code goes here, e.g.,
assertTrue(Files.exists(tempDir));
}

@Test
void test2(@New(TemporaryDirectory.class) Path tempDir) {
// This temporary directory is different to the first one.
}
// end::create_new_resources_demo[]

// tag::create_new_dir_demo[]
@Test
void dirTest1(@Dir Path tempDir) {
// Test code goes here, e.g.,
assertTrue(Files.exists(tempDir));
}

@Test
void dirTest2(@Dir Path tempDir) {
// This temporary directory is different to the first one.
}
// end::create_new_dir_demo[]

// @formatter:off
// tag::create_new_resource_with_arg_demo[]
@Test
void testWithArg(
@New(value = TemporaryDirectory.class, arguments = "customPrefix")
Path tempDir) {
// Test code goes here, e.g.,
Path rootTempDir = Paths.get(System.getProperty("java.io.tmpdir"));
assertTrue(rootTempDir.relativize(tempDir).startsWith("customPrefix"));
assertTrue(rootTempDir.relativize(tempDir).toString().startsWith("customPrefix"));
}
// end::create_new_resource_with_arg_demo[]
// @formatter:on

// @formatter:off
// tag::create_shared_resource_demo[]
@Test
void sharedResourceTest1(
@Shared(factory = TemporaryDirectory.class, name = "sharedTempDir")
Path sharedTempDir) {
// Test code goes here, e.g.,
assertTrue(Files.exists(sharedTempDir));
}

@Test
void sharedResourceTest2(
@Shared(factory = TemporaryDirectory.class, name = "sharedTempDir")
Path sharedTempDir) {
Expand All @@ -73,18 +82,21 @@ void sharedResourceTest2(

// @formatter:off
// tag::create_multiple_shared_resources_demo[]
@Test
void firstSharedResource1(
@Shared(factory = TemporaryDirectory.class, name = "first")
Path first) {
// Test code working with first shared resource...
}

@Test
void firstSharedResource2(
@Shared(factory = TemporaryDirectory.class, name = "first")
Path first) {
// Test code working with first shared resource...
}

@Test
void secondSharedResource(
@Shared(factory = TemporaryDirectory.class, name = "second")
Path second) {
Expand All @@ -99,6 +111,7 @@ void secondSharedResource(
// tag::create_global_shared_resource_demo_first[]
class FirstTest {

@Test
void test(
@Shared(
factory = TemporaryDirectory.class,
Expand All @@ -116,6 +129,7 @@ void test(
// tag::create_global_shared_resource_demo_second[]
class SecondTest {

@Test
void test(
@Shared(
factory = TemporaryDirectory.class,
Expand Down

0 comments on commit 504c77a

Please sign in to comment.