실습1.
[Production]
void sortArr(int[])
: int[]을 매개변수로 받아 배열의 내용을 오름차순으로 정렬하는 메소드 작성
[Test]
@Test
public void test1()
: 5개의 테스트 데이터를 준비하여 sortArr 결과,
배열이 오름차순이 되었으면 통과
실습2.
[Production]
int max(int[])
int min(int[])
: int[]을 매개변수로 받아 배열안에서 가장 큰 값과 가장 작은 값을 리턴
[Test]
@Test
public void test2()
: 테스트 실행하기 전에 /TEST 폴더 생성,
3개의 테스트 데이터로 두 메소드 테스트 진행,
테스트 통과화면 위 폴더에 'report.txt'란 이름의 텍스트 파일 생성,
최대값과 최소값을 저장해보기
실습3.
[Production]
String toNumber(String)
: String을 매개변수 받아 숫자를 제외한 문자열을 제거하여 리턴
ex) "010-4537-2233" -> "01045372233"
[Test]
@Test
public void test3()
: @Parameters를 사용하여 3개의 테스트 데이터로 테스트 수행하기
1. 내 코드
** [src/main/java] com.mvn.junitTest12.java
package com.mvn.junitTest12;
/**
* Hello world!
*
*/
public class App {
public static void main( String[] args ) {
App binna = new App();
int[] sortNeed = {2, 5, 3, 1};
binna.sortArr(sortNeed);
System.out.print("오름차순 : ");
for (int i = 0; i < sortNeed.length; i++) {
System.out.print(sortNeed[i] + " ");
}
System.out.println();
System.out.println("max : " + binna.max(sortNeed));
System.out.println("min : " + binna.min(sortNeed));
System.out.println("toNumber() : " + binna.toNumber("010-4537-2233"));
}
// 실습 1
// int[]을 매개변수로 받아 배열의 내용을 오름차순으로 정렬하는 메소드 작성
public void sortArr(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
if(arr[i] > arr[j]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
} // end sortArr()
// 실습 2
public int max(int[] arr) {
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if(max < arr[i]) {
max = arr[i];
}
}
return max;
} // end max()
public int min(int[] arr) {
int min = arr[0];
for (int i = 1; i < arr.length; i++) {
if(min > arr[i]) {
min = arr[i];
}
}
return min;
}
// 실습3
public String toNumber(String str) {
String result = "";
for (int i = 0; i < str.length(); i++) {
char temp = str.charAt(i);
if(48 <= temp && temp <= 57) {
result += temp + "";
}
}
return result;
} // end toNumber()
}
** [src/test/java] com.mvn.junitTest12 > AppTest.java
package com.mvn.junitTest12;
import static org.junit.Assert.fail;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.junit.Test;
public class AppTest {
private App app = new App();
// 실습 1
@Test
public void test1() {
// 테스트할 int 배열 만들기
int[] testArr = {1, 5, 3, 7, 2};
// 만약 정상적으로 sort 되었다면 나올 결과값
int[] resultArr = {1, 2, 3, 5, 7};
// 검사하고자하는 메서드 실행
app.sortArr(testArr);
for (int i = 0; i < testArr.length; i++) {
if(testArr[i] != resultArr[i]) {
fail();
}
}
}
// 실습 2
@Test
public void test2() {
// 테스트할 int 배열 만들기
int[] testArr = {1, 5, 3, 7, 2};
// 검사값
int resultMax = 7;
int resultMin = 1;
// 검사하고자하는 메서드 실행, 리턴값 담기
int max = app.max(testArr);
int min = app.min(testArr);
if(max == resultMax && min == resultMin) {
try {
OutputStream out = new FileOutputStream("test/report.txt");
String str = "최대값 : " + max + ", 최소값 : " + min;
byte[] by = str.getBytes();
out.write(by);
} catch (Exception e) {
e.getStackTrace();
}
} else {
fail();
}
}
}
** [src/test/java] com.mvn.junitTest12 > AppTest3.java
package com.mvn.junitTest12;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(value = Parameterized.class)
public class AppTest3 {
private String expected;
private String value;
@Parameters
public static Collection<String[]> getTestParameters() {
return Arrays.asList(new String[][] {
{"01012345678", "010-1234-5678"},
{"01012121212", "010-1212-1212"},
{"01099787788", "010-9978-7788"},
{"01095143578", "010-9514-3578"}
});
}
public AppTest3(String expected, String value) {
super();
System.out.println("AppTest3() 생성");
this.expected = expected;
this.value = value;
}
// 실습3
@Test
public void test3() {
App app = new App();
assertEquals(expected, app.toNumber(value));
System.out.println("테스트 통과: " + expected + ", " + value);
}
}
2. 강사님 코드
** [src/main/java] com.mvn.junitTestTeacher > App.java
package com.mvn.junitTestTeacher;
import java.util.Arrays;
/**
* Hello world!
*
*/
public class App
{
// 실습 1
public void sortArr(int [] arr) {
Arrays.sort(arr);
}
// 실습 2
public int max(int [] arr) {
int max = arr[0];
for (int i = 1 ; i<arr.length; i++){
if(max < arr[i]){
max = arr[i];
}
}
return max;
}
public int min(int [] arr) {
int min = arr[0];
for (int i = 1 ; i<arr.length; i++){
if(min > arr[i]){
min = arr[i];
}
}
return min;
}
// 실습3
public String toNumber(String str) {
String result = str.replaceAll("[^0-9]", "");
return result;
}
}
** [src/test/java] com.mvn.junitTestTeacher > AppTest.java
package com.mvn.junitTestTeacher;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
public class AppTest
{
private int [][] test = null;
App app = null;
// 저장경로
static String path = System.getProperty("user.dir") + File.separator + "TEST";
@Before
public void preTest() {
// 5개의 테스트 데이터
test = new int[][] {
{4, 1, 5, 7, -2},
{10, 5, 9, 7, 6},
{100, 600, 300, 200, 100},
{88, 55, 33, 22, 44},
{-4, -1, -5, -3, -1}
};
app = new App();
}
// 실습1
@Test
public void test1() {
int [][] expected = {
{-2, 1, 4, 5, 7},
{5, 6, 7, 9, 10},
{100, 100, 200, 300, 600},
{22, 33, 44, 55, 88},
{-5, -4, -3, -1, -1}
};
for(int i = 0; i < test.length; i++) {
app.sortArr(test[i]);
if(!Arrays.equals(test[i], expected[i]))
fail();
}
}
// 실습2
@Test
public void test2() {
int [][] expected = {
{-2, 7},
{5, 10},
{100, 600},
{22, 88},
{-5, -1}
};
// 테스트
for(int i = 0; i < test.length; i++) {
assertEquals(expected[i][0], app.min(test[i]));
assertEquals(expected[i][1], app.max(test[i]));
}
// 테스트 후 결과 파일 저장
try(
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(new File(path, "report.txt"))));
){
for(int i = 0; i < test.length; i++) {
out.println(app.min(test[i]) + " " + app.max(test[i]));
}
} catch (Exception e) {
e.printStackTrace();
}
}
@BeforeClass
public static void init() {
// 테스트 실행하기 전에 폴더 생성
File f = new File(path);
if(!f.exists()) {
if(f.mkdir()) {
System.out.println("폴더 생성");
}
}
}
}
** [src/test/java] com.mvn.junitTestTeacher > AppTest3.java
package com.mvn.junitTestTeacher;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(value = Parameterized.class)
public class AppTest3 {
String expected;
String value;
@Parameters
public static Collection<String[]> getTestParameters(){
return Arrays.asList(new String[][] {
{"01011112222", "010-1111-2222"}, // expected, value
{"025864722", "02)586-4722"},
{"20200707", "2020년 07월 07일"}
});
}
public AppTest3(String expected, String value) {
this.expected = expected;
this.value = value;
}
// 실습3
@Test
public void test3() {
App app = new App();
assertEquals(expected, app.toNumber(value));
}
}
'웹_프론트_백엔드 > JAVA프레임윅기반_풀스택' 카테고리의 다른 글
2020.07.09 (0) | 2020.07.09 |
---|---|
2020.07.08 (0) | 2020.07.08 |
2020.07.07 (0) | 2020.07.07 |
2020.07.06 (0) | 2020.07.06 |
2020.07.06 (0) | 2020.07.06 |