├── .gitignore ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src ├── main └── java │ └── com │ └── johngrib │ └── example │ ├── Application.java │ ├── math │ └── ComplexNumber.java │ └── member │ ├── Member.java │ └── MemberRepository.java └── test └── java └── com └── johngrib └── example ├── math ├── ComplexNumberKoTest.java └── ComplexNumberTest.java └── member ├── MemberRepositoryPassTest.java ├── MemberRepositoryPassWithExtendsTest.java └── MemberRepositoryTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | .gradle 3 | build/ 4 | !gradle/wrapper/gradle-wrapper.jar 5 | !**/src/main/** 6 | !**/src/test/** 7 | 8 | .idea 9 | build/ 10 | out/ 11 | tags 12 | lombok.config 13 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java' 3 | id 'idea' 4 | id 'maven' 5 | id 'io.freefair.lombok' version '4.1.6' 6 | id 'org.springframework.boot' version '2.2.4.RELEASE' 7 | id 'io.spring.dependency-management' version '1.0.6.RELEASE' 8 | id "org.asciidoctor.convert" version "1.5.3" 9 | } 10 | 11 | group 'com.johngrib.example' 12 | version '1.0-SNAPSHOT' 13 | sourceCompatibility = '13' 14 | 15 | repositories { 16 | mavenCentral() 17 | mavenLocal() 18 | } 19 | 20 | springBoot { 21 | buildInfo() 22 | } 23 | 24 | test { 25 | useJUnitPlatform() 26 | } 27 | 28 | dependencies { 29 | implementation 'org.springframework.boot:spring-boot-starter-data-jpa' 30 | testCompile group: 'com.h2database', name: 'h2' 31 | testCompile "org.springframework.boot:spring-boot-starter-test" 32 | 33 | testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1' 34 | testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1' 35 | testCompile group: 'org.hamcrest', name: 'hamcrest-all', version: '1.3' 36 | implementation 'org.projectlombok:lombok:1.18.4' 37 | } 38 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/johngrib/example-junit5/342820b6595302c30f74e47e4a916e5efd903875/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sun Dec 22 09:52:45 KST 2019 2 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.2.1-all.zip 3 | distributionBase=GRADLE_USER_HOME 4 | distributionPath=wrapper/dists 5 | zipStorePath=wrapper/dists 6 | zipStoreBase=GRADLE_USER_HOME 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS='"-Xmx64m"' 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS="-Xmx64m" 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'junit5-nested' 2 | 3 | -------------------------------------------------------------------------------- /src/main/java/com/johngrib/example/Application.java: -------------------------------------------------------------------------------- 1 | package com.johngrib.example; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.boot.context.properties.EnableConfigurationProperties; 6 | 7 | @SpringBootApplication 8 | @EnableConfigurationProperties 9 | public class Application { 10 | public static void main(String[] args) { 11 | SpringApplication.run(Application.class, args); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/johngrib/example/math/ComplexNumber.java: -------------------------------------------------------------------------------- 1 | package com.johngrib.example.math; 2 | 3 | import java.util.Objects; 4 | 5 | /** 6 | * 복소수를 정의한다 7 | */ 8 | public class ComplexNumber { 9 | private double real; 10 | private double imagine; 11 | 12 | private ComplexNumber() { 13 | } 14 | 15 | private ComplexNumber(double real, double imagine) { 16 | this.real = real; 17 | this.imagine = imagine; 18 | } 19 | 20 | public static ComplexNumber of(double real, double imagine) { 21 | return new ComplexNumber(real, imagine); 22 | } 23 | 24 | public static ComplexNumber of(double real) { 25 | return new ComplexNumber(real, 0); 26 | } 27 | 28 | /** 29 | * 두 복소수의 덧셈을 연산한다 30 | * 31 | * @param a 첫 번째 복소수 32 | * @param b 두 번째 복소수 33 | * @return 두 복소수의 합 34 | */ 35 | public static ComplexNumber sum(ComplexNumber a, ComplexNumber b) { 36 | return new ComplexNumber(a.real + b.real, a.imagine + b.imagine); 37 | } 38 | 39 | @Override 40 | public boolean equals(Object o) { 41 | if (this == o) { 42 | return true; 43 | } 44 | if (o == null || getClass() != o.getClass()) { 45 | return false; 46 | } 47 | ComplexNumber complex = (ComplexNumber) o; 48 | return Double.compare(complex.real, real) == 0 && 49 | Double.compare(complex.imagine, imagine) == 0; 50 | } 51 | 52 | @Override 53 | public int hashCode() { 54 | return Objects.hash(real, imagine); 55 | } 56 | 57 | @Override 58 | public String toString() { 59 | if (imagine == 0D) { 60 | return String.format("%f", real); 61 | } 62 | return String.format("%f+%fi", real, imagine); 63 | } 64 | 65 | public double getReal() { 66 | return real; 67 | } 68 | 69 | public double getImagine() { 70 | return imagine; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/com/johngrib/example/member/Member.java: -------------------------------------------------------------------------------- 1 | package com.johngrib.example.member; 2 | 3 | import lombok.Getter; 4 | import lombok.Setter; 5 | 6 | import javax.persistence.Entity; 7 | import javax.persistence.GeneratedValue; 8 | import javax.persistence.GenerationType; 9 | import javax.persistence.Id; 10 | 11 | @Entity 12 | @Getter 13 | @Setter 14 | public class Member { 15 | public Member() { 16 | } 17 | 18 | public Member(String name) { 19 | this.name = name; 20 | this.visited = 0; 21 | } 22 | 23 | public Member(String name, int visited) { 24 | this.name = name; 25 | this.visited = visited; 26 | } 27 | 28 | @Id 29 | @GeneratedValue(strategy = GenerationType.IDENTITY) 30 | private Long id; 31 | 32 | private String name; 33 | private int visited; 34 | } 35 | 36 | -------------------------------------------------------------------------------- /src/main/java/com/johngrib/example/member/MemberRepository.java: -------------------------------------------------------------------------------- 1 | package com.johngrib.example.member; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | import org.springframework.data.jpa.repository.Modifying; 5 | import org.springframework.data.jpa.repository.Query; 6 | import org.springframework.data.repository.query.Param; 7 | 8 | public interface MemberRepository extends JpaRepository { 9 | 10 | /** 11 | * 방문 카운터를 증가시킨다. 12 | * 13 | * @return 업데이트된 레코드의 수 14 | */ 15 | @Query("update Member m " 16 | + "set m.visited = m.visited + :plus " 17 | + "where m.id = :id ") 18 | @Modifying(clearAutomatically = true, flushAutomatically = true) 19 | int increaseCount(@Param("id") Long id, @Param("plus") int plus); 20 | } 21 | -------------------------------------------------------------------------------- /src/test/java/com/johngrib/example/math/ComplexNumberKoTest.java: -------------------------------------------------------------------------------- 1 | package com.johngrib.example.math; 2 | 3 | import com.johngrib.example.math.ComplexNumber; 4 | import org.junit.jupiter.api.*; 5 | import static org.junit.jupiter.api.Assertions.assertTrue; 6 | 7 | @SuppressWarnings({"InnerClassMayBeStatic", "NonAsciiCharacters"}) 8 | @DisplayName("ComplexNumber 클래스") 9 | class ComplexNumberKoTest { 10 | 11 | @Nested 12 | @DisplayName("of 메소드는") 13 | class Describe_of { 14 | private final double givenReal = 3d; 15 | private final double givenImagine = 3d; 16 | 17 | @Nested 18 | @DisplayName("만약 실수값만 주어지고 허수값은 없다면") 19 | class Context_with_real { 20 | @Test 21 | @DisplayName("i 값이 0 인 복소수를 리턴한다") 22 | void it_returns_a_valid_complex() { 23 | final ComplexNumber result = ComplexNumber.of(givenReal); 24 | 25 | Assertions.assertEquals(result.getImagine(), 0d, "리턴된 복소수는 허수 값으로 0 을 갖는다"); 26 | Assertions.assertEquals(result.getReal(), givenReal, "리턴된 복소수는 실수 값으로 주어진 실수 값을 갖는다"); 27 | } 28 | } 29 | 30 | @Nested 31 | @DisplayName("만약 실수값과 허수값이 주어진다면") 32 | class Context_with_real_and_i { 33 | @Test 34 | @DisplayName("주어진 실수값과 허수값을 갖는 복소수를 리턴한다") 35 | void it_returns_a_valid_complex() { 36 | final ComplexNumber result = ComplexNumber.of(givenReal, givenImagine); 37 | 38 | Assertions.assertEquals(result.getReal(), givenReal, "리턴된 복소수는 실수 값으로 주어진 실수 값을 갖는다"); 39 | Assertions.assertEquals(result.getImagine(), givenImagine, "리턴된 복소수는 허수 값으로 주어진 허수 값을 갖는다"); 40 | } 41 | } 42 | } 43 | 44 | @Nested 45 | @DisplayName("sum 메소드는") 46 | class Describe_sum { 47 | class TestSum { 48 | /** 주어진 복소수 A */ 49 | ComplexNumber givenA() { 50 | return ComplexNumber.of(1d, 2d); 51 | } 52 | /** 주어진 복소수 B */ 53 | ComplexNumber givenB() { 54 | return ComplexNumber.of(3d, 4d); 55 | } 56 | /** A와 B의 합 */ 57 | ComplexNumber subject() { 58 | return ComplexNumber.sum(givenA(), givenB()); 59 | } 60 | } 61 | 62 | @Nested 63 | @DisplayName("만약 실수부와 허수부가 있는 두 복소수가 주어진다면") 64 | class Context_with_two_complex extends TestSum { 65 | @Test 66 | @DisplayName("실수부와 허수부가 올바르게 계산된 복소수를 리턴한다") 67 | void it_returns_a_valid_complex() { 68 | Assertions.assertEquals(givenA().getReal() + givenB().getReal(), subject().getReal(), 69 | "리턴된 복소수는 두 실수 값의 합을 실수로 갖는다"); 70 | Assertions.assertEquals(givenA().getImagine() + givenB().getImagine(), subject().getImagine(), 71 | "리턴된 복소수는 두 허수 값의 합을 허수로 갖는다"); 72 | } 73 | } 74 | @Nested 75 | @DisplayName("만약 두 복소수 중 하나가 null 이라면") 76 | class Context_with_one_complex extends TestSum { 77 | @Override 78 | ComplexNumber givenA() { 79 | return null; 80 | } 81 | 82 | @Test 83 | @DisplayName("에외를 던진다") 84 | void it_returns_a_valid_complex() { 85 | Assertions.assertThrows(NullPointerException.class, this::subject); 86 | } 87 | } 88 | } 89 | 90 | @Nested 91 | @DisplayName("toString 메소드는") 92 | class Describe_toString { 93 | @Nested 94 | @DisplayName("만약 실수값만 있고 허수값이 없다면") 95 | class Context_with_real { 96 | private final double givenNatual = 3d; 97 | private final String expectPattern = "^3(?:\\.0+)?$"; 98 | private ComplexNumber given = ComplexNumber.of(givenNatual); 99 | 100 | @Test 101 | @DisplayName("실수부만 표현한 문자열을 리턴한다") 102 | void it_returns_a_valid_string() { 103 | Assertions.assertTrue(given.toString().matches(expectPattern)); 104 | } 105 | } 106 | 107 | @Nested 108 | @DisplayName("만약 실수값이 있고 허수값도 있다면") 109 | class Context_with_real_and_imagine { 110 | private final double givenNatual = 3d; 111 | private final double givenImagine = 7d; 112 | private ComplexNumber given = ComplexNumber.of(givenNatual, givenImagine); 113 | private String expectPattern = "^3(?:\\.0+)?\\+7(?:\\.0+)?i$"; 114 | 115 | @Test 116 | @DisplayName("실수부 + 허수부i 형식으로 표현한 문자열을 리턴한다") 117 | void it_returns_a_valid_string() { 118 | assertTrue(given.toString().matches(expectPattern)); 119 | } 120 | } 121 | } 122 | } -------------------------------------------------------------------------------- /src/test/java/com/johngrib/example/math/ComplexNumberTest.java: -------------------------------------------------------------------------------- 1 | package com.johngrib.example.math; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import com.johngrib.example.math.ComplexNumber; 6 | import org.junit.jupiter.api.BeforeEach; 7 | import org.junit.jupiter.api.DisplayName; 8 | import org.junit.jupiter.api.Nested; 9 | import org.junit.jupiter.api.Test; 10 | 11 | import static org.hamcrest.CoreMatchers.is; 12 | import static org.hamcrest.MatcherAssert.assertThat; 13 | 14 | @DisplayName("Describe: ComplexNumber class") 15 | class ComplexNumberTest { 16 | 17 | @Nested 18 | @DisplayName("Describe: of method") 19 | class DescribeAdd { 20 | 21 | @Nested 22 | @DisplayName("Context: with a real number") 23 | class Context_with_naturals { 24 | private final double givenNatual = 3d; 25 | private ComplexNumber given = ComplexNumber.of(givenNatual); 26 | 27 | @Test 28 | @DisplayName("It returns a complex number with 0i.") 29 | void it_has_0_imagine_value() { 30 | assertThat(given.getImagine(), is((0d))); 31 | } 32 | } 33 | } 34 | 35 | @Nested 36 | @DisplayName("Describe: sum method") 37 | class DescribeSum { 38 | @Nested 39 | @DisplayName("Context: with two complex numbers with real and i parts") 40 | class Context_with_naturals { 41 | private ComplexNumber a, b; 42 | 43 | @BeforeEach 44 | void prePareNumbers() { 45 | a = ComplexNumber.of(1d, 2d); 46 | b = ComplexNumber.of(32d, 175d); 47 | } 48 | 49 | ComplexNumber subject() { 50 | return ComplexNumber.sum(a, b); 51 | } 52 | 53 | @Test 54 | @DisplayName("It returns a complex number with the sum of the two real values.") 55 | void it_returns_complex_has_each_real_sum() { 56 | final double expect = a.getReal() + b.getReal(); 57 | final double result = subject().getReal(); 58 | assertThat(result, is(expect)); 59 | } 60 | 61 | @Test 62 | @DisplayName("It returns a complex number with the sum of the two i values") 63 | void it_returns_complex_has_each_imagine_sum() { 64 | final double expect = a.getImagine() + b.getImagine(); 65 | final double result = subject().getImagine(); 66 | assertThat(result, is(expect)); 67 | } 68 | } 69 | } 70 | 71 | @Nested 72 | @DisplayName("Describe: toString") 73 | class GivenToString { 74 | @Nested 75 | @DisplayName("Context: with only real value") 76 | class Context_with_naturals { 77 | private final double givenNatual = 3d; 78 | private final String expectPattern = "^3(?:\\.0+)?$"; 79 | private ComplexNumber given = ComplexNumber.of(givenNatual); 80 | 81 | @Test 82 | @DisplayName("It returns a string that represents only real value") 83 | void it_has_0_imagine_value() { 84 | assertTrue(given.toString().matches(expectPattern)); 85 | } 86 | } 87 | 88 | @Nested 89 | @DisplayName("Context: with a real value and an imagine value") 90 | class Context_with_imagine { 91 | private final double givenNatual = 3d; 92 | private final double givenImagine = 7d; 93 | private ComplexNumber given = ComplexNumber.of(givenNatual, givenImagine); 94 | private String expectPattern = "^3(?:\\.0+)?\\+7(?:\\.0+)?i$"; 95 | 96 | @Test 97 | @DisplayName("It returns a string in the form of real and i addition.") 98 | void it_has_0_imagine_value() { 99 | System.out.println(given); 100 | assertTrue(given.toString().matches(expectPattern)); 101 | } 102 | } 103 | 104 | } 105 | } -------------------------------------------------------------------------------- /src/test/java/com/johngrib/example/member/MemberRepositoryPassTest.java: -------------------------------------------------------------------------------- 1 | package com.johngrib.example.member; 2 | 3 | import org.junit.jupiter.api.*; 4 | import org.junit.jupiter.api.extension.ExtendWith; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; 7 | import org.springframework.test.context.junit.jupiter.SpringExtension; 8 | 9 | @ExtendWith(SpringExtension.class) 10 | @DataJpaTest 11 | @DisplayName("MemberRepository") 12 | class MemberRepositoryPassTest { 13 | 14 | @Autowired 15 | private MemberRepository repository; 16 | final String givenName = "홍길동"; 17 | 18 | @Nested 19 | @DisplayName("save 메소드") 20 | class Describe_save { 21 | 22 | @BeforeEach 23 | void preapre() { 24 | repository.deleteAll(); 25 | } 26 | 27 | @Nested 28 | @DisplayName("멤버 객체가 주어지면") 29 | class Context_with_a_member { 30 | final Member givenMember = new Member(givenName); 31 | 32 | @Test 33 | @DisplayName("주어진 객체를 저장하고, 저장된 객체를 리턴한다") 34 | void it_saves_obj_and_returns_a_saved_obj() { 35 | Assertions.assertNull(givenMember.getId()); 36 | 37 | final Member saved = repository.save(givenMember); 38 | 39 | Assertions.assertNotNull(saved.getId()); 40 | Assertions.assertEquals(saved.getName(), givenName); 41 | } 42 | } 43 | } 44 | 45 | @Nested 46 | @DisplayName("increase 메소드 - DataJpaTest 어노테이션을 사용하는 테스트") 47 | class Describe_increase { 48 | @Nested 49 | @DataJpaTest // 필수 50 | @DisplayName("저장된 멤버 객체의 아이디와 증가할 숫자가 주어지면") 51 | class Context_with_a_member { 52 | final int givenNumber = 3; 53 | Member givenMember; 54 | long givenId() { 55 | return givenMember.getId(); 56 | } 57 | 58 | @Autowired 59 | private MemberRepository repository; 60 | 61 | @BeforeEach 62 | void preapre() { 63 | givenMember = repository.save(new Member(givenName, 0)); 64 | } 65 | 66 | @Test 67 | @DisplayName("방문 카운트를 증가시키고, 업데이트된 레코드 수를 리턴한다") 68 | void it_increases_count_and_returns_count_of_updated_records() { 69 | Assertions.assertEquals(givenMember.getVisited(), 0, 70 | "실행 전의 카운트는 0 이다"); 71 | 72 | final int updatedRecords = repository.increaseCount(givenId(), givenNumber); 73 | 74 | Assertions.assertEquals(updatedRecords, 1, 75 | "한 건이 업데이트되었다"); 76 | Assertions.assertEquals( 77 | givenNumber, 78 | repository.findById(givenId()).get().getVisited(), 79 | "주어진 증가 숫자만큼 방문 카운트가 증가한다" 80 | ); 81 | } 82 | } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/test/java/com/johngrib/example/member/MemberRepositoryPassWithExtendsTest.java: -------------------------------------------------------------------------------- 1 | package com.johngrib.example.member; 2 | 3 | import lombok.Getter; 4 | import org.junit.jupiter.api.*; 5 | import org.junit.jupiter.api.extension.ExtendWith; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; 8 | import org.springframework.test.context.junit.jupiter.SpringExtension; 9 | 10 | @DataJpaTest 11 | @DisplayName("MemberRepository") 12 | class MemberRepositoryPassWithExtendsTest { 13 | 14 | @ExtendWith(SpringExtension.class) 15 | @DataJpaTest 16 | @Getter 17 | class JpaTest { 18 | @Autowired MemberRepository memberRepository; 19 | } 20 | 21 | final String givenName = "홍길동"; 22 | 23 | @Nested 24 | @DisplayName("save 메소드") 25 | class Describe_save extends JpaTest { 26 | 27 | @BeforeEach 28 | void preapre() { 29 | getMemberRepository().deleteAll(); 30 | } 31 | 32 | @Nested 33 | @DisplayName("멤버 객체가 주어지면") 34 | class Context_with_a_member { 35 | final Member givenMember = new Member(givenName); 36 | 37 | @Test 38 | @DisplayName("주어진 객체를 저장하고, 저장된 객체를 리턴한다") 39 | void it_saves_obj_and_returns_a_saved_obj() { 40 | Assertions.assertNull(givenMember.getId()); 41 | 42 | final Member saved = getMemberRepository().save(givenMember); 43 | 44 | Assertions.assertNotNull(saved.getId()); 45 | Assertions.assertEquals(saved.getName(), givenName); 46 | } 47 | } 48 | } 49 | 50 | @Nested 51 | @DisplayName("increase 메소드 - DataJpaTest 어노테이션을 상속으로 해결한 테스트") 52 | class Describe_increase { 53 | @Nested 54 | @DisplayName("저장된 멤버 객체의 아이디와 증가할 숫자가 주어지면 - 여기에서 JpaTest를 상속한다") 55 | class Context_with_a_member extends JpaTest { 56 | final int givenNumber = 3; 57 | Member givenMember; 58 | long givenId() { 59 | return givenMember.getId(); 60 | } 61 | 62 | @BeforeEach 63 | void prepare() { 64 | givenMember = getMemberRepository().save(new Member(givenName, 0)); 65 | } 66 | 67 | @Test 68 | @DisplayName("방문 카운트를 증가시키고, 업데이트된 레코드 수를 리턴한다") 69 | void it_increases_count_and_returns_count_of_updated_records() { 70 | Assertions.assertEquals(givenMember.getVisited(), 0, 71 | "실행 전의 카운트는 0 이다"); 72 | 73 | final int updatedRecords = getMemberRepository().increaseCount(givenId(), givenNumber); 74 | 75 | Assertions.assertEquals(updatedRecords, 1, 76 | "한 건이 업데이트되었다"); 77 | Assertions.assertEquals( 78 | givenNumber, 79 | getMemberRepository().findById(givenId()).get().getVisited(), 80 | "주어진 증가 숫자만큼 방문 카운트가 증가한다" 81 | ); 82 | } 83 | } 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /src/test/java/com/johngrib/example/member/MemberRepositoryTest.java: -------------------------------------------------------------------------------- 1 | package com.johngrib.example.member; 2 | 3 | import org.junit.jupiter.api.*; 4 | import org.junit.jupiter.api.extension.ExtendWith; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; 7 | import org.springframework.test.context.junit.jupiter.SpringExtension; 8 | 9 | @ExtendWith(SpringExtension.class) 10 | @DataJpaTest 11 | @DisplayName("MemberRepository") 12 | class MemberRepositoryTest { 13 | 14 | @Autowired 15 | private MemberRepository repository; 16 | final String givenName = "홍길동"; 17 | 18 | @Nested 19 | @DisplayName("save 메소드") 20 | class Describe_save { 21 | 22 | @BeforeEach 23 | void prepare() { 24 | repository.deleteAll(); 25 | } 26 | 27 | @Nested 28 | @DisplayName("멤버 객체가 주어지면") 29 | class Context_with_a_member { 30 | final Member givenMember = new Member(givenName); 31 | 32 | @Test 33 | @DisplayName("주어진 객체를 저장하고, 저장된 객체를 리턴한다") 34 | void it_saves_obj_and_returns_a_saved_obj() { 35 | Assertions.assertNull(givenMember.getId()); 36 | 37 | final Member saved = repository.save(givenMember); 38 | 39 | Assertions.assertNotNull(saved.getId()); 40 | Assertions.assertEquals(saved.getName(), givenName); 41 | } 42 | } 43 | } 44 | 45 | @Nested 46 | @DisplayName("increase 메소드 - JPA 어노테이션이 없어 실패하는 테스트") 47 | class Describe_increase { 48 | @Nested 49 | @DisplayName("저장된 멤버 객체의 아이디와 증가할 숫자가 주어지면") 50 | class Context_with_a_member { 51 | final int givenNumber = 3; 52 | Member givenMember; 53 | long givenId() { 54 | return givenMember.getId(); 55 | } 56 | 57 | @BeforeEach 58 | void preapre() { 59 | givenMember = repository.save(new Member(givenName, 0)); 60 | } 61 | 62 | @Test 63 | @DisplayName("방문 카운트를 증가시키고, 업데이트된 레코드 수를 리턴한다") 64 | void it_increases_count_and_returns_count_of_updated_records() { 65 | Assertions.assertEquals(givenMember.getVisited(), 0, 66 | "실행 전의 카운트는 0 이다"); 67 | 68 | final int updatedRecords = repository.increaseCount(givenId(), givenNumber); 69 | 70 | Assertions.assertEquals(updatedRecords, 1, 71 | "한 건이 업데이트되었다"); 72 | Assertions.assertEquals( 73 | givenNumber, 74 | repository.findById(givenId()).get().getVisited(), 75 | "주어진 증가 숫자만큼 방문 카운트가 증가한다" 76 | ); 77 | } 78 | } 79 | } 80 | } 81 | --------------------------------------------------------------------------------