Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Junit中验证URL的值?_Java_Json_Junit - Fatal编程技术网

Java 在Junit中验证URL的值?

Java 在Junit中验证URL的值?,java,json,junit,Java,Json,Junit,我必须为返回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" } }

我必须为返回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"
        }
}
朱尼特:

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

谢谢

您只需解析URL并比较您提取的各个片段

比如:

int indexOfNextParameter = url.indexOf("=");
List<String> parameters = new ArrayList<>();

while(youAreParsingTheURL){
    int indexOfParameter = url.indexOf("=", indexOfNextParameter);
    String parameter = url.substring("=", indexOfParameter);
    parameters.add(parameter);
}
int indexOfNextParameter=url.indexOf(“=”);
列表参数=新的ArrayList();
当(你在巴黎){
int indexOfParameter=url.indexOf(“=”,indexOfNextParameter);
字符串参数=url.substring(“=”,indexOfParameter);
参数。添加(参数);
}

然后只需浏览参数列表并验证每个项目。

如果使用AssertJ,则可以创建URI(或URL)对象并使用其上的断言,如这些示例所示:


发布一些代码,也许有人能提供更多帮助。查看并张贴相关代码;你的测试代码。或者至少把你的情况公布在你的测试中
assertThat(new URI("http://www.helloworld.org/index.html?happy=very")).hasParameter("happy", "very");