Guest User

Untitled

a guest
Feb 15th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. package com.rbezzi.playground.junit5;
  2.  
  3. import com.google.common.base.CaseFormat;
  4. import org.junit.jupiter.api.BeforeEach;
  5. import org.junit.jupiter.api.DisplayName;
  6. import org.junit.jupiter.api.DisplayNameGeneration;
  7. import org.junit.jupiter.api.DisplayNameGenerator;
  8. import org.junit.jupiter.api.Nested;
  9. import org.junit.jupiter.api.Test;
  10.  
  11. import java.lang.reflect.Method;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14.  
  15. import static org.junit.jupiter.api.Assertions.assertEquals;
  16.  
  17. @DisplayName("A list")
  18. @DisplayNameGeneration(NestedJunit5Tests.CamelToSpaces.class)
  19. public class NestedJunit5Tests {
  20.  
  21. private List<Integer> l = new ArrayList<>();
  22.  
  23. @Test
  24. void whenIsEmptyHasSizeZero() {
  25. assertEquals(0, l.size());
  26. }
  27.  
  28. @Nested
  29. class WhenHasOneElement {
  30.  
  31. @BeforeEach
  32. void addOneElement() {
  33. l.add(100);
  34. }
  35.  
  36. @Test
  37. void hasSize1() {
  38. assertEquals(1, l.size());
  39. }
  40.  
  41. @Test
  42. void canGetElement0() {
  43. assertEquals(100, l.get(0));
  44. }
  45. }
  46.  
  47. @Nested
  48. class WhenHasTwoElements {
  49.  
  50. @BeforeEach
  51. void addTwoElements() {
  52. l.add(10);
  53. l.add(20);
  54. }
  55.  
  56. @Test
  57. void hasSize2() {
  58. assertEquals(2, l.size());
  59. }
  60.  
  61. @Test
  62. void getElement0and1() {
  63. assertEquals(10, l.get(0));
  64. assertEquals(20, l.get(1));
  65. }
  66.  
  67. }
  68.  
  69. static class CamelToSpaces extends DisplayNameGenerator.Standard {
  70.  
  71. @Override
  72. public String generateDisplayNameForClass(Class<?> testClass) {
  73. return toSpaces(super.generateDisplayNameForClass(testClass));
  74. }
  75.  
  76. @Override
  77. public String generateDisplayNameForNestedClass(Class<?> nestedClass) {
  78. return toSpaces(super.generateDisplayNameForNestedClass(nestedClass));
  79. }
  80.  
  81. @Override
  82. public String generateDisplayNameForMethod(Class<?> testClass, Method testMethod) {
  83. return toSpaces(super.generateDisplayNameForMethod(testClass, testMethod));
  84. }
  85.  
  86. private String toSpaces(String s) {
  87. return CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, s)
  88. .replaceAll("-", " ")
  89. .replaceAll("(\\D)(\\d)", "$1 $2")
  90. .replaceFirst("\\(\\)$", "");
  91. }
  92. }
  93.  
  94. @Test
  95. void one() {
  96. assertEquals("has size 1 and", new CamelToSpaces().toSpaces("hasSize1And()"));
  97. }
  98.  
  99. @Test
  100. void two() {
  101. assertEquals("get element 0 and 1", new CamelToSpaces().toSpaces("getElement0And1()"));
  102. }
  103. }
Add Comment
Please, Sign In to add comment