Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

첫번쨰 단위테스트 #100

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ repositories {
dependencies {
testImplementation "org.junit.jupiter:junit-jupiter:5.7.2"
testImplementation "org.assertj:assertj-core:3.19.0"
// https://mvnrepository.com/artifact/org.hamcrest/hamcrest
testImplementation group: 'org.hamcrest', name: 'hamcrest', version: '2.1'
testImplementation "org.junit.jupiter:junit-jupiter-params:5.8.1"

}

test {
Expand Down
91 changes: 91 additions & 0 deletions src/main/java/baseball/BaseBallMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package baseball;

import java.util.Arrays;
import java.util.Scanner;
import java.util.stream.Collectors;

public class BaseBallMain {

public static String[] computerRandomNumbers = new String[3];
public static String[] userRandomNumbers = new String[3];

public static int ballNum = 0;
public static int strike = 0;
public static boolean isReturnGame = true;
public static int isReturnNumber = 0;
public static Scanner sc = new Scanner(System.in);

public static String inputValue() {
return sc.next();
}

public static void getComputerRandomNumbers() {
for(int i = 0; i < computerRandomNumbers.length;i++) {
computerRandomNumbers[i] = String.valueOf((int)(Math.random() * 9) + 1);
for(int j = 0; j < i; j++) {
if(computerRandomNumbers[i].equals(computerRandomNumbers[j])) {
i--;
}
}
}
getComputerRandomNumbersTrans(computerRandomNumbers);
}

public static void getComputerRandomNumbersTrans(String[] computerRandomNumbers) {
String resComputerRandomNumbers = Arrays.stream(BaseBallMain.computerRandomNumbers).collect(Collectors.joining());
print(resComputerRandomNumbers);
}

public static void characterComparison(String userInputNumbers) {
for(int i = 0; i < computerRandomNumbers.length;i++) {
if(userInputNumbers.contains(computerRandomNumbers[i])) {
indexNumberComparison(userInputNumbers.indexOf(computerRandomNumbers[i]),i);
}
}
}

public static void indexNumberComparison(int indexNumbers, int i ) {
if( indexNumbers == i ) {
strike++;
} else {
ballNum++;
}
}

public static void numberBaseBallIsBoolean() {
if( strike == 3 ) {
System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료");
System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.");
isReturnNumber = Integer.parseInt(inputValue());
gameChoiceNumbers(isReturnNumber);
}
}

public static void gameChoiceNumbers(int isReturnNumber) {
if(isReturnNumber == 2) {
isReturnGame = false;
return;
}
getComputerRandomNumbers();
}

public static void print(String resComputerRandomNumbers) {
System.out.println("컴퓨터 :" + resComputerRandomNumbers);
}

public static void main(String[] args) {
getComputerRandomNumbers();

String userInputNumber = "";

while ( isReturnGame ) {
System.out.print("숫자를 입력해 주세요 : ");
String inputNum = inputValue();
characterComparison(inputNum);
System.out.println(ballNum + "볼 " + strike + "스트라이크");
numberBaseBallIsBoolean();
ballNum = 0;
strike = 0;
}
}
}
78 changes: 78 additions & 0 deletions src/test/java/study/SetTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package study;

import org.assertj.core.internal.Strings;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.HashSet;
import java.util.Set;

/*
지원을 안해준다.hamcrest
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
*/

/*
파악 필요
import static org.hamcrest.MatcherAssert.assertThat;*/


import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;

import static org.assertj.core.api.Assertions.*;


public class SetTest {
private Set<Integer> numbers;
// Set은 무엇이며 HashSet은 어떤구조로 이루어져있는가?

@BeforeEach
void setUp() {
numbers = new HashSet<>();
numbers.add(1);
numbers.add(1);
numbers.add(2);
numbers.add(3);
}

@Test
@DisplayName("현재 Set에 담긴 데이터 값이 4개인지")
void sizeFour() {
assertThat(numbers.size()).isEqualTo(4);
}

@Test
@DisplayName("현재 Set에 담긴 데이터 값이 3개인지")
void sizeThree() {
assertThat(numbers.size()).isEqualTo(3);
}

@Test
@DisplayName("값들이 정상적으로 존재하는지 확인")
void contains() {
assertThat(numbers.contains(1)).isTrue();
assertThat(numbers.contains(2)).isTrue();
assertThat(numbers.contains(3)).isTrue();
}

// 중복되는 코드들은 생각해서 개발 필요
// 둘다 동일한건지 확인 필요 import static org.hamcrest.MatcherAssert.assertThat;
@ParameterizedTest
@ValueSource(strings = {"", ""})
void isBlank_ShouldReturnTrueForNullOrBlankStrings(String input) {
assertThat(input).isEqualTo("");
}

// 입력값에 따른 결과값이 달라지는 테스트 구현
@ParameterizedTest
@CsvSource(value = {"test:test", "tEst:test","Java:java","asb:sss"}, delimiter = ':')
void toLowerCase_shouldGenerateTheExpectedLowercaseValue(String input,String expected) {
String actualValue = input.toLowerCase();
assertThat(expected).isEqualTo(actualValue);
}

}
60 changes: 59 additions & 1 deletion src/test/java/study/StringTest.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,71 @@
package study;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.*;

public class StringTest {
@Test
void replace() {
String actual = "abc".replace("b", "d");
assertThat(actual).isEqualTo("adc");
}

@Test
void test() {
String[] actual = {"2"};
String s = "123";

System.out.println(s.indexOf(actual[0]));
System.out.println(s.contains(actual[0]));
}

// 요구사항 1
@Test
void split() {
String[] actual = "1,2".split(",");
assertThat(actual[0]).isEqualTo("1");
}

@Test
void contains() {
String[] actual = "1,2".split(",");
assertThat(actual).contains(actual[0],actual[1]);
}

@Test
void containsExactly() {
String[] actual = "1,2".split(",");
assertThat(actual).containsExactly(actual[0],actual[1]);
}

// 요구사항 2
@Test
void substring() {
String str = "(1,2)".substring(1,4);
assertThat(str).isEqualTo("1,2");
}

// 요구사항 3
@Test
void charAt() {
char oneChar = "123".charAt(5);
System.out.println(oneChar);
}

@Test
@DisplayName("입력값 범위 밖일 경우 StringIndexOutOfBoundsException 확인")
public void StringIndexOutOfBoundsException() {
String str = "abc";

assertThatThrownBy(() -> str.charAt(str.length()))
.isInstanceOf(StringIndexOutOfBoundsException.class)
.hasMessageContaining("String 범위를 넘어섰습니다.")
.hasMessageContaining(String.valueOf(str.length()));
}




}
5 changes: 5 additions & 0 deletions src/test/java/study/baseBallTest/BaseBallTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package study.baseBallTest;

public class BaseBallTest {

}
21 changes: 21 additions & 0 deletions src/test/java/study/calculator/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package study.calculator;

public class Calculator {

public static int add(int a, int b) {
return a + b;
}

public static int minus(int a, int b) {
return a - b;
}

public static int division(int a, int b) {
return a / b;
}

public static int remainder(int a, int b) {
return a % b;
}

}
44 changes: 44 additions & 0 deletions src/test/java/study/calculator/CalculatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package study.calculator;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class CalculatorTest {
Calculator calculator;

// 생성자 초기화
@BeforeEach
public void setUp() {
calculator = new Calculator();
}

@Test
public void 더하기() {
assertEquals(5,calculator.add(1,3));
}

@Test
public void 빼기() {
assertEquals(5,calculator.minus(1,3));
}

@Test
public void 나누기() {
assertEquals(5,calculator.division(1,3));
}

@Test
public void 나머지() {
assertEquals(5,calculator.remainder(1,3));
}

// 클래스 null 처리
// 이미 한번 선언한 클래스는 계속 쓰레기 영역에 들어가있기때문임. 메모리 낭비
@AfterEach
public void tearDown() {
calculator = null;
}

}
8 changes: 8 additions & 0 deletions src/test/java/study/calculatorNew/AddStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package study.calculatorNew;

public class AddStrategy implements CalculatorStrategy{
@Override
public int calculator(int a, int b) {
return a + b;
}
}
17 changes: 17 additions & 0 deletions src/test/java/study/calculatorNew/CalculatorNew.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package study.calculatorNew;

import java.util.Scanner;

public class CalculatorNew {

CalculatorStrategy calculatorStrategy;

CalculatorNew(CalculatorStrategy calculatorStrategy) {
this.calculatorStrategy = calculatorStrategy;
}

public int returnResult(int a, int b) {
return calculatorStrategy.calculator(a, b);
}

}
4 changes: 4 additions & 0 deletions src/test/java/study/calculatorNew/CalculatorState.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package study.calculatorNew;

public enum CalculatorState {
}
6 changes: 6 additions & 0 deletions src/test/java/study/calculatorNew/CalculatorStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package study.calculatorNew;

// 전략패턴을 이용하여 객체간의 의존성을 낮춰준다.
public interface CalculatorStrategy {
int calculator(int a, int b );
}
27 changes: 27 additions & 0 deletions src/test/java/study/calculatorNew/CalculatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package study.calculatorNew;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.*;

public class CalculatorTest {

CalculatorNew calculatorNew;

@Test
@DisplayName("정상적으로 add의 값이 동일한지 확인")
void add() {
calculatorNew = new CalculatorNew(new AddStrategy());
assertThat(calculatorNew.returnResult(1,3)).isEqualTo(4);
}

@Test
@DisplayName("정상적으로 곱의 값이 동일한지 확인")
void mut() {
calculatorNew = new CalculatorNew(new MultipleStategy());
assertThat(calculatorNew.returnResult(1,3)).isEqualTo(4);
}

}
Loading