Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content
This repository was archived by the owner on Oct 4, 2020. It is now read-only.

Commit 627028b

Browse files
author
nipa
committed
Test and demonstrate all features
1 parent f4ab1af commit 627028b

11 files changed

+701
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.codefx.lab.junitlambda;
2+
3+
import static java.lang.String.format;
4+
5+
public class Console {
6+
7+
public static void print(String message, Object... args) {
8+
System.out.println(format("\t >> " + message, args));
9+
}
10+
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.codefx.lab.junitlambda;
2+
3+
import static java.util.Objects.requireNonNull;
4+
import static org.codefx.lab.junitlambda.Console.print;
5+
6+
/**
7+
* Highly complicated server interface with complex initialisation.
8+
*/
9+
public class Server {
10+
11+
private final String url;
12+
13+
public Server(String url) {
14+
this.url = requireNonNull(url, "The argument 'url' must not be null.");
15+
}
16+
17+
public int sendRequest(String request) {
18+
print("Sending '%s' to '%s'...", request, url);
19+
return 200;
20+
}
21+
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package org.codefx.lab.junitlambda;
2+
3+
import org.junit.gen5.api.AfterAll;
4+
import org.junit.gen5.api.AfterEach;
5+
import org.junit.gen5.api.BeforeAll;
6+
import org.junit.gen5.api.BeforeEach;
7+
import org.junit.gen5.api.Disabled;
8+
import org.junit.gen5.api.Name;
9+
import org.junit.gen5.api.Test;
10+
11+
import static org.codefx.lab.junitlambda.Console.print;
12+
13+
/**
14+
* How to set up, execute and tear down tests.
15+
*/
16+
@Name("JUnit Lambda: Basic Feature Test")
17+
public class _0_BasicFeatures {
18+
19+
// SET UP, TEAR DOWN
20+
21+
@BeforeAll
22+
public static void setUpTestClass() {
23+
print("Executed once, before all tests in this class.");
24+
}
25+
26+
@BeforeEach
27+
public void setUpTestMethod() {
28+
print("Executed before each test in this class.");
29+
}
30+
31+
@AfterEach
32+
public void tearDownTestMethod() {
33+
print("Executed after each test in this class.");
34+
}
35+
36+
@AfterAll
37+
public static void tearDownTestClass() {
38+
print("Executed once, after all tests in this class.");
39+
}
40+
41+
// TESTS
42+
43+
public void test_doesNotRunNotAnnotatedMethods() {
44+
// the method is named 'test...' to lure JUnit into running it old school style
45+
throw new AssertionError("This method should not be run!");
46+
}
47+
48+
@Test
49+
public void runsAnAnnotatedMethod() {
50+
print("Hello World!");
51+
}
52+
53+
@Test
54+
@Name("test name annotation")
55+
public void showsName() {
56+
print("This test should have a nice name.");
57+
}
58+
59+
@Disabled
60+
@Test
61+
public void disabledTestsAreNotRun() {
62+
// '@Disabled' is the new '@Ignored'
63+
throw new AssertionError("This method should not be run!");
64+
}
65+
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package org.codefx.lab.junitlambda;
2+
3+
import org.junit.gen5.api.Test;
4+
5+
import java.io.IOException;
6+
import java.util.function.BooleanSupplier;
7+
8+
import static org.junit.gen5.api.Assertions.assertAll;
9+
import static org.junit.gen5.api.Assertions.assertEquals;
10+
import static org.junit.gen5.api.Assertions.assertFalse;
11+
import static org.junit.gen5.api.Assertions.assertNotEquals;
12+
import static org.junit.gen5.api.Assertions.assertNotNull;
13+
import static org.junit.gen5.api.Assertions.assertNotSame;
14+
import static org.junit.gen5.api.Assertions.assertNull;
15+
import static org.junit.gen5.api.Assertions.assertSame;
16+
import static org.junit.gen5.api.Assertions.assertTrue;
17+
import static org.junit.gen5.api.Assertions.expectThrows;
18+
19+
public class _1_Assertions {
20+
21+
@Test
22+
public void boringAssertions() {
23+
String mango = "Mango";
24+
25+
// as usual: expected, actual
26+
assertEquals("Mango", mango);
27+
assertNotEquals("Banana", mango);
28+
assertSame(mango, mango);
29+
assertNotSame(new String(mango), mango);
30+
31+
assertNull(null);
32+
assertNotNull(mango);
33+
assertFalse(false);
34+
assertTrue(true);
35+
}
36+
37+
@Test
38+
public void interestingAssertions() {
39+
String mango = "Mango";
40+
41+
// message comes last
42+
assertEquals("Mango", mango, "Y U no equal?!");
43+
44+
// message can be created lazily
45+
assertEquals("Mango", mango, () -> "Expensive string, creation deferred until needed.");
46+
47+
// for 'assert[True|False]' it is possible to directly test a supplier that exists somewhere in the code
48+
BooleanSupplier existingBooleanSupplier = () -> true;
49+
assertTrue(existingBooleanSupplier);
50+
}
51+
52+
@Test
53+
public void exceptionAssertions() {
54+
IOException exception = expectThrows(
55+
IOException.class,
56+
() -> {
57+
throw new IOException("Something bad happened");
58+
});
59+
assertTrue(exception.getMessage().contains("Something bad"));
60+
}
61+
62+
@Test
63+
public void groupedAssertions() {
64+
assertAll("Multiplication",
65+
() -> assertEquals(15, 3 * 5, "3 x 5 = 15"),
66+
// this fails on purpose to see what the message looks like
67+
() -> assertEquals(15, 5 + 3, "5 x 3 = 15")
68+
);
69+
}
70+
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.codefx.lab.junitlambda;
2+
3+
import org.junit.gen5.api.AfterAll;
4+
import org.junit.gen5.api.BeforeAll;
5+
import org.junit.gen5.api.Test;
6+
import org.junit.gen5.api.TestInstance;
7+
import org.junit.gen5.api.TestInstance.Lifecycle;
8+
9+
import static org.junit.gen5.api.Assertions.assertEquals;
10+
11+
/**
12+
* Note the annotation {@code @TestInstance(Lifecycle.PER_CLASS)}, which leads to having a fixed instance of this class
13+
* for all contained tests. This allows to have state across tests. (Why do we want this?)
14+
*/
15+
@TestInstance(Lifecycle.PER_CLASS)
16+
public class _2_PerClassLifecycle {
17+
18+
/**
19+
* There are {@link #oneMethod()} and {@link #otherMethod()}, so the value is 2.
20+
*/
21+
private static final int EXPECTED_TEST_METHOD_COUNT = 2;
22+
23+
/**
24+
* Is incremented by every test method.
25+
*/
26+
private int executedTestMethodCount = Integer.MIN_VALUE;
27+
28+
/*
29+
* Note that the following @[Before|After]All methods are _not_ static!
30+
* They don't have to be because this test class has a lifecycle PER_CLASS.
31+
*/
32+
33+
@BeforeAll
34+
public void initializeCounter() {
35+
executedTestMethodCount = 0;
36+
}
37+
38+
@AfterAll
39+
public void assertAllMethodsExecuted() {
40+
assertEquals(EXPECTED_TEST_METHOD_COUNT, executedTestMethodCount);
41+
}
42+
43+
@Test
44+
public void oneMethod() {
45+
executedTestMethodCount++;
46+
}
47+
48+
@Test
49+
public void otherMethod() {
50+
executedTestMethodCount++;
51+
}
52+
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.codefx.lab.junitlambda;
2+
3+
import org.junit.gen5.api.AfterAll;
4+
import org.junit.gen5.api.BeforeAll;
5+
import org.junit.gen5.api.Test;
6+
import org.junit.gen5.api.TestInstance;
7+
import org.junit.gen5.api.TestInstance.Lifecycle;
8+
9+
import static org.junit.gen5.api.Assertions.assertEquals;
10+
11+
/**
12+
* Classes and methods are also executed if they only have default visibility, which reduces verbosity of tests.
13+
*/
14+
@TestInstance(Lifecycle.PER_CLASS)
15+
class _3_Visibility {
16+
17+
// we use the PER_CLASS lifecycle to assert that all methods are executed;
18+
19+
private static final int EXPECTED_TEST_METHOD_COUNT = 2;
20+
private int executedTestMethodCount = Integer.MIN_VALUE;
21+
22+
@BeforeAll
23+
void initializeCounter() {
24+
// note that if this would not run (because it is not public), the test would fail
25+
executedTestMethodCount = 0;
26+
}
27+
28+
@AfterAll
29+
public void assertAllMethodsExecuted() {
30+
// this method is public because we need it to execute
31+
// and we already know that public methods to get executed
32+
assertEquals(EXPECTED_TEST_METHOD_COUNT, executedTestMethodCount);
33+
}
34+
35+
// TESTS
36+
37+
@Test
38+
public void runsPublicTestMethods() {
39+
executedTestMethodCount++;
40+
}
41+
42+
@Test
43+
void runsPackageVisibleTestMethods() {
44+
executedTestMethodCount++;
45+
}
46+
47+
@Test
48+
private void doesNotRunPrivateTestMethods() {
49+
throw new AssertionError("This method should not be run!");
50+
}
51+
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.codefx.lab.junitlambda;
2+
3+
import org.junit.gen5.api.AfterAll;
4+
import org.junit.gen5.api.Nested;
5+
import org.junit.gen5.api.Test;
6+
import org.junit.gen5.api.TestInstance;
7+
import org.junit.gen5.api.TestInstance.Lifecycle;
8+
9+
import static org.junit.gen5.api.Assertions.assertEquals;
10+
11+
/**
12+
* It is now really easy to group test methods into inner classes and have them be executed as well.
13+
*/
14+
@TestInstance(Lifecycle.PER_CLASS)
15+
class _4_InnerClasses {
16+
17+
// we use the PER_CLASS lifecycle to assert that all tests are executed
18+
19+
private static final int EXPECTED_TEST_METHOD_COUNT = 2;
20+
private int executedTestMethodCount = 0;
21+
22+
@AfterAll
23+
void assertAllMethodsExecuted() {
24+
assertEquals(EXPECTED_TEST_METHOD_COUNT, executedTestMethodCount);
25+
}
26+
27+
// TESTS
28+
29+
@Nested
30+
class SomeInnerClass {
31+
32+
@Test
33+
void someTestMethod() {
34+
executedTestMethodCount++;
35+
}
36+
37+
}
38+
39+
@Nested
40+
class OtherInnerClass {
41+
42+
@Test
43+
void someTestMethod() {
44+
executedTestMethodCount++;
45+
}
46+
47+
}
48+
49+
@Nested
50+
static class StaticClass {
51+
52+
@Test
53+
public void doesNotRunTestMethodsInStaticInnerClasses() {
54+
// the documentation says "Notice that only non-static inner classes can serve as contexts."
55+
// but this runs anyway... strange
56+
throw new AssertionError("This method should not be run!");
57+
}
58+
59+
}
60+
61+
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.codefx.lab.junitlambda;
2+
3+
import org.junit.gen5.api.AfterAll;
4+
import org.junit.gen5.api.Test;
5+
import org.junit.gen5.api.TestInstance;
6+
import org.junit.gen5.api.TestInstance.Lifecycle;
7+
8+
import static org.junit.gen5.api.Assertions.assertEquals;
9+
import static org.junit.gen5.api.Assumptions.assumeFalse;
10+
import static org.junit.gen5.api.Assumptions.assumeTrue;
11+
import static org.junit.gen5.api.Assumptions.assumingThat;
12+
13+
/**
14+
* Assumptions can be used to dynamically abort tests.
15+
*/
16+
@TestInstance(Lifecycle.PER_CLASS)
17+
class _5_Assumptions {
18+
19+
// we use the PER_CLASS lifecycle to assert that all tests are executed
20+
21+
private static final int EXPECTED_TEST_METHOD_COUNT = 2;
22+
private int executedTestMethodCount = 0;
23+
24+
@AfterAll
25+
void assertAllMethodsExecuted() {
26+
assertEquals(EXPECTED_TEST_METHOD_COUNT, executedTestMethodCount);
27+
}
28+
29+
// TESTS
30+
31+
@Test
32+
void assumeTrue_true() {
33+
assumeTrue(true);
34+
executedTestMethodCount++;
35+
}
36+
37+
@Test
38+
void assumeFalse_true() {
39+
assumeFalse(true);
40+
String message = "If you can see this, 'assumeFalse(true)' passed, which it obviously shouldn't.";
41+
throw new AssertionError(message);
42+
}
43+
44+
@Test
45+
void assumeThat_trueAndFalse() {
46+
assumingThat(true, () -> executedTestMethodCount++);
47+
assumingThat(false, () -> {
48+
String message = "If you can see this, 'assumeFalse(true)' passed, which it obviously shouldn't.";
49+
throw new AssertionError(message);
50+
});
51+
}
52+
53+
}

0 commit comments

Comments
 (0)