我在使用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)
天哪!您在每个测试中都打包了许多测试。您能提供堆栈跟踪吗?或者你说的不工作是什么意思?
编辑
扩展我的评论,这些测试中的每一个都应该是几个测试而不是一个,这可能是你找不到问题的根本原因之一。如果有什么东西坏了,测试应该非常非常清楚什么地方坏了。
让我以测试testIsMessageOfCorrectLength
为例。这是一次3个测试,你在一次测试中检查4个不同的预期结果(其中两个是相同的)。有些人(包括我自己)认为好的测试代码库,会将其分解为以下测试:
- testValidatesMessageOfMinimumLength
- testValidatesMessageOfIncorrectLength
- testMessageValidationThrowsExceptionIfMessageIsNull
如果将来有东西坏了,你会更清楚为什么坏了,是什么坏的。
除了Augusto已经解释过的过于复杂的测试用例之外,您似乎遇到的主要问题是,无论运行您的测试工具的是什么,都无法找到您的测试方法。这通常是因为测试工具加载了一个过时的类文件,它以某种方式缓存了这个文件。因此,当要求运行testValidatePanelistIDHwid
时,使用反射来找到类的测试方法。但是,在这种情况下,它无法找到该方法(因为它的类过时了),并且抛出了一个错误来指示此问题。
确保清除此问题的最简单方法是关闭您可能正在运行的任何服务器(如果您正在编写web应用程序),然后进行干净构建(删除所有类文件并重新编译它们)。然后,当您再次运行测试时,测试线束应该具有正确的Class。
尝试在方法前使用@Test注释
我遇到了以下与这篇文章相关的问题,修复如下。为我抛出的问题/错误是: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);
}
我已经注释了上面的代码,并使用了注释行上面的语句。