'Can't test microservice with JUnit

I'm testing a microservice that writes into a file with JUnit. The problem is that I get a java.io.FileNotFoundException: DecryptService/src/main/resources/KEY_DECR/KeyStorage/provaChiave (File or directory not found), but when using the function not in the test everything works fine. I think that the problem might be that the location of the file that I want to write is in another microservice (DecryptService). How can I solve it?

This is the KeysMicroservice

@RequestMapping(value="/insertKey",method= RequestMethod.POST)
    public ResponseEntity<String> post(@RequestBody String json){
    try 
        (FileWriter file = new FileWriter("DecryptService/src/main/resources/KEY_DECR/KeyStorage/"+result.get("keyName"))) {
            file.write(result.get("json"));
            file.flush();
    } catch (IOException e) {
            e.printStackTrace();
            return new ResponseEntity<>("Upload failed", HttpStatus.OK);
    }
}

And this is how I'm testing it:

    @Test
    public void correctKey() {
        JSONObject json = new JSONObject();
        try {
            json.put("username", "luca");
            json.put("keyName","provaChiave");
            json.put("json", "--- value ---");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String expectedResult ="<200 OK OK,Upload completed,[]>";
        assertEquals(expectedResult,String.valueOf(insertRequest.post(json.toString())));
    }

Thank you in advance.



Sources

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

Source: Stack Overflow

Solution Source