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

[20210420] Assertions.assertThatThrownBy, Entity Validation #103

Open
JuHyun419 opened this issue Apr 20, 2021 · 0 comments
Open

[20210420] Assertions.assertThatThrownBy, Entity Validation #103

JuHyun419 opened this issue Apr 20, 2021 · 0 comments
Labels

Comments

@JuHyun419
Copy link
Owner

Assertions.assertThatThrownBy,

  • 예외처리 검증 함수

// pom.xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
</dependency>


// Comment Entity
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.NotBlank;

...
    @Length(max = 1000, message = "댓글은 1000자를 초과할 수 없습니다.")
    @NotBlank(message = "댓글 내용은 비어 있을 수 없습니다.")
    private String content;


// Test
import static org.assertj.core.api.Assertions.assertThatThrownBy;

...

    @DisplayName("[예외] 댓글 내용은 1000자를 넘어서는 안된다.")
    @Test
    void commentIsTooLong() {
        // given
        final String tooLongComment = RandomStringUtils.randomAlphanumeric(1010);

        // when
        CommentCreateRequest request = CommentCreateRequest.builder()
                .content(tooLongComment)
                .lectureId(testLecture.getId())
                .userId(testUser.getId())
                .build();

        // then
        assertThatThrownBy(() -> commentService.createComment(request.toEntity(), request.getUserId(), request.getLectureId()))
                .isInstanceOf(ConstraintViolationException.class)
                .hasMessageContaining("1000자");
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant