java 断言失败,即使它不应该

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17200203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:17:29  来源:igfitidea点击:

Assert Fails Even Though It Shouldn't

javaselenium-webdrivertestng

提问by DarthOpto

I have the following two asserts which are checking the checkedattribute of a check box:

我有以下两个断言正在检查checked复选框的属性:

Assert.assertEquals(true, notificationCheck.getAttribute("checked").equals(true));
Assert.assertEquals(true, accessCheck.getAttribute("checked").equals(true));

I have setup a sort of debugging where it spits out to console the value of the checkedattribute and they both say true.

我已经设置了一种调试方式,它会输出来控制checked属性的值,并且它们都说 true。

The error that I am getting is below:

我得到的错误如下:

java.lang.AssertionError: expected [false] but found [true]
at org.testng.Assert.fail(Assert.java:94)
at org.testng.Assert.failNotEquals(Assert.java:494)
at org.testng.Assert.assertEquals(Assert.java:123)
at org.testng.Assert.assertEquals(Assert.java:286)
at org.testng.Assert.assertEquals(Assert.java:296)
at ui_Tests.ParticipantsPage_AddParticipant.participantPage_AddParticipant(ParticipantsPage_AddParticipant.java:305)
at ui_Tests.ParticipantsPage_AddParticipant.test_participantPage_AddParticipant_FF(ParticipantsPage_AddParticipant.java:315)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

What am I doing wrong?

我究竟做错了什么?

I removed the .equals(true)from both of the Asserts. Now I am getting the same error only now it says expected [true] but found [true]

.equals(true)从两个断言中删除了。现在我才遇到同样的错误,它说expected [true] but found [true]

回答by Renato

Most likely you're mixing up data types... what type does notificationCheck.getAttribute("checked") return?? I suppose it does not return a Boolean but a String and that's why your test fails. In that case you could try this:

很可能你混淆了数据类型……notificationCheck.getAttribute("checked") 返回什么类型?我想它不会返回一个布尔值而是一个字符串,这就是你的测试失败的原因。在这种情况下,你可以试试这个:

Assert.assertEquals(true, notificationCheck.getAttribute("checked").equals("true"));

Even better:

更好的是:

Assert.assertEquals("true", notificationCheck.getAttribute("checked"));

回答by Chandini Haridas

If its saying expected true and found true that means: The type might be different. eg : what its getting is boolean-trueand checking if its equal with String-"true"or vice versa.

如果它的说法预期为真并且发现为真,则意味着:类型可能不同。例如:它得到的是什么boolean-true并检查它是否等于String-"true"或反之亦然。