4월, 2023의 게시물 표시

Spring Boot Admin Server, Client config 설정하기

이미지
 Spring Boot로 많은 프로젝트를 진행하게 됩니다. 많은 모니터링 도구가 있지만, Spring Boot 어플리케이션을 쉽게 모니터링 할 수 있는 방법을 소개하려고 합니다.   코드 중심으로 살펴보겠습니다. 1. 어드민 서버 구축 1-1. 디펜던시 추가 dependencies { // https://mvnrepository.com/artifact/de.codecentric/spring-boot-admin-starter-server implementation 'de.codecentric:spring-boot-admin-starter-server:2.5.4' } 1-2. 어드민 서버 설정 활성화 @SpringBootApplication @EnableAdminServer public class ServerApplication { public static void main (String[] args) { SpringApplication. run (ServerApplication. class, args) ; } } EnableAdminServer를 하면 됩니다. 2. 클라이언트 서버 설정  예제는 book-client, member-client 2가지 클라이언트, member-client가 2개의 인스턴스 실행으로 작성했습니다.  2-1 디펜던시 추가 dependencies { // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web implementation 'org.springframework.boot:spring-boot-starter-web:2.5.4' // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-actuator implementation 'org.spring...

intellij lombok 인식 오류 error

이미지
  요즘 자바 개발에서 lombok 사용이 거의 필수가 되었습니다. intellij에서 롬복관련 자동완성이 안되고 컴파일 에러(실패)가 발생할 때가 있습니다.  (사용 버전은 2020.2.4 입니다. 설치가 된 버전도 있습니다.) 기본 예제 클래스 import lombok. Builder ; @Builder public class Book { private Integer id ; private String title ; } 컴파일 에러 @Test public void test () { Book book = Book. builder ().id( 1 ).title( " 제목 " ).build() ; // 코드 작성 } 테스트 코드를 실행시키면, 실행은 문제없이 됩니다. intellij에서 lombok가 인식되지 못 한 상태인데요. Window File > Settings ( Mac Intellij IDEA > Preferences) 메뉴에서 Plugins를 선택합니다. Lombok을 선택하여 설치(install)하고 재시작하면 완료됩니다. @Test public void test () { Book book = Book. builder ().id( 1 ).title( " 제목 " ).build() ; // 코드 작성 } 해결된 것을 볼 수 있습니다. 실행이 안될 때   혹시 실행이 안 되는 경우가 있습니다. 다음과 같이 해결 할 수 있습니다. Build > Compiler > Annotation Processors 메뉴에서 Enable annotation processing을 선택하면 완료 혹시 더 다른 문제나 방법이 있으면 문의 부탁드려요.

자바 Junit static 함수 Mocking

   지난번에 spring bean을 mocking 하는 코드는 작성했었는데, 이번에는 static함수를 응답을 mocking해보겠습니다. 1. 의존성 주입 testCompile group : 'junit' , name : 'junit' , version : '4.12' testImplementation 'org.mockito:mockito-inline:3.6.0' testImplementation 'org.mockito:mockito-core:3.6.0' testImplementation 'org.easytesting:fest-assert:1.4' static을 mocking하기 위해서는 mockito 3.4버전 이상부터 사용해야 합니다. mockito-inline가 추가되었습니다. (fest-assert는 필수아님) 2. static 함수 클래스 정의 public class AutoIncrement { private static int i = 0 ; public static int getId () { return i ++ ; } } getId를 하면 숫자를 순자적으로 반환하는 함수입니다. 특별한 의미는 없습니다. 3. 테스트 코드 작성 import com.hevia.example.AutoIncrement ; import org.junit. Test ; import org.mockito.MockedStatic ; import org.mockito.Mockito ; import static org.fest.assertions.Assertions.assertThat ; import static org.mockito.BDDMockito.given ; public class MockStaticTest { @Test public void mockStaticTest () { try (MockedStatic<AutoI...