Assert.isTrue()는 Spring Framework에서 제공하는 Assertion 유틸리티 클래스인 Assert 클래스의 메소드 중 하나로, 주어진 boolean 표현식이 참(true)인지를 검사하고, 만약 그렇지 않으면 예외를 발생시킨다.

Assert.isTrue() 메소드는 다음과 같은 형태로 사용할 수 있다.

publicstaticvoidisTrue(boolean expression, String message)

여기서 expression은 검사할 "boolean" 표현식이며, message는 예외가 발생했을 때 출력할 "메시지"다.

아래는 Assert.isTrue() 예시 코드.

import org.springframework.util.Assert;

public class Example {
    public void doSomething(int value) {
      //value > 0 <- expression, "value must be greater than 0" <- message
        Assert.isTrue(value > 0, "value must be greater than 0");
  
    }
}

위 코드에서 Assert.isTrue() 메소드를 사용하여 value 변수가 0보다 큰지를 검사한다. 만약 value 변수가 0 이하의 값이라면 IllegalArgumentException 예외가 발생하며, 예외 메시지로는 "value must be greater than 0"가 출력된다.(Custom Error Message를 써도 좋다.)

즉, Assert.isTrue() 메소드는 주어진 조건이 참인지 검사하여, 그렇지 않을 경우 예외를 발생시킴으로써 코드의 안정성과 신뢰성을 높이는 데 사용된다.

true면 아무 메세지가 출력되지 않는다.

'Java' 카테고리의 다른 글

Java Garbage Collection  (0) 2023.06.07
Thread async in Java  (0) 2023.06.02
Wrapper Class  (0) 2023.05.14
05.인스턴스 생성과 힙 메모리  (0) 2023.04.23
04.객체의 속성은 멤버 변수로, 객체의 기능은 메서드로 구현한다.  (0) 2023.04.23

+ Recent posts