BAEL-3040: @TestInstance annotation in JUnit 5 (#7247)
* BAEL-3040: @TestInstance annotation in JUnit 5 * BAEL-3040: @TestInstance annotation in JUnit 5 (renamed the tests) * BAEL-3040: @TestInstance annotation in JUnit 5 (corrected the name and removed unnecessary code) * BAEL-3040: @TestInstance annotation in JUnit 5 - renamed the files of test classes * BAEL-3040: @TestInstance annotation in JUnit 5 (changed based on code review comments) * BAEL-3040: @TestInstance annotation in JUnit 5 (added the class for Tweet Serializer using JUnit 4) * BAEL-3040: @TestInstance annotation in JUnit 5 (Code review changes) * BAEL-3040: @TestInstance annotation in JUnit 5 (missed a code review comment, made the changes for the same) * BAEL-3040: @TestInstance annotation in JUnit 5 (Moved the code to the advanced JUnit 5 directory) * BAEL-3040: Formatted pom.xml * BAEL-3040: Removed the files from junit5 directory
This commit is contained in:
parent
c08db10def
commit
8e1d751f68
|
@ -1,32 +1,52 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>junit-5-advanced</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>junit-5-advanced</name>
|
||||
<description>Advanced JUnit 5 Topics</description>
|
||||
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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>junit-5-advanced</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>junit-5-advanced</name>
|
||||
<description>Advanced JUnit 5 Topics</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
</parent>
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<junit-jupiter.version>5.4.2</junit-jupiter.version>
|
||||
<maven-surefire-plugin.version>2.21.0</maven-surefire-plugin.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
<version>${junit.vintage.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<properties>
|
||||
<junit-jupiter.version>5.4.2</junit-jupiter.version>
|
||||
<maven-surefire-plugin.version>2.21.0</maven-surefire-plugin.version>
|
||||
<junit.vintage.version>5.4.2</junit.vintage.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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()));
|
||||
}
|
||||
}
|
|
@ -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()));
|
||||
}
|
||||
|
||||
}
|
|
@ -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.
|
Loading…
Reference in New Issue