在 Junit 中验证 URL 的值?

TP_*_*AVA 2 java junit json

我必须为返回 URL 的方法编写单元测试用例,我必须验证 URL 中参数的所有值,是否有任何 API 或更好的方法来解决此问题。

任何建议,将不胜感激。

Java方法:

 class Sample{
      public String returnURL(){
          return "http://localhost:9080/sample?a=12-a&b=param2&c=param3&d=param_4&e=param5"
        }
}
Run Code Online (Sandbox Code Playgroud)

朱尼特:

@Test
public void returnURLTest(){
  String url = new Sample().returnURL()
  // Parse the url 
  assert url['a'] == "param1"
 assert url['b'] == "param2"
}
Run Code Online (Sandbox Code Playgroud)

谢谢

小智 5

如果您使用 AssertJ,您可以创建 URI(或 URL)对象并在其上使用断言,如以下示例所示:https : //github.com/joel-costigliola/assertj-examples/blob/master/assertions-示例/src/test/java/org/assertj/examples/UriAssertionsExamples.java

assertThat(new URI("http://www.helloworld.org/index.html?happy=very")).hasParameter("happy", "very");
Run Code Online (Sandbox Code Playgroud)