javaspring-bootspring-data-jpaspring-cloudspring-cloud-vault-config

Data Jpa Test fails to load properties


I am using Hashi Corp vault in Spring Boot project. I am able to run the project without any issue. But when I run unit tests, secret-id and role-id are not being passed. I tried the following but got an exception saying both are empty. Tried hard coding the values, that didn't work either

EmployeeTest.java

@RunWith(SpringRunner.class)
@DataJpaTest
@ActiveProfiles(value = "ide")
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class EmployeeTest
{
    private final Logger logger= LoggerFactory.getLogger(getClass());

    @Autowired
    EmployeeRepository employeeRepository;

    @Test
    public void getEmployeeById()
    {
        Employee employee=employeeRepository.getOne(13L);
        logger.info(employee.toString());
    }
}

Update: I am able to pass secret-id and role-id through VM arguments but still properties are not resolving


Solution

  • Okay, it turns out when using profile from src/main/resources/application-ide.yml in spring boot test, properties are not being replaced by vault vaules. Copying the same file to src/test/resources/application-ide.yml fixes the issue.

    TL;DR For Spring Boot testing always better to use properties file from src/test/resources rather than src/main/resources