TestNG 5.1 - When is constructor called (parameter passing)?

35 views
Skip to first unread message

magnatron

unread,
Aug 18, 2006, 2:08:20 PM8/18/06
to testng...@googlegroups.com
Here is my base class:

public class IdmGetSSOToken
{

public IdmGetSSOToken()
{
System.out.println("IdmGetSSOToken Default Constructor");
}

protected SSOToken getToken(String name, String password, String basedn)
throws Exception
{
System.out.println("INSIDE GETTOKEN");
String s = name;
String s1 = password;
String s2 = basedn;

System.out.println("name=" + name);
System.out.println("password=" + password);
System.out.println("basedn=" + basedn);
...
}
}

Here is my derived class:

@Test(sequential = true)
public class IdmCleanup extends IdmGetSSOToken
{
String s1 = null;
String s2 = null;
String s3 = null;

@Test(parameters={"amadmin.user","amadmin.password","basedn"})
public void IdmCleanup(String s1, String s2, String s3)
throws IdRepoException, SSOException
{
System.out.println("INSIDE CLEANUP CONST");
s1 = s1;
s2 = s2;
s3 = s3;
System.out.println("name1=" + s1);
System.out.println("password1=" + s2);
System.out.println("basedn1=" + s3);
}

public void Cleanup()
throws IdRepoException, SSOException
{
System.out.println("INSIDE CLEANUP");
ssotoken = getToken(s1, s2, s3); (defined in base class)
...
}
}

And here is my testing.xml:

<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" >

<suite name="server" >

<parameter name="amadmin.user" value="amadmin"/>
<parameter name="amadmin.password" value="secret12"/>
<parameter name="basedn" value="dc=red,dc=iplanet,dc=com"/>
<test name="idm1" >
<classes>
<class name="IdmCleanup"/>
</classes>
</test>

</suite>

And here is the problem:

When I execute the calling trace is as follows:

[java] IdmGetSSOToken Default Constructor (default constructor of base class)
[java] INSIDE CLEANUP (method in derived class)
[java] INSIDE GETTOKEN (method in base class)
[java] name=null
[java] password=null
[java] basedn=null
[java] INSIDE CLEANUP CONST (default constructor in derived class)
[java] name1=amadmin
[java] password1=secret12
[java] basedn1=dc=red,dc=iplanet,dc=com

What I am trying to do:

Basically pass some parameters to the derived class, which can be consumed by the program. But the problem I am facing is that the CONSTRUCTOR of the derived class is called last and hence I dont get access to parameters at the right time. It should have been called after the consrtuctor of the base class was called. I even tried the new (sequential=true) stuff but still got the same result. Why is the constructor of the derived class called last? It should have been called first? How can I solve this? I need access to passed parameters at the start of the program, not at the end of it.
---------------------------------------------------------------------
Posted via Jive Forums
http://forums.opensymphony.com/thread.jspa?threadID=40655&messageID=80944#80944

Cédric Beust ♔

unread,
Aug 18, 2006, 2:14:59 PM8/18/06
to testng...@googlegroups.com
Magnatron, a quick remark:  it is very dangerous to 1) use similar field and parameter names and 2) use "s1 = s1".

Can you please fix this and confirm you are still seeing this behavior?

--
Cedric


On 8/18/06, magnatron <testng...@opensymphony.com> wrote:

Here is my base class:

public class IdmGetSSOToken
{

    public IdmGetSSOToken()
    {
        System.out.println("IdmGetSSOToken Default Constructor");
    }

    protected SSOToken getToken(String name, String password, String basedn)
        throws Exception
    {
        System.out.println("INSIDE GETTOKEN");
        String s = name;
        String s1 = password;
        String s2 = basedn;

        System.out.println ("name=" + name);

magnatron

unread,
Aug 18, 2006, 2:23:24 PM8/18/06
to testng...@googlegroups.com
I changed the parameter names and it worked fine. But technically speaking it should have worked but I do agree with you; its a bad thing to do. Thanks

---------------------------------------------------------------------
Posted via Jive Forums
http://forums.opensymphony.com/thread.jspa?threadID=40655&messageID=80949#80949

Cédric Beust ♔

unread,
Aug 18, 2006, 2:26:37 PM8/18/06
to testng...@googlegroups.com
On 8/18/06, magnatron <testng...@opensymphony.com> wrote:

I changed the parameter names and it worked fine. But technically speaking it should have worked but I do agree with you; its a bad thing to do. Thanks

So...  Are you okay now or is the problem still there?  If the latter, please post your new code...

Thanks!

--
Cédric

magnatron

unread,
Aug 18, 2006, 2:51:15 PM8/18/06
to testng...@googlegroups.com
No, that didnt solve the problem. I have attached the 2 files and the output is here:

execute-common-server:
[java] IdmGetSSOToken Default Constructor (constructor in base class)
[java] INSIDE CLEANUP (method of interest in derived class)


[java] name=null
[java] password=null
[java] basedn=null

[java] name1=amadmin (constructor in derived class)
[java] password1=secret12
[java] basedn1=dc=red,dc=iplanet,dc=com

[java] ===============================================
[java] server
[java] Total tests run: 2, Failures: 1, Skips: 0
[java] ===============================================

[java] Java Result: 1

It still 1st calls the default constructor of the base class, than calls the method of interest in the derived class and than calls the constructor of the derived class. It should have been the other way round for the last two calls.

I was able to get what I want by using dependsonmethod, but thats a different way to go about it all together. The constructor calling issue still remains.


---------------------------------------------------------------------
Posted via Jive Forums

http://forums.opensymphony.com/thread.jspa?threadID=40655&messageID=80955#80955

IdmCleanup.java
IdmGetSSOToken.java

flipper

unread,
Aug 18, 2006, 3:46:15 PM8/18/06
to testng-users
Are you expecting the derived class constructor got called first; and
the the base class constructor?

magnatron

unread,
Aug 18, 2006, 4:08:30 PM8/18/06
to testng...@googlegroups.com
No, I am expecting the following:
1. Base class constuctor
2. Derived class constructor (I collect the passed parameters here and set them as global parameters in my derived class)
3. My functional methods in derived class.

Hence the call order should be 1->2->3

Whats happening is 1->3->2.


---------------------------------------------------------------------
Posted via Jive Forums

http://forums.opensymphony.com/thread.jspa?threadID=40655&messageID=80976#80976

flipper

unread,
Aug 18, 2006, 8:30:00 PM8/18/06
to testng-users
I think we have already mentioned that we cannot pass parameters to
constructor.
the sequence is still correct, 1->2->3
just that you cannot get the parameter from constructor.

Reply all
Reply to author
Forward
0 new messages