Junit3 error: Implicit super constructor TestCase() is not visible

A

albert kao

The following code has this error compiled in Eclipse:
Implicit super constructor TestCase() is not visible. Must explicitly
invoke another constructor

package com.my.prog;


import junit.framework.*;
import javax.naming.*;
import javax.rmi.PortableRemoteObject;


public abstract class TestProIndexBean extends TestCase {

static {
System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
System.setProperty(Context.PROVIDER_URL, "iiop://localhost:
7001" );
}

private ProIndex proIndex = null;
private ProIndexHome home = null;

protected void setUp() throws Exception {

super.setUp();
if( home == null ) {
Context context = new InitialContext();
home = (ProIndexHome)PortableRemoteObject.narrow(
context.lookup("com.my.prog.business.ProIndexHome"),
ProIndexHome.class);
}
proIndex = home.create();
}

protected void tearDown() throws Exception {
proIndex.remove();
super.tearDown();
}

public void testProIndexRequest1() throws Exception {

System.out.println("testProIndexRequest1()");
ProIndexRequest request = new ProIndexRequest();
ProIndexResponse actualReturn =
proIndex.getProIndexInfo(request);
this.assertEquals("0000", actualReturn.getCd());

}
}
 
E

Eric Sosman

The following code has this error compiled in Eclipse:
Implicit super constructor TestCase() is not visible. Must explicitly
invoke another constructor

package com.my.prog;


import junit.framework.*;
import javax.naming.*;
import javax.rmi.PortableRemoteObject;


public abstract class TestProIndexBean extends TestCase {
[...]

You have written no constructor for the TestProIndexBean
class, so the compiler writes one for you. That compiler-
generated constructor is as simple as possible: All it does
is run the constructor of the superclass, TestCase (it is a
basic rule of Java that the superclass constructor must run
as part of constructing a subclass instance).

All right, but a class may have several constructors;
which one should the compiler-generated code use? The answer
is that it uses the simplest possible constructor: The no-
argument constructor TestCase() in this situation.

... and there's the problem: The TestCase class has a
constructor, of course (every class has at least one), but it
does not have a constructor taking no arguments. So when the
compiler automatically generates code trying to use that non-
existent TestCase() constructor, the compilation fails. Solution:
Write an explicit TestProIndexBean constructor, and have it invoke
the appropriate TestCase constructor via `super(...arguments...)'.

"Why is the compiler so dumb as to write a call to a constructor
that doesn't exist?" I hear you cry. Well, TestCase's constructor
needs arguments -- so what argument values should the compiler
fabricate on your behalf? If TestCase has more than one constructor
(all requiring arguments), how should the compiler choose among them?
The compiler can write a dead-simple constructor for you, but it
can't write anything more complicated than dead-simple without help.
Give it some.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top