junit 활용예시

junit 및 Mockito 활용예시를 정리해보았다.
자주 사용하는것들을 참고하기 위해..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import static org.assertj.core.api.Assertions.assertThat;
public class StringTest {
@Test
public void split() {
String[] values = "1".split(",");
assertThat(values).contains("1");
values = "1,2".split(",");
assertThat(values).containsExactly("1", "2");
}
@Test
public void substring() {
String input = "(1,2)";
String result = input.substring(1, input.length() - 1);
assertThat(result).isEqualTo("1,2");
}

@Test
public void arrayList() {
ArrayList<String> values = new ArrayList<>();
values.add("first");
values.add("second");
assertThat(values.add("third")).isTrue();
assertThat(values.size()).isEqualTo(3);
assertThat(values.get(0)).isEqualTo("first");
assertThat(values.contains("first")).isTrue();
assertThat(values.remove(0)).isEqualTo("first");
assertThat(values.size()).isEqualTo(2);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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 cal;

@BeforeEach
public void setUp() {
cal = new Calculator();
}

@Test
public void 덧셈() {
assertEquals(7, cal.add(3, 4));
}

@Test
public void 뺄셈() {
assertEquals(1, cal.subtract(5, 4));
}

@Test
public void 곱셉() {
assertEquals(6, cal.multiply(2, 3));
}

@Test
public void 나눗셈() {
assertEquals(2, cal.divide(8, 4));
}

@AfterEach
public void tearDown() {
cal = null;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

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 containsTest() {

String data = "1,2";
String[] spitedData = data.split(",");

assertThat(spitedData).contains("1");
}

@Test
void equalTest() {
String data = "1";
String[] spitedData = data.split(",");
assertThat(spitedData).containsExactly("1");
}

@DisplayName("Exception Test")
@Test
void charAtTest() {
String arg = "abcd";
// assertThatExceptionOfType(StringIndexOutOfBoundsException.class).isThrownBy(()->{
// arg.charAt(5);
// });

assertThatThrownBy(()-> {
arg.charAt(5);
}).isInstanceOf(StringIndexOutOfBoundsException.class);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import static org.assertj.core.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;

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

public class SetTest {
private Set<Integer> numbers;

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


@Test
void sizeTest() {
assertThat(numbers.size()).isEqualTo(3);
}

@ParameterizedTest
@ValueSource(ints = {1, 2, 3})
void contains(int num) {
assertTrue(numbers.contains(num));
}

@ParameterizedTest
@CsvSource(value = {"1:true", "2:true","4:false"}, delimiter = ':')
void containsTest2(int num, boolean res) {
assertThat(numbers.contains(num)).isEqualTo(res);
}

}
Share