Skip to content

aghorpade/zerocode-hello-world

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Zerocode Hello World

Sample API testing code using JUnit and Zerocode test framework

The simple and convinient way of testing your APIs

Maven library-

<dependency>
    <groupId>org.jsmart</groupId>
    <artifactId>zerocode-rest-bdd</artifactId>
    <version>1.2.x</version>
</dependency>

How to run the examples?

Ans: The same and usual stuff you do everyday for any maven project.

Step-1:

  • Import this maven project zerocode-hello-world using Eclipse or IntelliJ or your favourite IDE
          * i.e. /zerocode-hello-world/pom.xml, 
          * mvn clean install -DskipTests
          * The above command brings all the libs to the local .m2 repo

Step-2:

  • Run the JustHelloWorldTest.java test (it invokes GitHub REST https apis and asserts the result)
          * i.e. src/test/java/org/jsmart/zerocode/testhelp/tests/helloworld/JustHelloWorldTest.java
          * Then, you can fiddle with the assertions section and run the test again, 
            observe the PASS/FAILURES at the console.
          
          * To run more tests go to individual package and run. e.g. folders-  helloworldgithub, helloworldmore etc

Done. That's it. It is as simple as that.


See more(optional) below what happens when test run is completed.

Sample test run log below for reference-

Quick look at the test case and its run log below-

  • JUnit Test case-
@TargetEnv("github_host.properties")
@RunWith(ZeroCodeUnitRunner.class)
public class JustHelloWorldTest {

    @Test
    @JsonTestCase("helloworld/hello_world_status_ok_assertions.json")
    public void testGet() throws Exception {
    
    }

}
  • Test case JSON content of "helloworld/hello_world_status_ok_assertions.json"
{
    "scenarioName": "GIVEN- the GitHub REST end point, WHEN- I invoke GET, THEN- I will receive the 200 status with body",
    "steps": [
        {
            "name": "get_user_details",
            "url": "/users/siddhagalaxy",
            "operation": "GET",
            "request": {
            },
            "assertions": {
                "status": 200,
                "body": {
                    "login" : "siddhagalaxy",
                    "id" : 33847730,
                    "type" : "User",
                    "followers_url" : "https://api.github.com/users/siddhagalaxy/followers"
                }
            }
        }
    ]
}
  • github_host.properties contains-
# Web Server host and port
restful.application.endpoint.host=https://api.github.com
restful.application.endpoint.port=443

# Web Service context; Leave it blank in case you do not have a common context
restful.application.endpoint.context=
  • Output i.e. the actual Response and expected Assertions -
2018-04-10 21:47:33,748 [main] INFO o.j.z.c.r.ZeroCodeMultiStepsScenarioRunnerImpl - 
------ BDD: Scenario:GIVEN- the GitHub REST end point, WHEN- I invoke GET, THEN- I will receive the 200 status with body -----

2018-04-10 21:47:34,700 [main] INFO org.jsmart.zerocode.core.runner.StepNotificationHandler - 
***Step PASSED:
GIVEN- the GitHub REST end point, WHEN- I invoke GET, THEN- I will receive the 200 status with body
->get_user_details

--------- TEST-STEP-CORRELATION-ID: 0b27d9e4-d3ff-46c8-9b10-53fa0f153a33 ---------
requestTimeStamp:2018-04-10T21:47:33.768
step:get_user_details
url:https://api.github.com:443/users/siddhagalaxy
method:GET
request:
{ } 
--------- TEST-STEP-CORRELATION-ID: 0b27d9e4-d3ff-46c8-9b10-53fa0f153a33 ---------
Response:
{
  "status" : 200,
  "headers" : {
    "Server" : [ [ "GitHub.com" ] ],
    "Content-Type" : [ [ "application/json; charset=utf-8" ] ],
    "ETag" : [ [ "W/\"638280d346aa3b795c938e1635ac65a9\"" ] ],
    "Cache-Control" : [ [ "public, max-age=60, s-maxage=60" ] ],
    "X-Runtime-rack" : [ [ "0.072247" ] ],
    "Status" : [ [ "200 OK" ] ]
  },
  "body" : {
    "login" : "siddhagalaxy",
    "id" : 33847730,
    "avatar_url" : "https://avatars0.githubusercontent.com/u/33847730?v=4",
    "gravatar_id" : "",
    "url" : "https://api.github.com/users/siddhagalaxy",
    "html_url" : "https://github.com/siddhagalaxy",
    "type" : "User",
    "created_at" : "2017-11-20T22:13:14Z",
    "updated_at" : "2018-03-26T19:57:08Z"
  }
}
*responseTimeStamp:2018-04-10T21:47:34.692 
*Response delay:924.0 milli-secs 
---------> Assertion: <----------
{
  "status" : 200,
  "body" : {
    "login" : "siddhagalaxy",
    "id" : 33847730,
    "type" : "User",
    "followers_url" : "https://api.github.com/users/siddhagalaxy/followers"
  }
} 
-done-

**FINISHED executing all Steps for [GIVEN- the GitHub REST end point, WHEN- I invoke GET, THEN- I will receive the 200 status with body] **.
Steps were:[get_user_details]

So what happens when your tests grows to thousands in numbers? You then, need to organize them by feature, by consumer and/or by profile etc. Hence Zerocode helps here to maintain peace!

Also it saves you from creating hundreds of java Pojos with getters/setters, creating their builders and getting lost in visualizing or interpreting in mind how the actual request/response could look like. This hassle one has to face everytime you run the tests or encounter a test failure or during a bug/scenario analysis.

Step-3: (Optional Step to bring up local REST server and fiddle with the tests)

Start the Local Mock REST server
          * i.e. just Run as main() -> test/.../RunMeFirstLocalMockRESTServer.java. 
          * Then you can run the src/test/java/org/jsmart/zerocode/testhelp/tests/helloworldmore/JustHelloWorldMoreTest.java

Run as Suite:

          * src/test/java/org/jsmart/zerocode/testhelp/tests/HelloWorldGitHubSuite.java

More examples:
          * src/test/java/org/jsmart/zerocode/testhelp/tests/helloworldmore/JustHelloWorldMoreTest.java
          * -or- Browse under test/resources/ folders- helloworld, helloworld_github_REST_api, helloworld_more

Done.

Now you can see the-

  • Reports @ target
  • Logs @ target/logs/zerocode_test_logs.log
  • Test coverage CSV Report @ target/zerocode_full_report_YYYY-MM-DDTHH-MM-SS.SSS.csv
  • Test coverage Chart @ target/zerocode_results_chart_YYYY-MM-DDTHH-MM-SS.SSS.html
  • More reports

References:

Eclipse(General key-board shotcuts):

For quick reference only- See more eclipse keys https://www.linkedin.com/pulse/top-30-eclipse-keyboard-shortcuts-java-programmer-jayveersinh-solanki/

  1. Open a matching java file -> Ctrl + Shift + R
  2. Open a matching JSON file -> Ctrl + Shift + R
  3. To navigate to file -> Ctrl + Click

IntelliJ(General key-board shotcuts):

More keys: https://www.jetbrains.com/help/idea/mastering-intellij-idea-keyboard-shortcuts.html

  1. Open a matching java file -> Ctrl + n
  2. Open a matching JSON or XML file -> Ctrl + Shift + n
  3. To navigate to file -> Ctrl + Click
  4. Usage/invocation of a JSON file or Java File or Java method/variable etc -> Alt + F7

About

Zerocode JSON steps hello world example

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 100.0%