java 断言失败错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7139350/
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
AssertionFailedError
提问by vibhas
I am facing the following problem while running it using junit: All the methods are working fine except testValidatePanelistIDHwid
. This is the new method that i have added in this class all other methods are working fine.
使用 junit 运行它时,我面临以下问题:除testValidatePanelistIDHwid
. 这是我在这个类中添加的新方法,所有其他方法都可以正常工作。
junit.framework.AssertionFailedError: Method "testValidatePanelistIDHwid" not found
at junit.framework.Assert.fail(Assert.java:47)
at junit.framework.TestCase.runTest(TestCase.java:147)
at junit.framework.TestCase.runBare(TestCase.java:127)
import junit.framework.TestCase;
import org.apache.cactus.ServletTestCase;
import org.apache.cactus.WebRequest;
import xys.model.common.BusinessObjectException;
import xys.class.model.common.util.ValidationUtility;
public class ValidationUtilityTest extends ServletTestCase
{
ValidationUtility validationUtil = new ValidationUtility();
public void begin(WebRequest request){}
public void setUp() throws Exception
{
super.setUp();
}//end of SetUp
protected void tearDown() throws Exception
{
super.tearDown();
}
public void testIsMessageOfCorrectLength()
{
boolean isCorrectLen = ValidationUtility.isMessageOfCorrectLength("Test",4);
assertTrue("Message is not correct.",isCorrectLen == true);
isCorrectLen = ValidationUtility.isMessageOfCorrectLength("Test",3);
assertFalse("Message is longer than the limit.",isCorrectLen == true);
isCorrectLen = ValidationUtility.isMessageOfCorrectLength("Test",8);
assertTrue("Message is not longer.",isCorrectLen == true);
try
{
isCorrectLen = ValidationUtility.isMessageOfCorrectLength(null,8);
// fail("InputString is null.");
}
catch(Exception e)
{
System.out.println("ValidationUtilityTest:testIsMessageOfCorrectLength");
}
}
public void testValidateCommandInstanceId()throws BusinessObjectException
{
boolean isValidCmdInstID = false;
isValidCmdInstID = ValidationUtility.validateCommandInstanceId(341);
assertTrue("Command Instance ID 341 is not valid.", isValidCmdInstID == true);
isValidCmdInstID = ValidationUtility.validateCommandInstanceId(400);
assertTrue("Command Instance ID 400 is not valid.", isValidCmdInstID == true);
isValidCmdInstID = ValidationUtility.validateCommandInstanceId(56);
assertTrue("Command Instance ID 56 is not valid.", isValidCmdInstID == true);
isValidCmdInstID = ValidationUtility.validateCommandInstanceId(320);
assertTrue("Command Instance ID 320 is not valid.", isValidCmdInstID == true);
try
{
isValidCmdInstID = ValidationUtility.validateCommandInstanceId(0);
}
catch(BusinessObjectException b)
{
System.out.println("ValidationUtilityTest:testValidateCommandInstanceId");
}
try
{
isValidCmdInstID = ValidationUtility.validateCommandInstanceId(21);
fail("Instance ID not present.");
}
catch(BusinessObjectException b)
{
System.out.println("ValidationUtilityTest:testValidateCommandInstanceId:");
}
try
{
isValidCmdInstID = ValidationUtility.validateCommandInstanceId(-500);
fail("Invalid Instance ID.");
}
catch(BusinessObjectException b)
{
System.out.println("ValidationUtilityTest:testValidateCommandInstanceId");
}
}
public void testValidateHardwareId()throws BusinessObjectException
{
boolean isValidHardwareID = false;
isValidHardwareID = ValidationUtility.validateHardwareId("35279401003038707");
assertTrue("Hardware ID 35279401003038707 is not valid.", isValidHardwareID == true);
isValidHardwareID = ValidationUtility.validateHardwareId("357246016572305");
assertTrue("Hardware ID is not valid.", isValidHardwareID == true);
try
{
isValidHardwareID = ValidationUtility.validateHardwareId("352794010030387076546456546357567567686742324");
fail("Very Long Instance ID.");
}
catch(Exception b)
{
System.out.println("ValidationUtilityTest:testValidateHardwareId: Exception: Very Long Hardware ID.");
}
try
{
isValidHardwareID = ValidationUtility.validateHardwareId("3527dsds$$$707");
fail("Invalid Hardware ID.");
}
catch(Exception b)
{
System.out.println("ValidationUtilityTest:testValidateHardwareId: Exception: Invalid Hardware ID. ");
}
}
public void testValidatePanelistIDHwid()throws BusinessObjectException
{
boolean PanelistIDHwid = false;
PanelistIDHwid = ValidationUtility.validatePanelistIDHwid(19560,"A0000024B885E2");
assertTrue("Panelist ID 19560 and Hardware ID A0000024B885E2 is valid.", PanelistIDHwid == true);
//assertTrue("Hardware ID A0000024B885E2 is valid.", isValidHardwareID == true);
PanelistIDHwid = ValidationUtility.validatePanelistIDHwid(123,"357246016572305");
assertTrue("Hardware ID is not valid.", PanelistIDHwid == true);
try
{
PanelistIDHwid = ValidationUtility.validatePanelistIDHwid(19560,"352794010030387076546456546357567567686742324");
fail("Very Long Instance ID.");
}
catch(Exception b)
{
System.out.println("ValidationUtilityTest:testValidateHardwareId: Exception: Very Long Hardware ID.");
}
try
{
PanelistIDHwid = ValidationUtility.validatePanelistIDHwid(19560,"3527dsds$$$707");
fail("Invalid Hardware ID.");
}
catch(Exception b)
{
System.out.println("ValidationUtilityTest:testValidateHardwareId: Exception: Invalid Hardware ID. ");
}
}
}
junit.framework.AssertionFailedError: Method "testValidatePanelistIDHwid" not found
at junit.framework.Assert.fail(Assert.java:47)
at junit.framework.TestCase.runTest(TestCase.java:147)
at junit.framework.TestCase.runBare(TestCase.java:127)
at org.apache.cactus.internal.AbstractCactusTestCase.runBareServer(AbstractCactusTestCase.java:153)
at org.apache.cactus.internal.server.AbstractWebTestCaller.doTest(AbstractWebTestCaller.java:119)
at org.apache.cactus.internal.server.AbstractWebTestController.handleRequest_aroundBody0(AbstractWebTestController.java:93)
at org.apache.cactus.internal.server.AbstractWebTestController.handleRequest_aroundBody1$advice(AbstractWebTestController.java:224)
at org.apache.cactus.internal.server.AbstractWebTestController.handleRequest(AbstractWebTestController.java)
at org.apache.cactus.server.ServletTestRedirector.doPost_aroundBody2(ServletTestRedirector.java:101)
at org.apache.cactus.server.ServletTestRedirector.doPost_aroundBody3$advice(ServletTestRedirector.java:224)
at org.apache.cactus.server.ServletTestRedirector.doPost(ServletTestRedirector.java)
at org.apache.cactus.server.ServletTestRedirector.doGet_aroundBody0(ServletTestRedirector.java:72)
at org.apache.cactus.server.ServletTestRedirector.doGet_aroundBody1$advice(ServletTestRedirector.java:224)
at org.apache.cactus.server.ServletTestRedirector.doGet(ServletTestRedirector.java)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:432)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
at org.apache.coyote.ajp.AjpProcessor.process(AjpProcessor.java:437)
at org.apache.coyote.ajp.AjpProtocol$AjpConnectionHandler.process(AjpProtocol.java:366)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
at java.lang.Thread.run(Thread.java:619)
回答by Augusto
Holy guacamole! You pack a lot of tests in each one of your tests. Can you provide a stack trace? or what do you mean by not working?
神圣的鳄梨酱!你在每一项测试中都打包了很多测试。你能提供一个堆栈跟踪吗?或者不工作是什么意思?
edit
编辑
Extending my comment, each one of those tests should be several tests instead of one, that's probably one of the bottom reasons why you cannot find the issue. If something breaks, the test should be very, very clear about what is not working.
扩展我的评论,这些测试中的每一个都应该是多个测试而不是一个,这可能是您找不到问题的最根本原因之一。如果出现问题,测试应该非常非常清楚哪些地方不起作用。
Let me take the testtestIsMessageOfCorrectLength
as an example. That's is 3 tests in one, you're checking 4 different expected outcomes (two of them are the same) in just one test. What some people (and myself) consider a good test code base, would break that into the following tests.
让我以测试testIsMessageOfCorrectLength
为例。这是三合一测试,您只需在一次测试中检查 4 种不同的预期结果(其中两个是相同的)。有些人(和我自己)认为一个好的测试代码库会将其分解为以下测试。
- testValidatesMessageOfMinimumLength
- testValidatesMessageOfIncorrectLength
- testMessageValidationThrowsExceptionIfMessageIsNull
- testValidatesMessageOfMinimumLength
- testValidatesMessageOfIncorrectLength
- testMessageValidationThrowsExceptionIfMessageIsNull
If in the future something breaks, you'll know better why and what broke.
如果将来有什么东西坏了,你会更清楚为什么坏了,是什么坏了。
回答by Dunes
Aside from overly complicated test cases that Augusto has already explained about, the main problem that you appear to be having is that whatever is running your test harness can't find your test method. This normally comes about because the test harness has loaded an out of date class file that it has somehow cached. So when asked to run testValidatePanelistIDHwid
to used reflections to find the test method on the Class. However, in this case it wasn't able to find the method (as it's class is outdated), and has thrown an error to indicate this problem.
除了 Augusto 已经解释过的过于复杂的测试用例之外,您似乎遇到的主要问题是运行测试工具的任何东西都找不到您的测试方法。这通常是因为测试工具加载了一个以某种方式缓存的过期类文件。所以当被要求运行testValidatePanelistIDHwid
到使用反射来查找类上的测试方法时。但是,在这种情况下,它无法找到该方法(因为它的类已过时),并且抛出了一个错误以指示此问题。
Easiest way to make sure you clear this problem out is to close down any servers you may be running (if you're programming a webapp), and then do a clean build (deleting all class files and recompiling them). Then when you run your tests again the test harness should have the correct Class.
确保您清除此问题的最简单方法是关闭您可能正在运行的所有服务器(如果您正在编写 web 应用程序),然后进行干净的构建(删除所有类文件并重新编译它们)。然后,当您再次运行测试时,测试工具应该具有正确的类。
回答by ????
Try to use @Test annotation before method.
尝试在方法之前使用@Test 注解。
回答by Rajkumar Pallerla
I have faced below issue related to this post and the fix is below.' issue/error thrown for me is: junit.framework.AssertionFailedError: expected: but was:..
我遇到了与这篇文章相关的以下问题,修复程序如下。为我抛出的问题/错误是:junit.framework.AssertionFailedError:预期:但是是:..
fix for the issue is if you see below code snippet
如果您看到以下代码片段,则解决此问题
public void updateTradesMaintData() throws Exception {
when(tradeMaintenanceDao.updateTradesMaintData(anyString(), anyList(), anyList(),anyString(), anyString())).thenReturn(true);
//when(tradeMaintenanceDao.updateTradesMaintData(userName, //tradeStatusList, tradeReasonList, notes, pendStatus)).thenReturn(true);
boolean data = tradeMaintainanceService.updateTradesMaintData(userName, clientCode, tradeId, tradeStatusCode, tradeSubCategory, tradeActiveFlag, tradeMatchStatusCode, tradeStatusCodeType, notes, reasonCode, reasonCodeType, reasonStatusCode);
verify(tradeMaintenanceDao, times(1)).updateTradesMaintData(anyString(), anyList(), anyList(), anyString(), anyString());
verifyNoMoreInteractions(tradeMaintenanceDao);
assertNotNull(data);
assertEquals(true, data);
}
i have commented the code above and used the statement above the commented line.
我已经注释了上面的代码并使用了注释行上方的语句。