How to make spring boot @Value fields inject the values from properties?

@ExtendWith(MockitoExtension.class)
@TestPropertySource("classpath:application-test.properties")
class AuthServiceTests {

@Mock
private UserService userService;

@InjectMocks
private AuthService authService;

@Test
void testRegisterUser() {
when(userService.existsByEmail("[email protected]")).thenReturn(false);
when(userService.existsByEmail("[email protected]")).thenReturn(true);

assertThat(authService.register("username", "[email protected]", "Password1"))
.isNotNull();
assertThatThrownBy(() -> authService.register("username", "[email protected]", "Password1"))
.isInstanceOf(UserAlreadyExistsException.class);
}
}
@ExtendWith(MockitoExtension.class)
@TestPropertySource("classpath:application-test.properties")
class AuthServiceTests {

@Mock
private UserService userService;

@InjectMocks
private AuthService authService;

@Test
void testRegisterUser() {
when(userService.existsByEmail("[email protected]")).thenReturn(false);
when(userService.existsByEmail("[email protected]")).thenReturn(true);

assertThat(authService.register("username", "[email protected]", "Password1"))
.isNotNull();
assertThatThrownBy(() -> authService.register("username", "[email protected]", "Password1"))
.isInstanceOf(UserAlreadyExistsException.class);
}
}
authService contains 2 fields:
@Value("${notecz.auth.jwt.expire}")
private Long validFor;
@Value("${notecz.auth.jwt.secret}")
private String secret;
@Value("${notecz.auth.jwt.expire}")
private Long validFor;
@Value("${notecz.auth.jwt.secret}")
private String secret;
both of them are set to null for some reason. any idea why? applicaiton test properties indeed contains the 2 pairs
No description
22 Replies
JavaBot
JavaBot6mo ago
This post has been reserved for your question.
Hey @Koblížkáč! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
szatkus
szatkus6mo ago
It only happens in tests?
Koblížkáč
KoblížkáčOP6mo ago
i think so
szatkus
szatkus6mo ago
Inject a bean of type Environment and inspect property sources there.
Koblížkáč
KoblížkáčOP6mo ago
i cant @autowire into tests if you mean that
szatkus
szatkus6mo ago
Wait, the test is not under any Spring context. So @TestPropertySource won't work. Actually you don't even have any Spring beans. Everything is done by Mockito.
Koblížkáč
KoblížkáčOP6mo ago
do i need to use @springboottest then? i dont want to load the whole configuratiopn though
szatkus
szatkus6mo ago
I would inject those values by constructor.
Koblížkáč
KoblížkáčOP6mo ago
Wdym?
szatkus
szatkus6mo ago
public AuthService(UserService userService, @Value("${notecz.auth.jwt.expire}") Long validFor, @Value("${notecz.auth.jwt.secret}") String secret)
JavaBot
JavaBot6mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Koblížkáč
KoblížkáčOP6mo ago
it passes null still though @tjoener sorry for pinging, but i know you knew how my problems could be fixed, do you maybe have an idea why is this happening?
JavaBot
JavaBot6mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
imp_o_rt
imp_o_rt6mo ago
you're using MockitoExtension so spring context is not starting try using SpringExtension this test seems a little confused - if you're using mocks you probably don't need to use spring (and vice versa)?
Koblížkáč
KoblížkáčOP6mo ago
still same error i dont get what you mean by that
szatkus
szatkus6mo ago
Have you provided values for those fields?
Koblížkáč
KoblížkáčOP6mo ago
i mean they should be loaded from the properties file no?
szatkus
szatkus6mo ago
No
Koblížkáč
KoblížkáčOP6mo ago
but thats what i want i dont want to pass them manually
szatkus
szatkus6mo ago
Well, you don't use Spring in this test, so you can't count on its mechanics. And loading properties from a file is trival, so it's not much of a headache.
Koblížkáč
KoblížkáčOP6mo ago
what if i wanted to use spring though
JavaBot
JavaBot6mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?