Skip to content

Commit

Permalink
Merge pull request #475 from nramc/404-feature-remove-deprecated-mock…
Browse files Browse the repository at this point in the history
…bean-to-appropriate-mockito-bean

404-feature-remove-deprecated-mockbean-to-appropriate-mockito-bean
  • Loading branch information
nramc authored Jan 26, 2025
2 parents 7a7ab34 + 68b1095 commit 30bb587
Show file tree
Hide file tree
Showing 18 changed files with 84 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class SecurityHeadersCustomizer implements Customizer<HeadersConfigurer<H

@Override
public void customize(HeadersConfigurer<HttpSecurity> headersConfigurer) {
headersConfigurer.permissionsPolicy(permissions -> permissions.policy("accelerometer=(),ambient-light-sensor=(),autoplay=(),battery=(),camera=(),display-capture=(),document-domain=(),encrypted-media=(),fullscreen=(self),gamepad=(),geolocation=(),gyroscope=(),layout-animations=(self),legacy-image-formats=(self),magnetometer=(),microphone=(),midi=(),oversized-images=(self),payment=(),picture-in-picture=(),publickey-credentials-stats=(),speaker-selection=(),sync-xhr=(self),unoptimized-images=(self),unsized-media=(self),usb=(),screen-wake-lock=(),web-share=(),xr-spatial-tracking=()"));
headersConfigurer.permissionsPolicyHeader(permissions -> permissions.policy("accelerometer=(),ambient-light-sensor=(),autoplay=(),battery=(),camera=(),display-capture=(),document-domain=(),encrypted-media=(),fullscreen=(self),gamepad=(),geolocation=(),gyroscope=(),layout-animations=(self),legacy-image-formats=(self),magnetometer=(),microphone=(),midi=(),oversized-images=(self),payment=(),picture-in-picture=(),publickey-credentials-stats=(),speaker-selection=(),sync-xhr=(self),unoptimized-images=(self),unsized-media=(self),usb=(),screen-wake-lock=(),web-share=(),xr-spatial-tracking=()"));

headersConfigurer.referrerPolicy(referrer -> referrer.policy(ReferrerPolicyHeaderWriter.ReferrerPolicy.NO_REFERRER));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.UserDetailsManager;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import java.time.LocalDateTime;
Expand All @@ -39,15 +39,15 @@ class RegistrationUseCaseTest {
.roles(Set.of(Role.AUTHENTICATED_USER))
.build();
@Autowired
private UserDetailsManager userDetailsManager;
UserDetailsManager userDetailsManager;
@Autowired
private Validator validator;
Validator validator;
@Autowired
private PasswordEncoder passwordEncoder;
@MockBean
private AccountActivationUseCase accountActivationUseCase;
@MockBean
private EmailNotificationUseCase emailNotificationUseCase;
PasswordEncoder passwordEncoder;
@MockitoBean
AccountActivationUseCase accountActivationUseCase;
@MockitoBean
EmailNotificationUseCase emailNotificationUseCase;

private RegistrationUseCase registrationUseCase;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.junit.jupiter.Container;
Expand Down Expand Up @@ -53,10 +53,10 @@ static void configureMail(DynamicPropertyRegistry registry) {
}

@Autowired
private MailService mailService;
MailService mailService;

@SpyBean
private JavaMailSender emailSender;
@MockitoSpyBean
JavaMailSender emailSender;

@Test
void context() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.github.nramc.dev.journey.api.web.resources.rest.auth.login;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.nramc.dev.journey.api.config.security.WebSecurityConfig;
import com.github.nramc.dev.journey.api.config.security.InMemoryUserDetailsConfig;
import com.github.nramc.dev.journey.api.config.security.WebSecurityConfig;
import com.github.nramc.dev.journey.api.config.security.WithMockAuthenticatedUser;
import com.github.nramc.dev.journey.api.core.jwt.JwtGenerator;
import com.github.nramc.dev.journey.api.core.jwt.JwtProperties;
Expand All @@ -14,10 +14,10 @@
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.autoconfigure.json.AutoConfigureJson;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

Expand Down Expand Up @@ -47,11 +47,11 @@
@AutoConfigureJson
class LoginResourceTest {
@Autowired
private MockMvc mockMvc;
MockMvc mockMvc;
@Autowired
private ObjectMapper objectMapper;
@MockBean
private UserSecurityAttributeService attributeService;
ObjectMapper objectMapper;
@MockitoBean
UserSecurityAttributeService attributeService;

@Test
void test() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.autoconfigure.json.AutoConfigureJson;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

Expand Down Expand Up @@ -49,9 +49,9 @@ class MultiFactorAuthenticationResourceTest {
"value": "%s"
}""";
@Autowired
private MockMvc mockMvc;
@MockBean
private ConfirmationCodeUseCase confirmationCodeUseCase;
MockMvc mockMvc;
@MockitoBean
ConfirmationCodeUseCase confirmationCodeUseCase;

@Test
void test() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithAnonymousUser;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

Expand All @@ -36,12 +36,11 @@
@WebMvcTest(CreateJourneyResource.class)
@Import({WebSecurityConfig.class, InMemoryUserDetailsConfig.class})
@ActiveProfiles({"prod", "test"})
@MockBean({JourneyRepository.class})
class CreateJourneyResourceTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private JourneyRepository journeyRepository;
@MockitoBean
JourneyRepository journeyRepository;

@BeforeEach
void setup() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithAnonymousUser;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

Expand All @@ -34,12 +34,11 @@
@WebMvcTest(FindJourneyByIdResource.class)
@Import({WebSecurityConfig.class, InMemoryUserDetailsConfig.class})
@ActiveProfiles({"prod", "test"})
@MockBean({JourneyRepository.class})
class FindJourneyByIdResourceTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private JourneyRepository journeyRepository;
MockMvc mockMvc;
@MockitoBean
JourneyRepository journeyRepository;

@Test
@WithMockAuthenticatedUser
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package com.github.nramc.dev.journey.api.web.resources.rest.journeys.update.basic;

import com.github.nramc.dev.journey.api.config.security.WebSecurityConfig;
import com.github.nramc.dev.journey.api.config.security.InMemoryUserDetailsConfig;
import com.github.nramc.dev.journey.api.config.security.WebSecurityConfig;
import com.github.nramc.dev.journey.api.config.security.WithMockAuthenticatedUser;
import com.github.nramc.dev.journey.api.config.security.WithMockGuestUser;
import com.github.nramc.dev.journey.api.repository.journey.JourneyRepository;
import com.github.nramc.dev.journey.api.web.resources.Resources;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithAnonymousUser;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultMatcher;

Expand All @@ -36,7 +36,6 @@
@WebMvcTest(UpdateJourneyBasicDetailsResource.class)
@Import({WebSecurityConfig.class, InMemoryUserDetailsConfig.class})
@ActiveProfiles({"prod", "test"})
@MockBean({JourneyRepository.class})
class UpdateJourneyBasicDetailsResourceTest {
private static final String VALID_REQUEST = """
{
Expand All @@ -52,9 +51,9 @@ class UpdateJourneyBasicDetailsResourceTest {
content().contentType(MediaType.APPLICATION_JSON)
};
@Autowired
private MockMvc mockMvc;
@Autowired
private JourneyRepository journeyRepository;
MockMvc mockMvc;
@MockitoBean
JourneyRepository journeyRepository;

@Test
@WithMockAuthenticatedUser
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithAnonymousUser;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultMatcher;

Expand All @@ -37,16 +37,15 @@
@WebMvcTest(UpdateJourneyGeoDetailsResource.class)
@Import({WebSecurityConfig.class, InMemoryUserDetailsConfig.class})
@ActiveProfiles({"prod", "test"})
@MockBean({JourneyRepository.class})
class UpdateJourneyGeoDetailsResourceTest {
private static final ResultMatcher[] STATUS_AND_CONTENT_TYPE_MATCH = new ResultMatcher[]{
status().isOk(),
content().contentType(MediaType.APPLICATION_JSON)
};
@Autowired
private MockMvc mockMvc;
@Autowired
private JourneyRepository journeyRepository;
MockMvc mockMvc;
@MockitoBean
JourneyRepository journeyRepository;

@Test
@WithMockAuthenticatedUser
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithAnonymousUser;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultMatcher;

Expand All @@ -45,7 +45,6 @@
@WebMvcTest(PublishJourneyResource.class)
@Import({WebSecurityConfig.class, InMemoryUserDetailsConfig.class, ValidationAutoConfiguration.class, JourneyValidator.class})
@ActiveProfiles({"prod", "test"})
@MockBean({JourneyRepository.class})
class PublishJourneyResourceTest {
private static final JourneyEntity JOURNEY_ENTITY = JourneyData.JOURNEY_ENTITY.toBuilder().build();
private static final ResultMatcher[] STATUS_AND_CONTENT_TYPE_MATCH = new ResultMatcher[]{
Expand All @@ -64,11 +63,11 @@ class PublishJourneyResourceTest {
};
private static final Set<Visibility> DEFAULT_VISIBILITY = Set.of(MYSELF);
@Autowired
private MockMvc mockMvc;
MockMvc mockMvc;
@MockitoBean
JourneyRepository journeyRepository;
@Autowired
private JourneyRepository journeyRepository;
@Autowired
private ObjectMapper objectMapper;
ObjectMapper objectMapper;

static Stream<JourneyEntity> validJourneyDataProvider() {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package com.github.nramc.dev.journey.api.web.resources.rest.journeys.update.videos;

import com.github.nramc.dev.journey.api.config.security.WebSecurityConfig;
import com.github.nramc.dev.journey.api.config.security.InMemoryUserDetailsConfig;
import com.github.nramc.dev.journey.api.config.security.WebSecurityConfig;
import com.github.nramc.dev.journey.api.config.security.WithMockAuthenticatedUser;
import com.github.nramc.dev.journey.api.config.security.WithMockGuestUser;
import com.github.nramc.dev.journey.api.repository.journey.JourneyRepository;
import com.github.nramc.dev.journey.api.web.resources.Resources;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithAnonymousUser;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultMatcher;

Expand All @@ -34,7 +34,6 @@
@WebMvcTest(UpdateJourneyVideosDetailsResource.class)
@Import({WebSecurityConfig.class, InMemoryUserDetailsConfig.class})
@ActiveProfiles({"prod", "test"})
@MockBean({JourneyRepository.class})
class UpdateJourneyVideosDetailsResourceTest {
private static final ResultMatcher[] STATUS_AND_CONTENT_TYPE_MATCH = new ResultMatcher[]{
status().isOk(),
Expand All @@ -52,9 +51,9 @@ class UpdateJourneyVideosDetailsResourceTest {
};

@Autowired
private MockMvc mockMvc;
@Autowired
private JourneyRepository journeyRepository;
MockMvc mockMvc;
@MockitoBean
JourneyRepository journeyRepository;

@Test
@WithMockAuthenticatedUser
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.nramc.dev.journey.api.web.resources.rest.users.delete;

import com.github.nramc.dev.journey.api.config.security.WebSecurityConfig;
import com.github.nramc.dev.journey.api.config.security.InMemoryUserDetailsConfig;
import com.github.nramc.dev.journey.api.config.security.WebSecurityConfig;
import com.github.nramc.dev.journey.api.config.security.WithMockAdministratorUser;
import com.github.nramc.dev.journey.api.config.security.WithMockAuthenticatedUser;
import com.github.nramc.dev.journey.api.config.security.WithMockGuestUser;
Expand All @@ -10,12 +10,12 @@
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.context.annotation.Import;
import org.springframework.security.provisioning.UserDetailsManager;
import org.springframework.security.test.context.support.WithAnonymousUser;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean;
import org.springframework.test.web.servlet.MockMvc;

import static com.github.nramc.dev.journey.api.core.domain.user.Role.Constants.AUTHENTICATED_USER;
Expand All @@ -36,9 +36,9 @@
@ActiveProfiles({"prod", "test"})
class DeleteUserResourceTest {
@Autowired
private MockMvc mockMvc;
@SpyBean
private UserDetailsManager userDetailsManager;
MockMvc mockMvc;
@MockitoSpyBean
UserDetailsManager userDetailsManager;

@Test
void context() {
Expand Down
Loading

0 comments on commit 30bb587

Please sign in to comment.