I have this test:
@ExtendWith(SpringExtension.class)
@WebMvcTest(AuthController.class)
@TestPropertySource("classpath:application.properties")
class AuthControllerTest {
    @Autowired
    private MockMvc mvc;
    @Autowired
    AuthTokenFilter authTokenFilter;
    @MockBean
    AuthEntryPointJwt authEntryPointJwt;
    @MockBean
    JwtUtils jwtUtils;
    @Autowired
    private ObjectMapper objectMapper;
    @MockBean
    UserDetailsServiceImpl userDetailsServiceImpl;
    @MockBean
    AuthenticationManager authenticationManager;
    @MockBean
    Authentication authentication;
    @MockBean
    SecurityContext securityContext;
    @Test
    void test1withEnabledTrue() {
    }
    @Test
    void test2WithEnabledTrue() {
    }
    @Test
    void cannotRegisterUserWhenRegistrationsAreDisabled() throws Exception {
        
        var userToSave = validUserEntity("username", "password");
        var savedUser = validUserEntity("username", "a1.b2.c3");
        when(userDetailsServiceImpl.post(userToSave)).thenReturn(savedUser);
        mvc.perform(post("/api/v1/auth/register/").contentType(MediaType.APPLICATION_JSON)
                .content(objectMapper.writeValueAsBytes(userToSave))).andExpect(status().isCreated())
        .andExpect(jsonPath("$.status", is("registrations are disabled")));
    }
    private static UsersEntity validUserEntity(String username, String password) {
        return UsersEntity.builder().username(username).password(password).build();
    }
}
And this is the relevant part in Controller (Class Under Test):
@Value("${app.enableRegistration}")
private Boolean enableRegistration;
private Boolean getEnableRegistration() {
    return this.enableRegistration;
}
@PostMapping("/register")
@ResponseStatus(HttpStatus.CREATED)
public Map<String, String> post(@RequestBody UsersDTO usersDTO) {
    Map<String, String> map = new LinkedHashMap<>();
    if (getEnableRegistration()) {
        [...]
        map.put("status", "ok - new user created");
        return map;
    }
    map.put("status", "registrations are disabled");
    return map;
}
I have this application.properties under src/test/resources and I need to override it, only for my single test named cannotRegisterUserWhenRegistrationsAreDisabled
app.enableRegistration=true
Probably I could use another file "application.properties" and another class test, but I'm looking for a smarter solution.
 
     
     
    