Need help from TestNG guru to release a TestNG-happy GuiceBerry

65 views
Skip to first unread message

Luiz-Otavio Zorzella

unread,
Jul 10, 2010, 1:18:30 AM7/10/10
to testng-users
Hi,

I'm working on release 3.0 of GuiceBerry, which is a shim between test
frameworks and Guice (see go/guiceberry). I currently (as of this
release which is about to go out) have JUnit 3 and JUnit 4 support,
but I made the tool agnostic of test framework, and I'd love to have a
TestNG adapter for that, but I need help from someone that can guide
me in the right direction. I expect this to be short and simple (each
of the junit-specific adapters is about 15 lines of code, so that's
what I expect for TestNG as well).

Let me know if you are interested.

Thanks,

Z

Cédric Beust ♔

unread,
Jul 10, 2010, 1:53:26 AM7/10/10
to testng...@googlegroups.com
Hi Luis,

This sounds interesting, can you give more details on what you need?  (feel free to respond to the group)

--
Cédric



--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.




--
Cédric


Zorzella ♫

unread,
Jul 10, 2010, 2:09:58 PM7/10/10
to testng...@googlegroups.com
Cédric,

thanks for the prompt reply. I'm still working on javadoc'ing and
final polish, but I hope you can make sense out of this. There are
only three things that I need in order to make GuiceBerry work:

1. I need to be able to create an instance of a "TestDescription"
(basically a testframe-agnostic representation of a test)
2. I need to be able to run some code "before the test runs"
3. I need to be able to run some code "after the test runs"

Here, before and after mean as early as possible and as late as
possible, since the first step injects stuff into the test, and the
the other makes those injections invalid. That's it.

Just so you get a more concrete idea, here are the current adapters
for junit3 and junit4, paired with the "same" HelloWorld test that
makes use of these adapters:

* Vanilla JUnit3

http://code.google.com/p/guiceberry/source/browse/trunk/src/com/google/guiceberry/junit3/ManualTearDownGuiceBerry.java
http://code.google.com/p/guiceberry/source/browse/trunk/doc/tutorial/test/junit3/tutorial_0_basic/Example0HelloWorldTest.java

* JUnit3 with TearDownTestCase (skip this if you are not familiar with
TearDownTestCase, it will just make it more confusing)

http://code.google.com/p/guiceberry/source/browse/trunk/src/com/google/guiceberry/junit3/AutoTearDownGuiceBerry.java
http://code.google.com/p/guiceberry/source/browse/trunk/doc/tutorial/test/junit3_tdtc/tutorial_0_basic/Example0HelloWorldTest.java

* JUnit4

http://code.google.com/p/guiceberry/source/browse/trunk/src/com/google/guiceberry/junit4/GuiceBerryRule.java
http://code.google.com/p/guiceberry/source/browse/trunk/doc/tutorial/test/junit4/tutorial_0_basic/Example0HelloWorldTest.java

Z

Cédric Beust ♔

unread,
Jul 10, 2010, 4:28:13 PM7/10/10
to testng...@googlegroups.com
Hey Z,

Sounds like you might want to use @BeforeSuite and @AfterSuite, which is as early and as late as you can get with TestNG.  As for the TestDescription, @BeforeMethod sounds like the right place to create it.

Does this help?

--
Cédric

Cédric Beust ♔

