1. ๊ฐ๋จํ ๊ณ์ฐ๊ธฐ ๊ตฌํ
1) MyCalculator
- ์์น : com.pcy.dayonetest (main)
package com.pcy.dayonetest;
public class MyCalculator {
private Double result;
// ์์ฑ์
public MyCalculator() {
this.result = 0.0;
}
// ์์ฑ์
public MyCalculator(Double initResult) {
this.result = initResult;
}
// ๋ํ๊ธฐ
public MyCalculator add(Double number) {
this.result += number;
return this;
}
// ๋นผ๊ธฐ
public MyCalculator minus(Double number) {
this.result -= number;
return this;
}
// ๊ณฑํ๊ธฐ
public MyCalculator multiply(Double number) {
this.result *= number;
return this;
}
// ๋๋๊ธฐ
public MyCalculator divide(Double number) {
if (number == 0.0) {
throw new ZeroDivisionException();
}
this.result /= number;
return this;
}
// ๊ฒฐ๊ณผ๊ฐ ๋ฐํ
public Double getResult() {
return this.result;
}
public static class ZeroDivisionException extends RuntimeException {
}
}
- ๊ฐ๋จํ ๊ณ์ฐ๊ธฐ ํ๋ก๊ทธ๋จ ์ ๋๋ค.
2) MyCalculatorApplication
- MyCalculator ์คํํ๋ ์์์ ๋๋ค.
- ์์น : com.pcy.dayonetest (main)
package com.pcy.dayonetest;
public class MyCalculatorApplication {
public static void main(String[] args) {
// MyCalculator ์คํ ํ์ธ
MyCalculator myCalculator = new MyCalculator();
myCalculator.add(10.0)
.minus(2.0)
.multiply(2.0);
System.out.println(myCalculator.getResult());
// Exceptiom ์คํ ํ์ธ
MyCalculator myCalculator1 = new MyCalculator();
myCalculator1.add(2.0)
.divide(0.0);
System.out.println(myCalculator1.getResult());
}
}
- ์ํ ์ myCalculator์ ๊ฒฐ๊ณผ๊ฐ์ 16 ์ ๋๋ค.
- myCalculator1์ 0์ผ๋ก ๋๋๊ธฐ๋ฅผ ์งํํ์ฌ Exception์ด ๋ฐ์ํ์์ต๋๋ค.
2. ๊ณ์ฐ๊ธฐ ํ ์คํธ ์ฝ๋ ์์ฑ
- MyCalculator.java ์์ โฅ(option) + โฉ(return) ํด๋ฆญํ์ฌ ์์ฑํฉ๋๋ค.
1) MyCalculatorTest
- MyCalculator ํ ์คํธํ๋ ์์์ ๋๋ค.
- ์์น : com.pcy.dayonetest (test)
package com.pcy.dayonetest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class MyCalculatorTest {
@Test
void addTest() {
// AAA ํจํด
// Arrange - ์ค๋น
MyCalculator myCalculator = new MyCalculator();
// Act - ํ๋
myCalculator.add(10.0);
// Assert - ๊ฒ์ฆ
Assertions.assertEquals(10.0, myCalculator.getResult());
}
@Test
void minus() {
// GWT ํจํด
// Given - ์ค๋น
MyCalculator myCalculator = new MyCalculator(10.0);
// When - ํ๋
myCalculator.minus(5.0);
// Then - ๊ฒ์ฆ
Assertions.assertEquals(5.0, myCalculator.getResult());
}
@Test
void multiply() {
// Given
MyCalculator myCalculator = new MyCalculator(10.0);
// When
myCalculator.multiply(2.0);
// Then
Assertions.assertEquals(20.0, myCalculator.getResult());
}
@Test
void divide() {
// Given
MyCalculator myCalculator = new MyCalculator(10.0);
// When
myCalculator.divide(2.0);
// Then
Assertions.assertEquals(5.0, myCalculator.getResult());
}
@Test
void complicatedCalculateTest() {
// given
MyCalculator myCalculator = new MyCalculator();
// when
Double result = myCalculator
.add(10.0)
.minus(4.0)
.multiply(2.0)
.divide(3.0)
.getResult();
// then
Assertions.assertEquals(4.0, result);
}
@Test
void divideZeroTest() {
// given
MyCalculator myCalculator = new MyCalculator(10.0);
// when & then
Assertions.assertThrows(MyCalculator.ZeroDivisionException.class, () ->{
myCalculator.divide(0.0);
});
}
}
- ํ ์คํธ ๋ฉ์๋๋ GWT ํจํด์ผ๋ก ์์ฑํฉ๋๋ค.
- given - when - then
- ๊ฒฐ๊ณผ๊ฐ์ ๊ฒ์ฆํ ๋๋ assertEquals ์ฌ์ฉํฉ๋๋ค.
- Exception ํด๋์ค๋ฅผ ํ์ธํ ๊ฒฝ์ฐ์๋ assertThrows ๋ฅผ ์ฌ์ฉํฉ๋๋ค.
- MyCalculatorTest๋ฅผ ์ํํ๋ฉด ์๋์ ๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ํ์ธํ ์ ์์ต๋๋ค.
3. Github์ผ๋ก ํ์ธ
์ ์ฒด ํ์ผ ํ์ธ(ํ์ฌ ๋ณ๊ฒฝ๋ด์ญ)
[Github]
์ฐธ์กฐ : ์ธํ๋ฐ ๊ฐ์ [์ฅฌ์ฅฌ์ ํจ๊ป ํ๋ฃจ๋ง์ ๋๋ด๋ ์คํ๋ง ํ ์คํธ]
'๐ป FrameWork(ํ๋ ์์ํฌ) > SpringTEST(์คํ๋งํ ์คํธ)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
3-1 [Mocktio] ๊ฐ๋จํ ์ฑ์ ์ ์ฅ ์ ํ๋ฆฌ์ผ์ด์ ๊ตฌํ(๋ก์ปฌ ํ๊ฒฝ ๊ตฌ์ฑ) (0) | 2024.06.03 |
---|---|
2-4 [Junit] ํ ์คํธ ๋ฐ๋ณต ๋ฐ ์ ๋ ฅ๊ฐ ๋ค์ํ๊ฒ ์ค์ (0) | 2024.06.03 |
2-3 [Junit] ํ ์คํธ์ ์ด๋ฆ๋ถ์ด๊ธฐ (0) | 2024.06.03 |
2-2 [Junit] Assertions ๋ฉ์๋ (0) | 2024.06.02 |
1-1 ์คํ๋ง๋ถํธ(SpringBoot) ํ๋ก์ ํธ ์์ฑ ๋ฐ ๊ฐ๋จํ TEST ์์ฑ (0) | 2024.06.01 |