2

I created one folder src/test/resources and added one file Test.json and trying to access in junit test like this

@RunWith(SpringRunner.class)
@WebMvcTest(value = ReadController.class, secure = false)
public class ReadControllerTest {

    @Value("${classpath:Test.json}")
    Resource testFile;

    @Test
    public void test() throws Exception{


        File file = testFile.getFile();
        System.out.println(file.exists());

    }   
}

When I run this unit test, it tries to find file in classpath but file is not present there. How to add this file in classpath?

3
  • try File file = ResourceUtils.getFile(this.getClass().getResource("/some_file.txt")); to read the json file in test/resources folder Commented Aug 16, 2018 at 7:57
  • Doesn't seem to work. Commented Aug 16, 2018 at 8:41
  • It worked for me. can you share the code which you tried with my suggestion. Please don't forget to mention "/" at the beginning of the filename. Commented Aug 16, 2018 at 17:31

1 Answer 1

0

By Default, spring boot classpath will be pointed to src/main/resources but you are using src/test/resources. Try to copy the JSON file to src/main/resources.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.