From 5ea3fb6259926668e82955828a192474161d65ed Mon Sep 17 00:00:00 2001 From: hou27 Date: Sat, 11 Feb 2023 18:41:57 +0900 Subject: [PATCH 1/8] =?UTF-8?q?=ED=95=98=EB=93=9C=20=EC=BD=94=EB=94=A9?= =?UTF-8?q?=EB=90=9C=20=EA=B2=BD=EB=A1=9C=EB=A5=BC=20=EC=82=AC=EC=9A=A9?= =?UTF-8?q?=ED=95=9C=20=EA=B2=BD=EC=9A=B0=20-=20#79?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 파일 경로가 하드 코딩되어있어 테스트하려면 반드시 해당 경로에 파일이 위치해야 한다. 이런 경로 뿐만 아니라 하드 코딩된 ip, port 정보도 테스트를 어렵게 만든다. --- .../test/java/com/hou27/chap08/PaySync.java" | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 "\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/PaySync.java" diff --git "a/\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/PaySync.java" "b/\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/PaySync.java" new file mode 100644 index 0000000..73426a1 --- /dev/null +++ "b/\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/PaySync.java" @@ -0,0 +1,21 @@ +package com.hou27.chap08; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.stream.Collectors; + +public class PaySync { + private PayInfoDao payInfoDao = new PayInfoDao(); + + public void sync() { + // 하드 코딩된 경로를 사용하고 있다. + Path path = Path.of("tmp/paydata/payinfo.csv"); + List payInfos = Files.lines(path) + .map(line -> { + String[] data = line.split(","); + return new PayInfo(data[0], Integer.parseInt(data[1])); + }).collect(Collectors.toList()); + + payInfos.forEach(pi -> payInfoDao.insert(pi)); + } +} From ce7fac430b96a3e2342622b066347982836f9615 Mon Sep 17 00:00:00 2001 From: hou27 Date: Sat, 11 Feb 2023 18:45:09 +0900 Subject: [PATCH 2/8] =?UTF-8?q?=EC=9D=98=EC=A1=B4=20=EA=B0=9D=EC=B2=B4?= =?UTF-8?q?=EB=A5=BC=20=EC=A7=81=EC=A0=91=20=EC=83=9D=EC=84=B1=ED=95=9C=20?= =?UTF-8?q?=EA=B2=BD=EC=9A=B0=20-=20#79?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 의존하는 대상을 직접 생성하는 경우도 테스트 시 올바르게 동작시키기 위해 필요한 환경을 모두 구성해야하므로 테스트가 어려워진다. --- .../src/test/java/com/hou27/chap08/PaySync.java" | 1 + 1 file changed, 1 insertion(+) diff --git "a/\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/PaySync.java" "b/\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/PaySync.java" index 73426a1..da1375a 100644 --- "a/\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/PaySync.java" +++ "b/\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/PaySync.java" @@ -5,6 +5,7 @@ import java.util.stream.Collectors; public class PaySync { + // 의존 객체를 직접 생성하고 있다. private PayInfoDao payInfoDao = new PayInfoDao(); public void sync() { From 66316238f81812d5e938f31e509364716965ceeb Mon Sep 17 00:00:00 2001 From: hou27 Date: Sat, 11 Feb 2023 19:25:14 +0900 Subject: [PATCH 3/8] =?UTF-8?q?=EC=A0=95=EC=A0=81=EC=9D=B8=20=EB=A9=94?= =?UTF-8?q?=EC=84=9C=EB=93=9C=EB=A1=9C=20=EC=9D=B8=ED=95=B4=20=ED=9E=98?= =?UTF-8?q?=EB=93=A0=20=EA=B2=BD=EC=9A=B0=20-=20#79?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. https://unabated.tistory.com/1041 - java에서 static을 지양해야하는 이유 - 테스트 중 static한 field 또는 method를 사용할 경우 모든 클래스는 예상되는 상태에서 시작되어야하며 매우 까다로워질 수 있다. 2. AuthUtil 클래스가 어떠한 인증 서버와 통신하는 경우 동작하고 있는 인증 서버가 필요하며, 통신할 인증 서버 정보를 시스템 프로퍼티에서 가져온다면 그 정보 또한 테스트 환경에 맞게 설정해야 한다. --- .../java/com/hou27/chap08/LoginService.java" | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 "\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/LoginService.java" diff --git "a/\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/LoginService.java" "b/\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/LoginService.java" new file mode 100644 index 0000000..a2e6be6 --- /dev/null +++ "b/\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/LoginService.java" @@ -0,0 +1,30 @@ +package com.hou27.chap08; + +public class LoginService { + + private String authKey = "some_key"; + private CustomerRepository customerRepository; + + public LoginService(CustomerRepository customerRepository) { + this.customerRepository = customerRepository; + } + + public LoginResult login(String id, String password) { + int resp = 0; + boolean authorized = AuthUtil.authorize(authKey); // 정적 메서드를 사용 + if (authorized) { + resp = AuthUtil.authenticate(id, password); // 정적 메서드를 사용 + } else { + resp = -1; + } + + if (resp == -1) { + return LoginResult.badAuthKey(); + } else if (resp == 1) { + return LoginResult.success(); + } else { + return LoginResult.fail(); + } + } + +} From 5752f235f6a662386eafaef443981a8fbede3f4b Mon Sep 17 00:00:00 2001 From: hou27 Date: Sat, 11 Feb 2023 21:01:01 +0900 Subject: [PATCH 4/8] =?UTF-8?q?=EC=8B=A4=ED=96=89=20=EC=8B=9C=EC=A0=90?= =?UTF-8?q?=EC=97=90=20=EB=94=B0=EB=9D=BC=20=EA=B2=B0=EA=B3=BC=EA=B0=80=20?= =?UTF-8?q?=EB=8B=AC=EB=9D=BC=EC=A7=80=EB=8A=94=20=EA=B2=BD=EC=9A=B0=20-?= =?UTF-8?q?=20#79?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 시점에 따라 결과가 달라지기 때문에 테스트가 어렵다 --- .../hou27/chap08/UserPointCalculator.java" | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 "\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/UserPointCalculator.java" diff --git "a/\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/UserPointCalculator.java" "b/\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/UserPointCalculator.java" new file mode 100644 index 0000000..d8d02ee --- /dev/null +++ "b/\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/UserPointCalculator.java" @@ -0,0 +1,37 @@ +package com.hou27.chap08; + +public class UserPointCalculator { + + private SubscriptionDao subscriptionDao; + private ProductDao productDao; + + public UserPointCalculator( + SubscriptionDao subscriptionDao, + ProductDao productDao + ) { + this.subscriptionDao = subscriptionDao; + this.productDao = productDao; + } + + public int calculatePoint(User u) { + Subscription s = subscriptionDao.selectByUser(u); + if(s == null) { + throw new NoSubscriptionException(); + } + + Product p = productDao.selectById(s.getProductId()); + LocalDate now = LocalDate.now(); // 현재 날짜를 구한다. + int point = 0; + if(s.isFinished(now)) { // 현재 시간에 따라 결과가 달라진다. + point += p.getDefaultPoint(); + } else { + point += p.getDefaultPoint() + 10; + } + + if(s.getGrade() == GOLD) { + point += 100; + } + + return point; + } +} From 4f49c7854d5cfe94288f300a65841b6bfa863546 Mon Sep 17 00:00:00 2001 From: hou27 Date: Sat, 11 Feb 2023 22:22:57 +0900 Subject: [PATCH 5/8] =?UTF-8?q?=ED=95=98=EB=93=9C=20=EC=BD=94=EB=94=A9?= =?UTF-8?q?=EB=90=9C=20=EC=83=81=EC=88=98=EB=A5=BC=20=EB=A9=94=EC=84=9C?= =?UTF-8?q?=EB=93=9C=20=ED=8C=8C=EB=9D=BC=EB=AF=B8=ED=84=B0=EB=A1=9C=20?= =?UTF-8?q?=EB=B0=9B=EA=B8=B0=20-=20#79?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 파일 경로를 변경할 수 있는 메서드를 구현하여 경로를 교체할 수 있도록 함. 경우에 따라 생성자를 통해 받게 작성할 수도 있다. --- .../src/test/java/com/hou27/chap08/PaySync.java" | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git "a/\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/PaySync.java" "b/\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/PaySync.java" index da1375a..1b59ea2 100644 --- "a/\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/PaySync.java" +++ "b/\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/PaySync.java" @@ -1,5 +1,6 @@ package com.hou27.chap08; +import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.stream.Collectors; @@ -7,10 +8,15 @@ public class PaySync { // 의존 객체를 직접 생성하고 있다. private PayInfoDao payInfoDao = new PayInfoDao(); + private String filePath = "tmp/paydata/payinfo.csv"; - public void sync() { - // 하드 코딩된 경로를 사용하고 있다. - Path path = Path.of("tmp/paydata/payinfo.csv"); + // 경로를 설정 가능하게 메서드 생성 + public void setFilePath(String filePath) { + this.filePath = filePath; + } + + public void sync() throws IOException { + Path path = Path.of(filePath); List payInfos = Files.lines(path) .map(line -> { String[] data = line.split(","); From 5aae78e76e270e3badb9dd0e510f1ac169ff2b27 Mon Sep 17 00:00:00 2001 From: hou27 Date: Sat, 11 Feb 2023 22:34:22 +0900 Subject: [PATCH 6/8] =?UTF-8?q?=EC=9D=98=EC=A1=B4=20=EB=8C=80=EC=83=81?= =?UTF-8?q?=EC=9D=84=20=EC=A3=BC=EC=9E=85=20=EB=B0=9B=EA=B8=B0=20-=20#79?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 의존 대상을 주입 받도록 하여 테스트 진행 시 대역을 사용할 수 있도록 하였다. --- .../src/test/java/com/hou27/chap08/PaySync.java" | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git "a/\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/PaySync.java" "b/\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/PaySync.java" index 1b59ea2..b3a8ce5 100644 --- "a/\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/PaySync.java" +++ "b/\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/PaySync.java" @@ -6,10 +6,14 @@ import java.util.stream.Collectors; public class PaySync { - // 의존 객체를 직접 생성하고 있다. - private PayInfoDao payInfoDao = new PayInfoDao(); + private PayInfoDao payInfoDao; private String filePath = "tmp/paydata/payinfo.csv"; + // 생성자를 통해 의존성을 주입받는다. + public PaySync(PayInfoDao payInfoDao) { + this.payInfoDao = payInfoDao; + } + // 경로를 설정 가능하게 메서드 생성 public void setFilePath(String filePath) { this.filePath = filePath; From 41a315488d8d32d85af7012c23abb750b99d6d70 Mon Sep 17 00:00:00 2001 From: hou27 Date: Sat, 11 Feb 2023 23:31:43 +0900 Subject: [PATCH 7/8] =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=ED=95=98?= =?UTF-8?q?=EA=B3=A0=20=EC=8B=B6=EC=9D=80=20=EC=BD=94=EB=93=9C=20=EB=B6=84?= =?UTF-8?q?=EB=A6=AC=20-=20#79?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 테스트하고 싶은 코드를 따로 분리하여 해당 기능만 테스트할 수 있다. --- .../hou27/chap08/UserPointCalculator.java" | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git "a/\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/UserPointCalculator.java" "b/\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/UserPointCalculator.java" index d8d02ee..df60a4f 100644 --- "a/\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/UserPointCalculator.java" +++ "b/\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/UserPointCalculator.java" @@ -21,8 +21,27 @@ public int calculatePoint(User u) { Product p = productDao.selectById(s.getProductId()); LocalDate now = LocalDate.now(); // 현재 날짜를 구한다. + PointRule rule = new PointRule(); + int point = rule.calculate(s, p, now); +// int point = 0; +// if(s.isFinished(now)) { // 현재 시간에 따라 결과가 달라진다. +// point += p.getDefaultPoint(); +// } else { +// point += p.getDefaultPoint() + 10; +// } +// +// if(s.getGrade() == GOLD) { +// point += 100; +// } + + return point; + } +} + +class PointRule { + public int calculate(Subscription s, Product p, LocalDate now) { int point = 0; - if(s.isFinished(now)) { // 현재 시간에 따라 결과가 달라진다. + if(s.isFinished(now)) { point += p.getDefaultPoint(); } else { point += p.getDefaultPoint() + 10; @@ -34,4 +53,4 @@ public int calculatePoint(User u) { return point; } -} +} \ No newline at end of file From 3c47622b66e9a3b4d3770d62a99c464f44d63f92 Mon Sep 17 00:00:00 2001 From: hou27 Date: Sun, 12 Feb 2023 00:00:17 +0900 Subject: [PATCH 8/8] =?UTF-8?q?=EC=8B=9C=EA=B0=84=EC=9D=B4=EB=82=98=20?= =?UTF-8?q?=EC=9E=84=EC=9D=98=20=EA=B0=92=20=EC=83=9D=EC=84=B1=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EB=B6=84=EB=A6=AC=ED=95=98=EA=B8=B0=20-=20#79?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 생성 기능은 따로 분리하여 테스트할 때 대역을 사용할 수 있도록 함. --- .../src/test/java/com/hou27/chap08/Times.java" | 9 +++++++++ .../java/com/hou27/chap08/UserPointCalculator.java" | 10 ++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 "\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/Times.java" diff --git "a/\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/Times.java" "b/\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/Times.java" new file mode 100644 index 0000000..e9694c1 --- /dev/null +++ "b/\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/Times.java" @@ -0,0 +1,9 @@ +package com.hou27.chap08; + +import java.time.LocalDate; + +public class Times { + public LocalDate today() { + return LocalDate.now(); + } +} diff --git "a/\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/UserPointCalculator.java" "b/\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/UserPointCalculator.java" index df60a4f..e1733ce 100644 --- "a/\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/UserPointCalculator.java" +++ "b/\352\271\200\354\240\225\355\230\270/src/test/java/com/hou27/chap08/UserPointCalculator.java" @@ -1,16 +1,21 @@ package com.hou27.chap08; +import java.time.LocalDate; + public class UserPointCalculator { private SubscriptionDao subscriptionDao; private ProductDao productDao; + private Times times; public UserPointCalculator( SubscriptionDao subscriptionDao, - ProductDao productDao + ProductDao productDao, + Times times ) { this.subscriptionDao = subscriptionDao; this.productDao = productDao; + this.times = times; } public int calculatePoint(User u) { @@ -20,7 +25,8 @@ public int calculatePoint(User u) { } Product p = productDao.selectById(s.getProductId()); - LocalDate now = LocalDate.now(); // 현재 날짜를 구한다. +// LocalDate now = LocalDate.now(); // 현재 날짜를 구한다. + LocalDate now = times.today(); // 현재 날짜를 구한다. PointRule rule = new PointRule(); int point = rule.calculate(s, p, now); // int point = 0;