Merge pull request #7298 from amit2103/BAEL-12826
[BAEL-12826] - Let's break the junit-5 module
This commit is contained in:
@@ -1,5 +1,11 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [The Basics of JUnit 5 – A Preview](http://www.baeldung.com/junit-5-preview)
|
||||
- [A Guide to JUnit 5](http://www.baeldung.com/junit-5)
|
||||
- [JUnit5 @RunWith](http://www.baeldung.com/junit-5-runwith)
|
||||
- [Get the Path of the /src/test/resources Directory in JUnit](https://www.baeldung.com/junit-src-test-resources-directory-path)
|
||||
- [Tagging and Filtering JUnit Tests](https://www.baeldung.com/junit-filtering-tests)
|
||||
- [JUnit 5 Temporary Directory Support](https://www.baeldung.com/junit-5-temporary-directory)
|
||||
- [@Before vs @BeforeClass vs @BeforeEach vs @BeforeAll](http://www.baeldung.com/junit-before-beforeclass-beforeeach-beforeall)
|
||||
- [JUnit 5 @Test Annotation](http://www.baeldung.com/junit-5-test-annotation)
|
||||
- [Migrating from JUnit 4 to JUnit 5](http://www.baeldung.com/junit-5-migration)
|
||||
- [Assert an Exception is Thrown in JUnit 4 and 5](http://www.baeldung.com/junit-assert-exception)
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.junit5;
|
||||
|
||||
public class Greetings {
|
||||
|
||||
public static String sayHello() {
|
||||
return "Hello";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.junit5.bean;
|
||||
|
||||
/**
|
||||
* Bean that contains utility methods to work with numbers.
|
||||
*
|
||||
* @author Donato Rimenti
|
||||
*
|
||||
*/
|
||||
public class NumbersBean {
|
||||
|
||||
/**
|
||||
* Returns true if a number is even, false otherwise.
|
||||
*
|
||||
* @param number
|
||||
* the number to check
|
||||
* @return true if the argument is even, false otherwise
|
||||
*/
|
||||
public boolean isNumberEven(int number) {
|
||||
return number % 2 == 0;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.baeldung;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.util.ConcurrentModificationException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.function.Executable;
|
||||
|
||||
public class ExceptionUnitTest {
|
||||
|
||||
@Test
|
||||
void shouldThrowException() {
|
||||
Throwable exception = assertThrows(UnsupportedOperationException.class, () -> {
|
||||
throw new UnsupportedOperationException("Not supported");
|
||||
});
|
||||
assertEquals(exception.getMessage(), "Not supported");
|
||||
}
|
||||
|
||||
@Test
|
||||
void assertThrowsException() {
|
||||
String str = null;
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
Integer.valueOf(str);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenModifyMapDuringIteration_thenThrowExecption() {
|
||||
Map<Integer, String> hashmap = new HashMap<>();
|
||||
hashmap.put(1, "One");
|
||||
hashmap.put(2, "Two");
|
||||
|
||||
Executable executable = () -> hashmap.forEach((key, value) -> hashmap.remove(1));
|
||||
assertThrows(ConcurrentModificationException.class, executable);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.baeldung;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class FirstUnitTest {
|
||||
|
||||
@Test
|
||||
void lambdaExpressions() {
|
||||
List<Integer> numbers = Arrays.asList(1, 2, 3);
|
||||
assertTrue(numbers.stream()
|
||||
.mapToInt(i -> i)
|
||||
.sum() > 5, "Sum should be greater than 5");
|
||||
}
|
||||
|
||||
@Disabled("test to show MultipleFailuresError")
|
||||
@Test
|
||||
void groupAssertions() {
|
||||
int[] numbers = { 0, 1, 2, 3, 4 };
|
||||
assertAll("numbers", () -> assertEquals(numbers[0], 1), () -> assertEquals(numbers[3], 3), () -> assertEquals(numbers[4], 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
void disabledTest() {
|
||||
assertTrue(false);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.platform.runner.JUnitPlatform;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import com.baeldung.junit5.Greetings;
|
||||
|
||||
@RunWith(JUnitPlatform.class)
|
||||
public class GreetingsUnitTest {
|
||||
|
||||
@Test
|
||||
void whenCallingSayHello_thenReturnHello() {
|
||||
assertTrue("Hello".equals(Greetings.sayHello()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.baeldung;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
//@RunWith(JUnitPlatform.class)
|
||||
public class JUnit5NewFeaturesUnitTest {
|
||||
|
||||
private static final Logger log = Logger.getLogger(JUnit5NewFeaturesUnitTest.class.getName());
|
||||
|
||||
@BeforeAll
|
||||
static void setup() {
|
||||
log.info("@BeforeAll - executes once before all test methods in this class");
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void init() {
|
||||
log.info("@BeforeEach - executes before each test method in this class");
|
||||
}
|
||||
|
||||
@DisplayName("Single test successful")
|
||||
@Test
|
||||
void testSingleSuccessTest() {
|
||||
log.info("Success");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled("Not implemented yet.")
|
||||
void testShowSomething() {
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
log.info("@AfterEach - executed after each test method.");
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void done() {
|
||||
log.info("@AfterAll - executed after all test methods.");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.baeldung;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.jupiter.api.DynamicTest;
|
||||
import org.junit.jupiter.api.TestFactory;
|
||||
|
||||
public class LiveTest {
|
||||
|
||||
private List<String> in = new ArrayList<>(Arrays.asList("Hello", "Yes", "No"));
|
||||
private List<String> out = new ArrayList<>(Arrays.asList("Cześć", "Tak", "Nie"));
|
||||
|
||||
@TestFactory
|
||||
public Stream<DynamicTest> translateDynamicTestsFromStream() {
|
||||
|
||||
return in.stream()
|
||||
.map(word -> DynamicTest.dynamicTest("Test translate " + word, () -> {
|
||||
int id = in.indexOf(word);
|
||||
assertEquals(out.get(id), translate(word));
|
||||
}));
|
||||
}
|
||||
|
||||
private String translate(String word) {
|
||||
if ("Hello".equalsIgnoreCase(word)) {
|
||||
return "Cześć";
|
||||
} else if ("Yes".equalsIgnoreCase(word)) {
|
||||
return "Tak";
|
||||
} else if ("No".equalsIgnoreCase(word)) {
|
||||
return "Nie";
|
||||
}
|
||||
return "Error";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.exception;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class ExceptionAssertionUnitTest {
|
||||
@Test
|
||||
public void whenExceptionThrown_thenAssertionSucceeds() {
|
||||
String test = null;
|
||||
assertThrows(NullPointerException.class, () -> {
|
||||
test.length();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDerivedExceptionThrown_thenAssertionSucceds() {
|
||||
String test = null;
|
||||
assertThrows(RuntimeException.class, () -> {
|
||||
test.length();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.baeldung.junit5.bean.test;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.baeldung.junit5.bean.NumbersBean;
|
||||
|
||||
/**
|
||||
* Test class for {@link NumbersBean}.
|
||||
*
|
||||
* @author Donato Rimenti
|
||||
*
|
||||
*/
|
||||
public class NumbersBeanUnitTest {
|
||||
|
||||
/**
|
||||
* The bean to test.
|
||||
*/
|
||||
private NumbersBean bean = new NumbersBean();
|
||||
|
||||
/**
|
||||
* Tests that when an even number is passed to
|
||||
* {@link NumbersBean#isNumberEven(int)}, true is returned.
|
||||
*/
|
||||
@Test
|
||||
void givenEvenNumber_whenCheckingIsNumberEven_thenTrue() {
|
||||
boolean result = bean.isNumberEven(8);
|
||||
|
||||
Assertions.assertTrue(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that when an odd number is passed to
|
||||
* {@link NumbersBean#isNumberEven(int)}, false is returned.
|
||||
*/
|
||||
@Test
|
||||
void givenOddNumber_whenCheckingIsNumberEven_thenFalse() {
|
||||
boolean result = bean.isNumberEven(3);
|
||||
|
||||
Assertions.assertFalse(result);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.junit5.spring;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import com.baeldung.junit5.Greetings;
|
||||
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@ContextConfiguration(classes = { SpringTestConfiguration.class })
|
||||
public class GreetingsSpringUnitTest {
|
||||
|
||||
@Test
|
||||
void whenCallingSayHello_thenReturnHello() {
|
||||
assertTrue("Hello".equals(Greetings.sayHello()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.junit5.spring;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class SpringTestConfiguration {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.migration.junit4;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.junit.experimental.categories.Category;
|
||||
|
||||
import com.baeldung.migration.junit4.categories.Annotations;
|
||||
import com.baeldung.migration.junit4.categories.JUnit4UnitTest;
|
||||
|
||||
@Category(value = { Annotations.class, JUnit4UnitTest.class })
|
||||
public class AnnotationTestExampleUnitTest {
|
||||
@Test(expected = Exception.class)
|
||||
public void shouldRaiseAnException() throws Exception {
|
||||
throw new Exception("This is my expected exception");
|
||||
}
|
||||
|
||||
@Test(timeout = 1)
|
||||
@Ignore
|
||||
public void shouldFailBecauseTimeout() throws InterruptedException {
|
||||
Thread.sleep(10);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.migration.junit4;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
public class AssertionsExampleUnitTest {
|
||||
@Test
|
||||
@Ignore
|
||||
public void shouldFailBecauseTheNumbersAreNotEqualld() {
|
||||
assertEquals("Numbers are not equal!", 2, 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldAssertAllTheGroup() {
|
||||
List<Integer> list = Arrays.asList(1, 2, 3);
|
||||
assertEquals("List is not incremental", list.get(0)
|
||||
.intValue(), 1);
|
||||
assertEquals("List is not incremental", list.get(1)
|
||||
.intValue(), 2);
|
||||
assertEquals("List is not incremental", list.get(2)
|
||||
.intValue(), 3);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.baeldung.migration.junit4;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@RunWith(JUnit4.class)
|
||||
public class BeforeAndAfterAnnotationsUnitTest {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(BeforeAndAfterAnnotationsUnitTest.class);
|
||||
|
||||
private List<String> list;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
LOG.info("startup");
|
||||
list = new ArrayList<>(Arrays.asList("test1", "test2"));
|
||||
}
|
||||
|
||||
@After
|
||||
public void finalize() {
|
||||
LOG.info("finalize");
|
||||
list.clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingListSize_ThenSizeEqualsToInit() {
|
||||
LOG.info("executing test");
|
||||
assertEquals(2, list.size());
|
||||
|
||||
list.add("another test");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingListSizeAgain_ThenSizeEqualsToInit() {
|
||||
LOG.info("executing another test");
|
||||
assertEquals(2, list.size());
|
||||
|
||||
list.add("yet another test");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.baeldung.migration.junit4;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@RunWith(JUnit4.class)
|
||||
public class BeforeClassAndAfterClassAnnotationsUnitTest {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(BeforeClassAndAfterClassAnnotationsUnitTest.class);
|
||||
|
||||
@BeforeClass
|
||||
public static void setup() {
|
||||
LOG.info("startup - creating DB connection");
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void tearDown() {
|
||||
LOG.info("closing DB connection");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void simpleTest() {
|
||||
LOG.info("simple test");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void anotherSimpleTest() {
|
||||
LOG.info("another simple test");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.migration.junit4;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
public class ExceptionAssertionUnitTest {
|
||||
|
||||
@Rule
|
||||
public ExpectedException exceptionRule = ExpectedException.none();
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void whenExceptionThrown_thenExpectationSatisfied() {
|
||||
String test = null;
|
||||
test.length();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenExceptionThrown_thenRuleIsApplied() {
|
||||
exceptionRule.expect(NumberFormatException.class);
|
||||
exceptionRule.expectMessage("For input string");
|
||||
Integer.parseInt("1a");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.migration.junit4;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.migration.junit4.rules.TraceUnitTestRule;
|
||||
|
||||
public class RuleExampleUnitTest {
|
||||
|
||||
@Rule
|
||||
public final TraceUnitTestRule traceRuleTests = new TraceUnitTestRule();
|
||||
|
||||
@Test
|
||||
public void whenTracingTests() {
|
||||
System.out.println("This is my test");
|
||||
/*...*/
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.migration.junit4.categories;
|
||||
|
||||
public interface Annotations {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.migration.junit4.categories;
|
||||
|
||||
public interface JUnit4UnitTest {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.baeldung.migration.junit4.rules;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.rules.TestRule;
|
||||
import org.junit.runner.Description;
|
||||
import org.junit.runners.model.MultipleFailureException;
|
||||
import org.junit.runners.model.Statement;
|
||||
|
||||
public class TraceUnitTestRule implements TestRule {
|
||||
|
||||
@Override
|
||||
public Statement apply(Statement base, Description description) {
|
||||
|
||||
return new Statement() {
|
||||
@Override
|
||||
public void evaluate() throws Throwable {
|
||||
List<Throwable> errors = new ArrayList<Throwable>();
|
||||
|
||||
System.out.println("Starting test ... " + description.getMethodName());
|
||||
try {
|
||||
base.evaluate();
|
||||
} catch (Throwable e) {
|
||||
errors.add(e);
|
||||
} finally {
|
||||
System.out.println("... test finished. " + description.getMethodName());
|
||||
}
|
||||
|
||||
MultipleFailureException.assertEmpty(errors);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.migration.junit5;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Tag;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.platform.runner.JUnitPlatform;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
@Tag("annotations")
|
||||
@Tag("junit5")
|
||||
@RunWith(JUnitPlatform.class)
|
||||
public class AnnotationTestExampleUnitTest {
|
||||
@Test
|
||||
public void shouldRaiseAnException() throws Exception {
|
||||
Assertions.assertThrows(Exception.class, () -> {
|
||||
throw new Exception("This is my expected exception");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void shouldFailBecauseTimeout() throws InterruptedException {
|
||||
Assertions.assertTimeout(Duration.ofMillis(1), () -> Thread.sleep(10));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.baeldung.migration.junit5;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.platform.runner.JUnitPlatform;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
@RunWith(JUnitPlatform.class)
|
||||
public class AssertionsExampleUnitTest {
|
||||
@Test
|
||||
@Disabled
|
||||
public void shouldFailBecauseTheNumbersAreNotEqual() {
|
||||
Assertions.assertEquals(2, 3, "Numbers are not equal!");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void shouldFailBecauseItsNotTrue_overloading() {
|
||||
Assertions.assertTrue(() -> {
|
||||
return false;
|
||||
}, () -> "It's not true!");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldAssertAllTheGroup() {
|
||||
List<Integer> list = Arrays.asList(1, 2, 3);
|
||||
|
||||
Assertions.assertAll("List is not incremental", () -> Assertions.assertEquals(list.get(0)
|
||||
.intValue(), 1), () -> Assertions.assertEquals(
|
||||
list.get(1)
|
||||
.intValue(),
|
||||
2),
|
||||
() -> Assertions.assertEquals(list.get(2)
|
||||
.intValue(), 3));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.migration.junit5;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assumptions.assumeFalse;
|
||||
import static org.junit.jupiter.api.Assumptions.assumeTrue;
|
||||
import static org.junit.jupiter.api.Assumptions.assumingThat;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class AssumptionUnitTest {
|
||||
|
||||
@Test
|
||||
public void trueAssumption() {
|
||||
assumeTrue(5 > 1, () -> "5 is greater the 1");
|
||||
assertEquals(5 + 2, 7);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void falseAssumption() {
|
||||
assumeFalse(5 < 1, () -> "5 is less then 1");
|
||||
assertEquals(5 + 2, 7);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void assumptionThat() {
|
||||
String someString = "Just a string";
|
||||
assumingThat(someString.equals("Just a string"), () -> assertEquals(2 + 2, 4));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.baeldung.migration.junit5;
|
||||
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.platform.runner.JUnitPlatform;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@RunWith(JUnitPlatform.class)
|
||||
public class BeforeAllAndAfterAllAnnotationsUnitTest {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(BeforeAllAndAfterAllAnnotationsUnitTest.class);
|
||||
|
||||
@BeforeAll
|
||||
public static void setup() {
|
||||
LOG.info("startup - creating DB connection");
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
public static void tearDown() {
|
||||
LOG.info("closing DB connection");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void simpleTest() {
|
||||
LOG.info("simple test");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void anotherSimpleTest() {
|
||||
LOG.info("another simple test");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.baeldung.migration.junit5;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.platform.runner.JUnitPlatform;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@RunWith(JUnitPlatform.class)
|
||||
public class BeforeEachAndAfterEachAnnotationsUnitTest {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(BeforeEachAndAfterEachAnnotationsUnitTest.class);
|
||||
|
||||
private List<String> list;
|
||||
|
||||
@BeforeEach
|
||||
public void init() {
|
||||
LOG.info("startup");
|
||||
list = new ArrayList<>(Arrays.asList("test1", "test2"));
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void finalize() {
|
||||
LOG.info("finalize");
|
||||
list.clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingListSize_ThenSizeEqualsToInit() {
|
||||
LOG.info("executing test");
|
||||
assertEquals(2, list.size());
|
||||
|
||||
list.add("another test");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingListSizeAgain_ThenSizeEqualsToInit() {
|
||||
LOG.info("executing another test");
|
||||
assertEquals(2, list.size());
|
||||
|
||||
list.add("yet another test");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.migration.junit5;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.junit.platform.runner.JUnitPlatform;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import com.baeldung.migration.junit5.extensions.TraceUnitExtension;
|
||||
|
||||
@RunWith(JUnitPlatform.class)
|
||||
@ExtendWith(TraceUnitExtension.class)
|
||||
public class RuleExampleUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenTracingTests() {
|
||||
System.out.println("This is my test");
|
||||
/*...*/
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.migration.junit5.extensions;
|
||||
|
||||
import org.junit.jupiter.api.extension.AfterEachCallback;
|
||||
import org.junit.jupiter.api.extension.BeforeEachCallback;
|
||||
import org.junit.jupiter.api.extension.ExtensionContext;
|
||||
|
||||
public class TraceUnitExtension implements AfterEachCallback, BeforeEachCallback {
|
||||
|
||||
@Override
|
||||
public void beforeEach(ExtensionContext context) throws Exception {
|
||||
System.out.println("Starting test ... " + context.getDisplayName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterEach(ExtensionContext context) throws Exception {
|
||||
System.out.println("... test finished. " + context.getDisplayName());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -17,7 +17,7 @@ public class ReadResourceDirectoryUnitTest {
|
||||
String absolutePath = file.getAbsolutePath();
|
||||
|
||||
System.out.println(absolutePath);
|
||||
Assert.assertTrue(absolutePath.endsWith("src/test/resources"));
|
||||
Assert.assertTrue(absolutePath.endsWith("src" + File.separator + "test" + File.separator + "resources"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -27,7 +27,7 @@ public class ReadResourceDirectoryUnitTest {
|
||||
String absolutePath = resourceDirectory.toFile().getAbsolutePath();
|
||||
|
||||
System.out.println(absolutePath);
|
||||
Assert.assertTrue(absolutePath.endsWith("src/test/resources"));
|
||||
Assert.assertTrue(absolutePath.endsWith("src" + File.separator + "test" + File.separator + "resources"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -39,7 +39,7 @@ public class ReadResourceDirectoryUnitTest {
|
||||
String absolutePath = file.getAbsolutePath();
|
||||
|
||||
System.out.println(absolutePath);
|
||||
Assert.assertTrue(absolutePath.endsWith("/example_resource.txt"));
|
||||
Assert.assertTrue(absolutePath.endsWith(File.separator + "example_resource.txt"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.suites;
|
||||
|
||||
import org.junit.platform.runner.JUnitPlatform;
|
||||
import org.junit.platform.suite.api.SelectPackages;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
@RunWith(JUnitPlatform.class)
|
||||
@SelectPackages("com.baeldung")
|
||||
// @SelectClasses({AssertionTest.class, AssumptionTest.class, ExceptionTest.class})
|
||||
public class AllUnitTest {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user