You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JUnit测试类报错求助:TestCase未找到及Object构造方法异常

Fixing JUnit TestCase Errors: "TestCase not found" and "Object(String) constructor not found"

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:
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
    
    For Gradle, add this to build.gradle:
    testImplementation 'junit:junit:3.8.1'
    
    If you're manually managing jars, download junit.jar and add it to your project's classpath.
  • Simplify your class declaration: Since you already imported junit.framework.TestCase, replace extends junit.framework.TestCase with just extends TestCase to 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.

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:50:19