Junit5 테스트시 객체주입 안되는 에러

2022. 5. 11. 16:46Spring/에러

package springbook.user.service;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.StaticApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestConstructor;
import org.springframework.test.context.junit.jupiter.SpringExtension;

// @TestConstructor(autowireMode = TestConstructor.AutowireMode.ALL) autowired를 생성자 주입 방식으로 만드는 어노테이션.

@ExtendWith(SpringExtension.class)
@ContextConfiguration(locations = "/test-applicationContext.xml")
public class UserServiceTest {
	@Autowired
	UserService userService;
	
	@Test
	public void bean() {
		assertThat(this.userService, is(notNullValue()));
		this.userService.testPrint();
	}
}

 

문제 : import org.junit.Test

수정 : import org.junit.jupiter.api.Test

 

자꾸 빈 객체 생성이 안되길래 autowired문제인줄 알고 autowired가 junit5에서 뭐가 안되는건지 계속 찾아봤다.

원래는 이렇게 오류가 났다.

대충 보자면 assertThat으로 빈 객체가 생성되었는지 ( 비었는지 ) 체크했는데

예상 답 : 안 비었음. | 답 : 비었음

이렇게 뜨는것이었다 .. 계속 검색하다보니

나왔는데 https://m.blog.naver.com/passionisall/222087832863에 나와있었다.

문제가 된 라이브러리는 Junit4에서 지향하는 라이브러리이고 ( 4, 5 둘다 되긴 한다 )

수정된 라이브러리는 Junit5에서 지향하는 라이브러리였다.