0

我在使用 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

4 回答 4

4

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

编辑

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

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

  • testValidatesMessageOfMinimumLength
  • testValidatesMessageOfIncorrectLength
  • testMessageValidationThrowsExceptionIfMessageIsNull

如果将来有什么东西坏了,你会更好地知道为什么和什么坏了。

于 2011-08-21T16:12:13.970 回答
2

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

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

于 2011-08-21T20:37:28.800 回答
0

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

于 2014-05-28T11:45:32.953 回答
0

我遇到了与这篇文章相关的以下问题,修复如下。为我抛出的问题/错误是:junit.framework.AssertionFailedError: expected: but was:..

解决该问题的方法是,如果您看到下面的代码片段

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

}

我已经注释了上面的代码并使用了注释行上方的语句。

于 2016-12-07T14:01:41.410 回答