diff --git a/testing-modules/junit-5-advanced/pom.xml b/testing-modules/junit-5-advanced/pom.xml index f65f7e2a2f..bc879ebfc3 100644 --- a/testing-modules/junit-5-advanced/pom.xml +++ b/testing-modules/junit-5-advanced/pom.xml @@ -1,32 +1,52 @@ - 4.0.0 - junit-5-advanced - 1.0-SNAPSHOT - junit-5-advanced - Advanced JUnit 5 Topics + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + junit-5-advanced + 1.0-SNAPSHOT + junit-5-advanced + Advanced JUnit 5 Topics - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - ../../ - + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + ../../ + - - - org.junit.jupiter - junit-jupiter - ${junit-jupiter.version} - test - - - - - 5.4.2 - 2.21.0 - + + + org.junit.jupiter + junit-jupiter + ${junit-jupiter.version} + test + + + org.junit.jupiter + junit-jupiter-engine + ${junit-jupiter.version} + + + org.junit.vintage + junit-vintage-engine + ${junit.vintage.version} + test + + + + + + + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + + + 5.4.2 + 2.21.0 + 5.4.2 + diff --git a/testing-modules/junit-5-advanced/src/main/java/com/baeldung/junit5/testinstance/Tweet.java b/testing-modules/junit-5-advanced/src/main/java/com/baeldung/junit5/testinstance/Tweet.java new file mode 100644 index 0000000000..dac023697a --- /dev/null +++ b/testing-modules/junit-5-advanced/src/main/java/com/baeldung/junit5/testinstance/Tweet.java @@ -0,0 +1,27 @@ +package com.baeldung.junit5.testinstance; + +import java.io.Serializable; + +public class Tweet implements Serializable { + + private static final long serialVersionUID = 1L; + private String id; + private String content; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + +} diff --git a/testing-modules/junit-5-advanced/src/main/java/com/baeldung/junit5/testinstance/TweetException.java b/testing-modules/junit-5-advanced/src/main/java/com/baeldung/junit5/testinstance/TweetException.java new file mode 100644 index 0000000000..920f996b59 --- /dev/null +++ b/testing-modules/junit-5-advanced/src/main/java/com/baeldung/junit5/testinstance/TweetException.java @@ -0,0 +1,10 @@ +package com.baeldung.junit5.testinstance; + +public class TweetException extends RuntimeException { + + private static final long serialVersionUID = 1L; + + public TweetException(String message) { + super(message); + } +} diff --git a/testing-modules/junit-5-advanced/src/main/java/com/baeldung/junit5/testinstance/TweetSerializer.java b/testing-modules/junit-5-advanced/src/main/java/com/baeldung/junit5/testinstance/TweetSerializer.java new file mode 100644 index 0000000000..734c06b9f5 --- /dev/null +++ b/testing-modules/junit-5-advanced/src/main/java/com/baeldung/junit5/testinstance/TweetSerializer.java @@ -0,0 +1,41 @@ +package com.baeldung.junit5.testinstance; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectOutputStream; + +public class TweetSerializer { + + private static final int MAX_TWEET_SIZE = 1000; + private static final int MIN_TWEET_SIZE = 150; + private final Tweet tweet; + + public TweetSerializer(Tweet tweet) { + this.tweet = tweet; + } + + public byte[] serialize() throws IOException { + byte[] tweetContent = serializeTweet(); + int totalLength = tweetContent.length; + validateSizeOfTweet(totalLength); + return tweetContent; + } + + private void validateSizeOfTweet(int tweetSize) { + if (tweetSize > MAX_TWEET_SIZE) { + throw new TweetException("Tweet is too large"); + } + if (tweetSize < MIN_TWEET_SIZE) { + throw new TweetException("Tweet is too small"); + } + } + + private byte[] serializeTweet() throws IOException { + try (ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream(); + ObjectOutputStream objectStream = new ObjectOutputStream(arrayOutputStream)) { + objectStream.writeObject(tweet); + return arrayOutputStream.toByteArray(); + } + } + +} diff --git a/testing-modules/junit-5-advanced/src/test/java/com/baeldung/junit5/testinstance/AdditionUnitTest.java b/testing-modules/junit-5-advanced/src/test/java/com/baeldung/junit5/testinstance/AdditionUnitTest.java new file mode 100644 index 0000000000..430082dc28 --- /dev/null +++ b/testing-modules/junit-5-advanced/src/test/java/com/baeldung/junit5/testinstance/AdditionUnitTest.java @@ -0,0 +1,22 @@ +package com.baeldung.junit5.testinstance; + +import static org.junit.Assert.assertEquals; + +import org.junit.jupiter.api.Test; + +class AdditionUnitTest { + + private int sum = 1; + + @Test + void addingTwoToSumReturnsThree() { + sum += 2; + assertEquals(3, sum); + } + + @Test + void addingThreeToSumReturnsFour() { + sum += 3; + assertEquals(4, sum); + } +} diff --git a/testing-modules/junit-5-advanced/src/test/java/com/baeldung/junit5/testinstance/OrderUnitTest.java b/testing-modules/junit-5-advanced/src/test/java/com/baeldung/junit5/testinstance/OrderUnitTest.java new file mode 100644 index 0000000000..5907dd5654 --- /dev/null +++ b/testing-modules/junit-5-advanced/src/test/java/com/baeldung/junit5/testinstance/OrderUnitTest.java @@ -0,0 +1,38 @@ +package com.baeldung.junit5.testinstance; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.api.TestInstance.Lifecycle; +import org.junit.jupiter.api.TestMethodOrder; + +@TestMethodOrder(OrderAnnotation.class) +@TestInstance(Lifecycle.PER_CLASS) +class OrderUnitTest { + + private int sum; + + @BeforeAll + void init() { + sum = 1; + } + + @Test + @Order(1) + void firstTest() { + sum += 2; + assertEquals(3, sum); + } + + @Test + @Order(2) + void secondTest() { + sum += 3; + assertEquals(6, sum); + } + +} diff --git a/testing-modules/junit-5-advanced/src/test/java/com/baeldung/junit5/testinstance/TweetSerializerJUnit4UnitTest.java b/testing-modules/junit-5-advanced/src/test/java/com/baeldung/junit5/testinstance/TweetSerializerJUnit4UnitTest.java new file mode 100644 index 0000000000..e8c4dae77d --- /dev/null +++ b/testing-modules/junit-5-advanced/src/test/java/com/baeldung/junit5/testinstance/TweetSerializerJUnit4UnitTest.java @@ -0,0 +1,62 @@ +package com.baeldung.junit5.testinstance; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.junit.Assert.assertThat; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; + +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +public class TweetSerializerJUnit4UnitTest { + + private static String largeContent; + private static String content; + private static String smallContent; + + private static Tweet tweet; + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + @BeforeClass + public static void setUpFixture() throws IOException { + content = "Lorem ipsum dolor sit amet, consectetur adipiscing elit"; + smallContent = "Lorem ipsum dolor"; + largeContent = new String(Files.readAllBytes(Paths.get("src/test/resources/lorem-ipsum.txt"))); + tweet = new Tweet(); + tweet.setId("AX1346"); + } + + @Test + public void serializerThrowsExceptionWhenMessageIsTooLarge() throws IOException { + tweet.setContent(largeContent); + + expectedException.expect(TweetException.class); + expectedException.expectMessage("Tweet is too large"); + + new TweetSerializer(tweet).serialize(); + } + + @Test + public void serializerThrowsExceptionWhenMessageIsTooSmall() throws IOException { + tweet.setContent(smallContent); + + expectedException.expect(TweetException.class); + expectedException.expectMessage("Tweet is too small"); + + new TweetSerializer(tweet).serialize(); + } + + @Test + public void serializeTweet() throws IOException { + tweet.setContent(content); + byte[] content = new TweetSerializer(tweet).serialize(); + assertThat(content, is(notNullValue())); + } +} diff --git a/testing-modules/junit-5-advanced/src/test/java/com/baeldung/junit5/testinstance/TweetSerializerUnitTest.java b/testing-modules/junit-5-advanced/src/test/java/com/baeldung/junit5/testinstance/TweetSerializerUnitTest.java new file mode 100644 index 0000000000..ddbea8b052 --- /dev/null +++ b/testing-modules/junit-5-advanced/src/test/java/com/baeldung/junit5/testinstance/TweetSerializerUnitTest.java @@ -0,0 +1,59 @@ +package com.baeldung.junit5.testinstance; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.junit.Assert.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.api.TestInstance.Lifecycle; + +@TestInstance(Lifecycle.PER_CLASS) +class TweetSerializerUnitTest { + + private String largeContent; + private String content; + private String smallContent; + + private Tweet tweet; + + @BeforeAll + void setUpFixture() throws IOException { + content = "Lorem ipsum dolor sit amet, consectetur adipiscing elit"; + smallContent = "Lorem ipsum dolor"; + largeContent = new String(Files.readAllBytes(Paths.get("src/test/resources/lorem-ipsum.txt"))); + tweet = new Tweet(); + tweet.setId("AX1346"); + } + + @Test + void serializerThrowsExceptionWhenMessageIsTooLarge() throws IOException { + tweet.setContent(largeContent); + + TweetException tweetException = assertThrows(TweetException.class, () -> new TweetSerializer(tweet).serialize()); + assertThat(tweetException.getMessage(), is(equalTo("Tweet is too large"))); + } + + @Test + void serializerThrowsExceptionWhenMessageIsTooSmall() throws IOException { + tweet.setContent(smallContent); + + TweetException tweetException = assertThrows(TweetException.class, () -> new TweetSerializer(tweet).serialize()); + assertThat(tweetException.getMessage(), is(equalTo("Tweet is too small"))); + } + + @Test + void serializeTweet() throws IOException { + tweet.setContent(content); + byte[] content = new TweetSerializer(tweet).serialize(); + assertThat(content, is(notNullValue())); + } + +} diff --git a/testing-modules/junit-5-advanced/src/test/resources/lorem-ipsum.txt b/testing-modules/junit-5-advanced/src/test/resources/lorem-ipsum.txt new file mode 100644 index 0000000000..2e9db81938 --- /dev/null +++ b/testing-modules/junit-5-advanced/src/test/resources/lorem-ipsum.txt @@ -0,0 +1,4 @@ +Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. +Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. +Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. +Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. \ No newline at end of file