'How to override a single application property in a test

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.



Solution 1:[1]

I think what you are looking for is the @TestProperty annotation, which was an answer of a question on Stackoverflow here. However this only works on class level, not for one test only.

You probably need to make a new test class and add the tests where it the value needs to be false.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Doompickaxe