JUnit测试类报错求助:TestCase未找到及Object构造方法异常
Hey there, let's work through these two errors you're facing with your JUnit test class. They're linked, so fixing one should resolve the other.
First Error: Error(4,8): TestCase not found
This error means your compiler can't locate the junit.framework.TestCase class. Here's how to fix it:
- Check your JUnit dependency: Ensure you have the correct JUnit 3 library added to your project. If using build tools like Maven, add this dependency to your
pom.xml:
For Gradle, add this to<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency>build.gradle:
If you're manually managing jars, downloadtestImplementation 'junit:junit:3.8.1'junit.jarand add it to your project's classpath. - Simplify your class declaration: Since you already imported
junit.framework.TestCase, replaceextends junit.framework.TestCasewith justextends TestCaseto avoid redundant full-class-name references and reduce confusion.
Second Error: Error(11,14): constructor Object(java.lang.String) not found in class java.lang.Object
This error is a side effect of the first one. When the compiler can't find TestCase, it defaults to treating your class as extending Object (the implicit parent class for all Java classes). Since Object doesn't have a constructor that takes a String, calling super(name) throws this error. Fixing the TestCase not found issue will automatically resolve this.
Bonus: Upgrade to Modern JUnit (Optional but Recommended)
JUnit 3 is pretty outdated. Consider moving to JUnit 4 or 5 for a more flexible testing experience. Here's a quick refactor of your test class to JUnit 4:
package smsgateway; import javax.xml.rpc.ServiceFactory; import org.junit.After; import org.junit.Before; import org.junit.Test; public class GlobalSMSProTest { private GlobalSMSPro myGlobalSMSPro; @Before public void setUp() throws Exception { ServiceFactory factory = ServiceFactory.newInstance(); myGlobalSMSPro = (smsgateway.GlobalSMSPro) factory.loadService(smsgateway.GlobalSMSPro.class); } @After public void tearDown() { myGlobalSMSPro = null; } @Test public void testSendMessage() throws Exception { smsgateway.GlobalSMSProSoap port = myGlobalSMSPro.getGlobalSMSProSoap(); java.lang.String toNumber = null; java.lang.String fromName = null; java.lang.String messageText = null; java.lang.String optionalTextFormat = null; sms.com.strikeiron.ws.LicenseInfo licenseInfo = null; smsgateway.holders.SubscriptionInfoHolder subscriptionInfo = new smsgateway.holders.SubscriptionInfoHolder(); subscriptionInfo.value = null; port.sendMessage(toNumber, fromName, messageText, optionalTextFormat, licenseInfo, subscriptionInfo); } // Add @Test annotation to all your other test methods (testSendMessagesBulk, testTrackMessage, etc.) }
With JUnit 4, you don't need to extend TestCase or define a custom constructor—just use @Before, @After, and @Test annotations for cleaner code.
内容的提问来源于stack exchange,提问作者Masood khan

