BAEL-6198: junit5 @Nested tests

This commit is contained in:
emanueltrandafir1993 2023-02-04 16:40:35 +01:00
parent ba8f9a6437
commit fecb0722ee
6 changed files with 220 additions and 0 deletions

View File

@ -0,0 +1,19 @@
package com.baeldung.junit5.nested;
public class Article {
private String name;
private Membership articleLevel;
public Article(String name, Membership articleLevel) {
this.name = name;
this.articleLevel = articleLevel;
}
public String getName() {
return name;
}
public Membership getArticleLevel() {
return articleLevel;
}
}

View File

@ -0,0 +1,16 @@
package com.baeldung.junit5.nested;
public enum Membership {
FREE(0), SILVER(10), GOLD(20);
private final int level;
Membership(int level) {
this.level = level;
}
public int compare(Membership other) {
return level - other.level;
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.junit5.nested;
import java.util.List;
import java.util.stream.Collectors;
public class Publication {
private final List<Article> articles;
public Publication(List<Article> articles) {
this.articles = articles;
}
public List<String> getReadableArticles(User user) {
return articles.stream()
.filter(a -> a.getArticleLevel()
.compare(user.getMembership()) <= 0)
.map(Article::getName)
.collect(Collectors.toList());
}
public List<String> getLockedArticles(User user) {
return articles.stream()
.filter(a -> a.getArticleLevel()
.compare(user.getMembership()) > 0)
.map(Article::getName)
.collect(Collectors.toList());
}
public List<Article> getArticles() {
return articles;
}
}

View File

@ -0,0 +1,19 @@
package com.baeldung.junit5.nested;
public class User {
private String name;
private Membership membership;
public User(String name, Membership membership) {
this.name = name;
this.membership = membership;
}
public String getName() {
return name;
}
public Membership getMembership() {
return membership;
}
}

View File

@ -0,0 +1,41 @@
package com.baeldung.junit5.nested;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
public class NestedTest {
@BeforeEach
void beforeEach() {
System.out.println("NestedTest.beforeEach()");
}
@Nested
class FirstNestedClass {
@BeforeEach
void beforeEach() {
System.out.println("FirstNestedClass.beforeEach()");
}
@Test
void test() {
System.out.println("FirstNestedClass.test()");
}
}
@Nested
class SecondNestedClass {
@BeforeEach
void beforeEach() {
System.out.println("SecondNestedClass.beforeEach()");
}
@Test
void test() {
System.out.println("SecondNestedClass.test()");
}
}
}

View File

@ -0,0 +1,93 @@
package com.baeldung.junit5.nested;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
@DisplayName("given a article publication with three articles")
class OnlinePublicationTest {
private Publication publication;
@BeforeEach
void setupArticlesAndPublication() {
Article freeArticle = new Article("free article", Membership.FREE);
Article silverArticle = new Article("silver level article", Membership.SILVER);
Article goldArticle = new Article("gold level article", Membership.GOLD);
publication = new Publication(Arrays.asList(freeArticle, silverArticle, goldArticle));
}
@Test
@DisplayName("then 3 articles are available")
void shouldHaveThreeArticlesInTotal() {
List<Article> allArticles = publication.getArticles();
assertThat(allArticles).hasSize(3);
}
@Nested
@DisplayName("when a user with a 'free' membership logs in")
class UserWithAFreeMembership {
User freeFreya = new User("Freya", Membership.FREE);
@Test
@DisplayName("then he should be able to read the 'free' articles")
void shouldOnlyReadFreeArticles() {
List<String> articles = publication.getReadableArticles(freeFreya);
assertThat(articles).containsExactly("free article");
}
@Test
@DisplayName("then he shouldn't be able to read the 'silver' and 'gold' articles")
void shouldSeeSilverAndGoldLevelArticlesAsLocked() {
List<String> articles = publication.getLockedArticles(freeFreya);
assertThat(articles).containsExactlyInAnyOrder("silver level article", "gold level article");
}
}
@Nested
@DisplayName("when a user with a 'silver' membership logs in")
class UserWithSilverMembership {
User silverSilvester = new User("Silvester", Membership.SILVER);
@Test
@DisplayName("then he should be able to read the 'free' and 'silver' level articles")
void shouldOnlyReadFreeAndSilverLevelArticles() {
List<String> articles = publication.getReadableArticles(silverSilvester);
assertThat(articles).containsExactlyInAnyOrder("free article", "silver level article");
}
@Test
@DisplayName("then he should see the 'gold' level articles as locked")
void shouldSeeGoldLevelArticlesAsLocked() {
List<String> articles = publication.getLockedArticles(silverSilvester);
assertThat(articles).containsExactlyInAnyOrder("gold level article");
}
}
@Nested
@DisplayName("when a user with a 'gold' membership logs in")
class UserWithGoldMembership {
User goldenGeorge = new User("George", Membership.GOLD);
@Test
@DisplayName("then he should be able to read all the articles")
void shouldSeeAllArticles() {
List<String> articles = publication.getReadableArticles(goldenGeorge);
assertThat(articles).containsExactlyInAnyOrder("free article", "silver level article", "gold level article");
}
@Test
@DisplayName("then he should not see any article as locked")
void shouldNotHaveHiddenArticles() {
List<String> articles = publication.getLockedArticles(goldenGeorge);
assertThat(articles).isEmpty();
}
}
}