Java Modify a property to perform a @Test

0

By doing some Test in Java I find two problems when trying the function to obtain a file from a folder that is in the properties of the application:

The first problem is that when doing the @Test it does not recognize the value extracted from ${routing.folder} , something that does not happen to me executing and debugging the code, that's why I left it commented and directly copied its value. Next I share 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 Utils.prepareXMLFile function returns the file I need to process it, validates among other things the file name and only takes the most updated one.

The second drawback is that if I wanted something like @Test(expected = FolderAccessDeniedException.class) I could not test it because it is assumed that the value is taken from the properties and this value simply can not be modified from @Test . And it would always be right or wrong.

Then I do not know how to make it fail when, for example, a route does not exist or is not accessible. I have thought about changing the function so that the path of the folder is passed to the readSepaFolder() function, however I would not like to leave it that way.

    
asked by AndreFontaine 19.03.2018 в 15:27
source

1 answer

0

The SepaRoutingUtils class that you are passing as a parameter in @ContextConfiguration(classes = {SepaRoutingUtils.class}) is not a configuration class. It is a Bean.

If you want a different context for tests, you should pass as a parameter the class that contains the configuration (normally annotated @Configuration ). If you want to use the same context in the tests as in the normal execution of the application you can use the annotation @ContextConfiguration without parameters.

    
answered by 19.03.2018 в 16:07