I have a Bean that is reading a file at initialization. The file is environment specific and is generated during the installation of the system. The path of the file is hardcoded as a final static variable in the code.
private static final String FILE_PATH = "/etc/project/file.path";
private String decryptedPassword = "";
@Autowired
public ClassToBeTested(@Value("${pass}") String encryptedPassword)
{
String decryptedPassword = StaticClass.decrypt(encryptedPassword, FILE_PATH);
}
I need to somehow mock this file in the JUnit tests so that I can test the rest of the functionalities. The @Before annotation is not useful because even that is run after initialization of the Beans according to my tests.
One very dirty way that can be used is to add another parameter to the Autowired function that can indicate if the call is for a unit test or not. But this is really not a clean way to do this. For example:
private static final String FILE_PATH = "/etc/project/file.path";
private String decryptedPassword = "";
@Autowired
public ClassToBeTested(@Value("${pass}") String encryptedPassword,
@Value("${isTest}") boolean isTest)
{
if (isTest)
decryptedPassword = encryptedPassword;
else
decryptedPassword = StaticClass.decrypt(encryptedPassword, FILE_PATH);
}
Any ideas how I can mock the file at FILE_PATH so that I do not have to use this workaround or force a property in the Autowired function without changing the Bean constructor?
Aucun commentaire:
Enregistrer un commentaire