본문 바로가기

개발 Note/SPRING

[SPRING] Spring boot - 외부 jar 추가 후 테스트하기

해시 알고리즘(자바)을 jar로 만들고 프로젝트에 라이브러리 추가하여 구동하고 싶었다.

 

1. 모듈화할 Class 작성

 

사용자로부터 문자열을 byte 배열로 전환하여 이 데이터와 hash 전환할 타입(MD4, MD6, SHA-1, SHA-126...)을 파라미터로 받고 해시로 전환해주는 Util 클래스를 작성하였다.

 

2. JAR 배포

자바 프로젝트 우클릭 -> Export -> JAR file 선택 -> 경로 지정 후 저장

 

 

 

 

3. JAR를 추가하고자 하는 프로젝트 설정(Spring Boot , Gradle 기준)

 

1) 프로젝트에 libs 폴더를 추가하고(root 경로) 방금 만든 JAR를 추가한다.

 

 

2) build.gradle 세팅

 

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-batch'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation fileTree(dir: 'libs', include:['*.jar']) // jar 파일 추가
    providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.springframework.batch:spring-batch-test'
}

 

※ 참고

하나의 파일만 등록하는 경우

 

dependencies {
    implementation files('libs/hash_algorithm.jar')
}

 

 

4. 테스트(JUnit 테스트)

참고로 자바 파일 자체를 테스트 할 때는 (여러 방법이 있지만)

나같은 경우 테스트 프로젝트를 따로 만들어 테스트할 프로젝트를 Java Build Path -> Projects 에 추가하고

main에서 import하여 테스트한다.

사실상 이미 테스트를 거쳐 만든 Class이기 때문에 따로 Spring Boot에서 또 테스트를 할 필요는 없지만

이후 Spring Boot에서 단위테스트, 통합테스트 방법을 업로드 할 예정이라.. 맛보기로 붙여본다.

 

import java.util.Arrays;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import (JAR 경로);

@SpringBootTest
class Tests {

	@Test
	public void hash() {
            HashUtil has = new HashUtil();

            String str = "123456789a";
            byte[] arr = str.getBytes();

            try {
            	System.out.println(has.getHashString(arr, "SHA-256"));
            } catch (Exception e) {
            // TODO Auto-generated catch block
            	e.printStackTrace();
            }
		
	}

}

 

@SpringBootTest는 통합 테스트이다.

Spring Boot에서는 MVC 테스트, 단위테스트(Mockito), 통합테스트(SpringBootTest)를 지원하는데

요거는 다음에 자세히 포스팅 하도록 하자.

 

그나저나 tistory는 왤케 코드블럭 들여쓰기가 자기 맘대로지...?ㅠㅠ

ㅎ..ㅎ tistory도 어렵다.

'개발 Note > SPRING' 카테고리의 다른 글

[SPRING] Spring AOP  (0) 2022.06.14
[SPRING] Spring Boot 단위 테스트 (1) - Controller  (0) 2022.04.26
[SPRING] Spring Security  (0) 2022.03.26