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
JavaBot4mo 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
szatkus4mo ago
It only happens in tests?
Koblížkáč
KoblížkáčOP4mo ago
i think so
szatkus
szatkus4mo ago
Inject a bean of type Environment and inspect property sources there.
Koblížkáč
KoblížkáčOP4mo ago
i cant @autowire into tests if you mean that
szatkus
szatkus4mo 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áčOP4mo ago
do i need to use @springboottest then? i dont want to load the whole configuratiopn though
szatkus
szatkus4mo ago
I would inject those values by constructor.
Koblížkáč
KoblížkáčOP4mo ago
Wdym?
szatkus
szatkus4mo ago
public AuthService(UserService userService, @Value("${notecz.auth.jwt.expire}") Long validFor, @Value("${notecz.auth.jwt.secret}") String secret)
JavaBot
JavaBot4mo 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áčOP4mo 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
JavaBot4mo 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.
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
Koblížkáč
KoblížkáčOP4mo ago
still same error i dont get what you mean by that
szatkus
szatkus4mo ago
Have you provided values for those fields?
Koblížkáč
KoblížkáčOP4mo ago
i mean they should be loaded from the properties file no?
szatkus
szatkus4mo ago
No
Koblížkáč
KoblížkáčOP4mo ago
but thats what i want i dont want to pass them manually
szatkus
szatkus4mo 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áčOP4mo ago
what if i wanted to use spring though
JavaBot
JavaBot4mo 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.
Want results from more Discord servers?
Add your server