我一直在尝试使用Selenium运行与REST API相关的一些测试。根据下面的代码,我需要将Float值转换为String。
当我使用String.valueOf(float arg)时,我得到了异常 - java.lang.ClassCastException:java.lang.Float无法强制转换为[C
码:
@Test
public void Test01()
{
Response r=given().parameter("id", "someID").parameter("appid", "someID").when().get("someURL");
String lon=String.valueOf(r.then().contentType(ContentType.JSON).extract().path("coord.lon"));
System.out.println(lon);
}
预期:价值转换必须成功。即浮点数 - >字符串
实际:失败:Test01
java.lang.ClassCastException: java.lang.Float cannot be cast to [C
at APITestingProject.com.org.api.TransferingAPIResponse_2.Test01(TransferingAPIResponse_2.java:19)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:643)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:782)
at org.testng.TestRunner.run(TestRunner.java:632)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
at org.testng.SuiteRunner.run(SuiteRunner.java:268)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
at org.testng.TestNG.run(TestNG.java:1064)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
更改代码的这一部分
String lon=String.valueOf(r.then().contentType(ContentType.JSON).extract().path("coord.lon"));
需要使用Float.toString()方法
String lon=Float.toString(r.then().contentType(ContentType.JSON).extract().path("coord.lon"));
如果需要更多信息,请访问该网站http://javadevnotes.com/java-float-to-string-examples