lundi 19 mars 2018

Java Modify a property to perform a @Test for code coverage

Performing some Test in Java I got two problems when I test the function to obtain a file from a folder, the route is placed in the application.properties:

The first problem is that @Test do not recognize the value extract from ${routing.folder}, it does not happen executing and doing debug to the code, so I let it commented and copy directly its value. Here the Test and the class:

SepaRoutingFromXMLTest.java :

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {SepaRoutingUtils.class})
public class SepaRoutingFromXMLTest {

    @Autowired
    SepaRoutingUtils sepa;

    @Test
    public void existValidOneFullXMLFileInFolder() throws Exception {
        SepaRoutingUtils sepa = new SepaRoutingUtils(); 
        assertThat(sepa.readSepaXMLFile(), containsString(".xml"));
        assertThat(sepa.readSepaXMLFile(), containsString("SEPAROUTING_V3_FULL_"));
    }

}

SepaRoutingUtils.java :

@Component
public class SepaRoutingUtils {

    // @Value("${routing.folder}")
    // private String SEPA_FOLDER
    private final String SEPA_FOLDER = "../../separouting";

    private File readSepaFolder() {

        File folder = new File(SEPA_FOLDER);
        if(!folder.isDirectory()) {
            throw new FolderAccessDeniedException();
        }

        return folder;
    }

    public String readSepaXMLFile() {

        try {
            return Utils.prepareXMLFile(readSepaFolder(), SepaRoutingFileType.FULL).getName();
        }
        catch (ParseException e) {
            e.printStackTrace();
            throw new NotValidFileException();      
        }

    }

}

The function Utils.prepareXMLFile returns me the file I need, it validates the file name and only takes the most updated.

The second problem is I cannot do something like @Test(expected = FolderAccessDeniedException.class) because the value comes from the properties and this can not be modify from the @Test. It would always be correct or always incorrect.

So, I don't know how to make it fails when for example a route does not exist or is not accessible. I am thinking in change the function to pass the folder route as its property readSepaFolder(String folderName) however, I would not like to leave it that way.

Aucun commentaire:

Enregistrer un commentaire