unread,
Jul 10, 2010, 4:40:36 PM7/10/10
to testng...@googlegroups.com
By the way, I took a quick look at your tutorial examples and it seems to me that most of them would probably be implemented with @DataProvider on TestNG, which is where you would provide the injection (the advantage of data providers over JUnit's versions is that the parameters are passed are real parameters to the test methods instead of being stored in fields).

--
Cédric


On Sat, Jul 10, 2010 at 11:09 AM, Zorzella ♫ <zorz...@google.com> wrote:



--
Cédric


Zorzella ♫

unread,
Aug 2, 2010, 8:50:51 PM8/2/10
to testng...@googlegroups.com
Cedric,

sorry for the enormous delay. I've been buried in working trying to
get the release out.

Once I got to this, though, I quickly managed to put together
GuiceBerry support for TestNG. Ignoring the borked copied and pasted
comment (I'll fix this soon), check this:

http://code.google.com/p/guiceberry/source/detail?r=246

Even though it's a no-op test, it's totally functional. It's a bit
verbose -- about as verbose as a vanilla JUnit3. Contrast with the
JUnit4, which is the least verbose. To cut down on verboseness, one
could extract those two methods to a base class, but that, as I
understand, is not idiomatic to TestNG (as it is in JUnit3). If you
think of a way to cut down on this verboseness, please do let me know.

PS: As for TestNG's support for injecting data, I do believe it's
orthogonal with what GuiceBerry addresses. The tutorial 0 is
misleading, in that sense, since it shows off GuiceBerry in a totally
artificial way. The tutorial 1, on the other hand, is much more
interesting...

Peace,

Z

Waheed Ahmed

unread,
Jul 21, 2017, 12:17:21 PM7/21/17
to testng-users
Hello Cedric

Greetings

I hope you are doing great. In below code I am trying to save token in first method and use it in second method..When I do system out in second method i see null...if you can see if I am doing something wrong?

package com.waheed.restassured.restassured;
import static io.restassured.RestAssured.*;

import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;

import org.testng.annotations.Test;
import org.testng.Assert;
import org.testng.ITestContext;
import org.testng.annotations.*;

import io.restassured.RestAssured;
import io.restassured.authentication.PreemptiveBasicAuthScheme;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.http.ContentType;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import io.restassured.response.ResponseBody;
import io.restassured.specification.RequestSpecification;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.any;
import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.anything;
import static org.hamcrest.Matchers.describedAs;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.equalToIgnoringWhiteSpace;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.sameInstance;
import static org.hamcrest.Matchers.hasKey;
/**
 * Unit test for simple App.
 */
public class AppTest 
    
{
AuthenticationToken authenticationToken;
String auth_token;
    @Test
    public void testStatusCode(ITestContext testContext){
     Credentials credentials = new Credentials();
        credentials.setUsername("wahmed");
        credentials.setPassword("wahmed123");
     Response response =  
             RestAssured.given().
                 header("Content-Type", "application/json").
                 body(credentials).
             when().
                 post("http://qa-takehome.dev.aetion.com:4440/login").
             then().
                 log().ifError().
                 statusCode(200).
                 contentType("application/json").
                 body("$", hasKey("token")).                                   //authorization_token is present in the response
                 body("any { it.key == 'token' }", is(notNullValue())).        //authorization_token value is not null - has a value
             extract().response();
    
    // auth_token = response.path("token").toString();
    
  
     testContext.setAttribute("AUTH_TOKEN", response.path("token"));
 

     //System.out.println(testContext.getAttribute("AUTH_TOKEN"));
    
    
    }
    @Test
    public void createUser1(ITestContext testContext){
    
     System.out.println((String) testContext.getAttribute("AUTH_TOKEN"));

     UserInfo post = new UserInfo();
     post.setEmail("peta@example.org");
post.setFirstName("Peta");
post.setLastName("Francis");
post.setAge(34);
    
Response response =  
        RestAssured.given().
         header("X-Auth-Token",(String) testContext.getAttribute("AUTH_TOKEN")).
            header("Content-Type", "application/json").
            body(post.toString()).
        when().
            post("http://qa-takehome.dev.aetion.com:4440/user").
        then().
            log().ifError().
            statusCode(200).
            contentType("application/json").
            body("$", hasKey("email")).                                   //authorization_token is present in the response
            body("any { it.key == 'email' }", is(notNullValue())).        //authorization_token value is not null - has a value
        extract().response();
     String resp_email = response.path("email").toString();
     System.out.println(resp_email);


    

    
    }
}

Output:
[TestNG] Running:
  C:\Users\waheed.ahmed\AppData\Local\Temp\testng-eclipse-1095218978\testng-customsuite.xml

null
PASSED: testStatusCode(org.testng.TestRunner@52a86356)
FAILED: createUser1(org.testng.TestRunner@52a86356)
java.lang.IllegalArgumentException: Header value cannot be null
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:83)
at org.codehaus.groovy.reflection.CachedConstructor.doConstructorInvoke(CachedConstructor.java:77)
at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrap.callConstructor(ConstructorSite.java:84)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:60)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:235)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:247)
at io.restassured.internal.assertion.AssertParameter.notNull(AssertParameter.groovy:26)
at io.restassured.internal.assertion.AssertParameter$notNull.callStatic(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:56)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:194)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:214)
at io.restassured.internal.RequestSpecificationImpl.header(RequestSpecificationImpl.groovy:933)
at com.waheed.restassured.restassured.AppTest.createUser1(AppTest.java:92)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:104)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:645)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:851)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1177)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:756)
at org.testng.TestRunner.run(TestRunner.java:610)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:387)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:382)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
at org.testng.SuiteRunner.run(SuiteRunner.java:289)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1293)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1218)
at org.testng.TestNG.runSuites(TestNG.java:1133)
at org.testng.TestNG.run(TestNG.java:1104)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)

