728x90
💡 Stream
컬렉션이나 배열에 저장된 데이터를 처리하기 위한 API이다. 특정 조건에 따라 Stream에서 요소의 하위 집합만 선택할 수 있는 필터링이다. filter() 스트림에 적용할 수 있으며 Stream에는 filter()를 통과하는 요소만 포함되게된다.
테스트 목적에서만 사용해야 하며, 실제 프로그램 코딩에서는 사용해선 안된다.
import java.util.Arra**ys;
import java.util.List**;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
// Create a list of integers
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// Create a stream from the list
Stream<Integer> stream = numbers.stream();
// Use the filter() method to select only the even numbers
Stream<Integer> evenNumbers = stream.filter(x -> x % 2 == 0);
// Print the even numbers to the console
evenNumbers.forEach(System.out::println); // Outputs 2 4 6 8 10
}
}
💡 Assertions(어설션)
프로그램의 특정 지점에서 True일 것으로 예상되는 조건을 지정하는 명령문이다. condition을 테스트하여 False가 나오면 알려준다. assertions을 사용하는 것은 코드의 올바름을 판단하는데 확신을 준다. ‘assertEquals’는 예상 출력이 실제 출력과 일치하는지 여부를 테스트하는 데 사용돤다.
int expected = 5;
int actual = someMethod();
assertEquals(expected, actual);
assertThat 은 어설션과 마찬가지로 JUnit 테스트 프레임워크의 메서드 중 하나이다. 예상값을 매개인수로 사용하고 isEqualTo 같이 예상 값과 동일한지 확인하는 메서드와 함께 사용된다.
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import org.junit.Test;
public class MyTest {
@Test
public void testSomething() {
int actual = doSomething();
int expected = 10;
assertThat(actual, is(equalTo(expected)));
}
}
728x90
'SpringBoot > Java' 카테고리의 다른 글
java 백엔드 개발자 면접 질문 (1) (0) | 2023.06.15 |
---|---|
Java 8 Stream ( 자바 8 스트림, 람다 ) (0) | 2023.06.04 |
22.11.17 Java 백준 1110번 더하기 사이클 (0) | 2022.11.17 |
22.11.09 Java (0) | 2022.11.10 |
22.05.17 족보 29일차 (0) | 2022.05.18 |