0

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.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)
3
  • 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?
    – Augusto
    Aug 21, 2011 at 16:04
  • If your tests are throwing errors, it means either your tests are bad or you found a bug. We can't help you in either case - we don't know your requirements or your code.
    – Mat
    Aug 21, 2011 at 16:04
  • Here is the complete stacktrace:
    – vibhas
    Aug 21, 2011 at 16:11

4 Answers 4

4

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 test testIsMessageOfCorrectLength 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.

  • testValidatesMessageOfMinimumLength
  • testValidatesMessageOfIncorrectLength
  • testMessageValidationThrowsExceptionIfMessageIsNull

If in the future something breaks, you'll know better why and what broke.

1
  • Ok Augusto actually I am just started with Junit and i am modifying the existing code and i have written based on the existing example . Thanks alot for your suggestion will try out in this way from now onwards
    – vibhas
    Aug 21, 2011 at 16:26
2

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.

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.

0

Try to use @Test annotation before method.

0

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:..

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.