AssertionFailedError异常

时间:2011-08-21 15:56:05

标签: java testing junit cactus

使用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$$$$38707");  
                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$$$$38707");  
                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)

4 个答案:

答案 0 :(得分:4)

神圣的鳄梨酱!您在每个测试中都包含了大量测试。你能提供堆栈跟踪吗?或者你不工作是什么意思?

修改

扩展我的评论,每个测试应该是几个测试而不是一个,这可能是你找不到问题的根本原因之一。如果出现问题,测试应该非常清楚地表明什么不起作用。

让我以 test testIsMessageOfCorrectLength为例。这是一次测试中的3次测试,你只需要在一次测试中检查4种不同的预期结果(其中两种是相同的)。有些人(和我自己)认为一个好的测试代码库会将其分解为以下测试。

  • testValidatesMessageOfMinimumLength
  • testValidatesMessageOfIncorrectLength
  • testMessageValidationThrowsExceptionIfMessageIsNull

如果将来出现问题,你会更清楚地知道为什么以及什么破坏了。

答案 1 :(得分:2)

除了Augusto已经解释过的过于复杂的测试用例之外,您似乎遇到的主要问题是运行测试工具的任何东西都找不到您的测试方法。这通常是因为测试工具已经加载了以某种方式缓存的过时类文件。因此,当被要求运行testValidatePanelistIDHwid使用反射来查找类上的测试方法时。但是,在这种情况下,它无法找到该方法(因为它的类已过时),并且已经抛出错误来指示此问题。

确保清除此问题的最简单方法是关闭您可能正在运行的任何服务器(如果您正在编写Web应用程序),然后执行干净构建(删除所有类文件并重新编译它们)。然后,当您再次运行测试时,测试工具应具有正确的Class。

答案 2 :(得分:0)

尝试在方法之前使用@Test注释。

答案 3 :(得分:0)

我遇到过与此帖相关的以下问题,修复方法如下。' 为我抛出的问题/错误是:junit.framework.AssertionFailedError:expected:但是:..

修复此问题的方法是,如果您看到下面的代码段

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);

}

我已经对上面的代码进行了评论,并使用了注释行上方的语句。