-
Spring boot JUnit test 시 field 값 배정하기 ( @Value )카테고리 없음 2022. 10. 7. 20:03
How do I mock an autowired @Value field in Spring with Mockito?
I'm using Spring 3.1.4.RELEASE and Mockito 1.9.5. In my Spring class I have: @Value("#{myProps['default.url']}") private String defaultUrl; @Value("#{myProps['default.password']}") private String
stackoverflow.com
@ExtendWith(MockitoExtension.class) class JsonWebTokenTest { @InjectMocks private JsonWebToken jsonWebToken; @Test @DisplayName("JWT 생성 테스트") void createJsonWebToken() { //given Long id = 1L; ReflectionTestUtils.setField(jsonWebToken, "secretKey", "secretKey"); //when String token = jsonWebToken.createJsonWebTokenById(id); Claims claims = jsonWebToken.getClaimsByToken(token); //then assertNotNull(token); assertThat(claims.get("id", Long.class)).isEqualTo(id); } }
ReflectionTestUtils.setField(jsonWebToken, "secretKey", "secretKey");
해당 bean의 field 명에 원하는 값을 넣는 식으로 작동을 한다.