간단한 JUnit 테스트를 진행해보자.
https://standout.tistory.com/378
junit 4.7 이란?
단위테스트 프레임워크, 코드수행결과를 확인해 코드의 정확성/신뢰성을 검증하는 기능 pom.xml에 추가 junit junit 4.7 test https://mvnrepository.com/artifact/junit/junit/4.7
standout.tistory.com
https://standout.tistory.com/598
전자정부프레임워크 Eclipse 웹프로젝트 만들기
전자정부프레임워크 웹프로젝트 예제와 같이 이미 만들어진 웹프로젝트. 간단한 게시판만들기에 빠르고 적합하다. 전자정부프레임워크 Eclipse 다운로드 후 진행한다. https://standout.tistory.com/591
standout.tistory.com
생성한 프로젝트에 아무 java 파일이라도 좋다, 해당 파일에
Controller에 메소드 하나를 추가한다.
public String hiSanghee(String name) {
return name + "가 작성했습니다.";
}
프로젝트 우클릭 - properties - Java Build Path - add library - JUnit - next - finish



JUnit 추가된것을 확인 후 apply and close

원하는 controller 우클릭 - new - new junit test case - browse를 통해 controller - method를 선택해 자동생성할 수 있다.




아래 게시물을 참고하여
생성된 class 파일으로 간단한 JUnit 테스트를 진행해보자.
https://standout.tistory.com/1089
JUnit: 자바용 유닛 테스트 프레임워크
JUnit 자바용 유닛 테스트 프레임워크 메서드, 클래스, 모듈이 의도한대로 작동하는지를 검증하는 프로세스 테스트 결과를 단순한 텍스트로 남기는 것이 아니라 Test 클래스로 남겨 개발자에게 테
standout.tistory.com
package egovframework.example.sample.web;
import static org.junit.Assert.assertEquals;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class AddSampleTest {
// 값 세팅
String name = "sanghee";
@BeforeClass
public static void beforeTest() {
System.out.println("@BeforeClass: Test 실행 전에 실행되는 메소드입니다.");
}
@AfterClass
public static void afterTest() {
System.out.println("@AfterClass: Test 실행 후에 실행되는 메소드입니다.");
}
@Test
public void testHiSanghee() throws Exception {
// 값 부여
this.name = name;
// Arrange
EgovSampleController controller = new EgovSampleController();
// Act
String result = controller.hiSanghee(name);
// 결과 확인
assertEquals(name, "sanghee");
System.out.println("assertEquals() name 값이 sanghee임을 확인했습니다.");
// 검증
System.out.println("hiSanghee() RETURN 문자열: " + result);
}
}
프로젝트 우클릭 - run as - junit test

결과 및 console창 확인

완료.
'JAVA > Spring' 카테고리의 다른 글
DbUnit 테스트: dataSource를 불러와 insert/select 확인하기 (0) | 2023.12.04 |
---|---|
EasyMock + JUnit 테스트 @Before @Test (0) | 2023.11.30 |
MVC패턴 게시판 구현하기: 페이징 (0) | 2023.11.28 |
MVC패턴 게시판 구현하기: 검색하기 (0) | 2023.11.28 |
MVC패턴 게시판 구현하기: 삭제하기 (0) | 2023.11.28 |