Mocking a URL in Java

前端 未结 10 1693
臣服心动
臣服心动 2020-12-14 08:16

We have a URL object in one of our Java classes that we want to mock, but it\'s a final class so we cannot. We do not want to go a level above, and mock the InputStream beca

10条回答
  •  温柔的废话
    2020-12-14 08:27

    Create a URL-object pointing to the test class itself.

    final URL url = 
        new URL("file://" + getClass().getProtectionDomain().getCodeSource().getLocation().getPath());
    

提交回复
热议问题