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

Discussion in 'Java' started by albert kao, Jan 21, 2011.

  1. albert kao

    albert kao Guest

    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());

    }
    }
     
    albert kao, Jan 21, 2011
    #1
    1. Advertising

  2. albert kao

    Eric Sosman Guest

    On 1/21/2011 4:30 AM, albert kao wrote:
    > 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.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Jan 21, 2011
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest

    super.super.super how?

    Guest, Feb 19, 2005, in forum: Java
    Replies:
    24
    Views:
    10,789
    Darryl Pierce
    Feb 24, 2005
  2. Replies:
    7
    Views:
    450
    Patricia Shanahan
    Apr 6, 2008
  3. Hugh Sasse Staff Elec Eng

    Inheriting variables, super, and "not super"?

    Hugh Sasse Staff Elec Eng, Dec 9, 2003, in forum: Ruby
    Replies:
    7
    Views:
    125
    Hugh Sasse Staff Elec Eng
    Dec 11, 2003
  4. Scott
    Replies:
    1
    Views:
    127
    Timothy Hunter
    Aug 20, 2005
  5. Replies:
    9
    Views:
    135
    Sean O'Halpin
    Jul 19, 2006
Loading...

Share This Page