On Saturday, July 10, 2010 at 1:53:26 AM UTC-4, Cedric wrote:
Hi Luis,

This sounds interesting, can you give more details on what you need?  (feel free to respond to the group)

--
Cédric


On Fri, Jul 9, 2010 at 10:18 PM, Luiz-Otavio Zorzella <zorz...@google.com> wrote:
Hi,

I'm working on release 3.0 of GuiceBerry, which is a shim between test
frameworks and Guice (see go/guiceberry). I currently (as of this
release which is about to go out) have JUnit 3 and JUnit 4 support,
but I made the tool agnostic of test framework, and I'd love to have a
TestNG adapter for that, but I need help from someone that can guide
me in the right direction. I expect this to be short and simple (each
of the junit-specific adapters is about 15 lines of code, so that's
what I expect for TestNG as well).

Let me know if you are interested.

Thanks,

Z

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.




--
Cédric


Krishnan Mahadevan

unread,
Jul 21, 2017, 11:42:10 PM7/21/17
to testng...@googlegroups.com

Waheed,

From your post, its not clear where you are hitting a null value, but I am guessing that the below illustration explains your problem

 

@Test

public void testStatusCode(ITestContext testContext){

// your rest of the code goes here

    testContext.setAttribute("AUTH_TOKEN", response.path("token"));

}

 

@Test

public void createUser1(ITestContext testContext){

//Am guessing the below line is what is giving you null correct ?

    System.out.println((String) testContext.getAttribute("AUTH_TOKEN"));

}

 

Is the above illustration correct ? If yes, then its perhaps because sometimes createUser1() gets executed first, followed by testStatusCode() method.

 

You should be having TestNG run your tests in an order (i.e., first run testStatusCode() followed by createUser() using one of the following mechanism in @Test annotation:

 

  1. Use “priority” attribute
  2. Use “dependsOnMethods” attribute
  3. Use “dependsOnGroups” attribute

 

Here’s one variant of what I am talking about:

 

@Test

public void testStatusCode(ITestContext testContext){

// your rest of the code goes here

    testContext.setAttribute("AUTH_TOKEN", response.path("token"));

}

 

@Test(dependsOnMethods="testStatusCode")

public void createUser1(ITestContext testContext){

    System.out.println((String) testContext.getAttribute("AUTH_TOKEN"));

}

 

 

 

Thanks & Regards

Krishnan Mahadevan

 

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

My Scribblings @ http://wakened-cognition.blogspot.com/

My Technical Scribbings @ http://rationaleemotions.wordpress.com/

Hello Cedric

 

Greetings

 

     post.setEmail("pe...@example.org");

To unsubscribe from this group, send email to testng-users...@googlegroups.com.


For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.




--
Cédric

--
You received this message because you are subscribed to the Google Groups "testng-users" group.

To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.


To post to this group, send email to testng...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages