Monday, 9 August 2021

Junit5: Dependency injection for constructors and methods

In Junit Jupiter, you can use parameterized constructors and methods.

'org.junit.jupiter.api.extension.ParameterResolver' interface is used to dynamically resolve parameters at run time.

 

There are three built-in resolvers available to dynamically resolve parameters at run time.

 

a.   TestInfoParameterResolver

b.   RepetitionInfoParameterResolver

c.    TestReporterParameterResolver

 

TestInfoParameterResolver

If a constructor or method parameter is of type TestInfo, the TestInfoParameterResolver will supply an instance of TestInfo corresponding to the current container or test as the value for the parameter.

 

TestInfoParameterResolverDemo.java

package com.sample.app;

import java.util.Set;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;

public class TestInfoParameterResolverDemo {

	public TestInfoParameterResolverDemo(TestInfo testInfo) {
		System.out.println("\nInside Constructor");
		String displayName = testInfo.getDisplayName();
		System.out.println("\t" + displayName);
	}

	@BeforeEach
	void init(TestInfo testInfo) {
		System.out.println("\nInside init method");
		String displayName = testInfo.getDisplayName();
		System.out.println("\t" + displayName);
	}

	@Test
	@DisplayName("TEST 1")
	@Tag("demoTest")
	@Tag("helloworld")
	void test1(TestInfo testInfo) {
		System.out.println("\nInside test1 method");

		String displayName = testInfo.getDisplayName();
		Set<String> tags = testInfo.getTags();

		System.out.println("\t" + displayName);
		System.out.println("\t" + tags);

	}

	@Test
	void test2() {
		System.out.println("\nInside test2 method");
	}

}

 

Run above class, you will see below messages in console.

Inside Constructor
	TestInfoParameterResolverDemo

Inside init method
	TEST 1

Inside test1 method
	TEST 1
	[demoTest, helloworld]

Inside Constructor
	TestInfoParameterResolverDemo

Inside init method
	test2()

Inside test2 method

 

 

Previous                                                    Next                                                    Home

Junit5: ParameterizedTest: Customizing display names

‘name’ attribute of ParameterizedTest annotations provides support for placeholders to customize the display name.

 

Below table summarizes placeholders supported in custom display names.

 

Placeholder

Display Name

{displayName}

the display name of the method

{index}

the current invocation index (1-based)

{arguments}

the complete, comma-separated arguments list

{argumentsWithNames}

the complete, comma-separated arguments list with parameter names

{0}, {1}, …​

an individual argument

 

Find the below working application.

 

ParameterizedTestCustomDisplayName.java

package com.sample.app;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

public class ParameterizedTestCustomDisplayName {
	@DisplayName("demoTest")
	@ParameterizedTest(name = "{index} ==> the Capital of ''{0}'' is {1}")
	@CsvSource({ "India, New Delhi", "AFGHANISTAN, KABUL", "'CENTRAL AFRICAN REPUBLIC', BANGUI" })
	void testWithCustomDisplayNames(String country, String capital) {
		
		
	}
}

 

Run above test class, you can observe that the display names are reflected in junit window.


 

 

Previous                                                    Next                                                    Home

Neo4j: Exercise 3: Create an organization

Create an organization with following hierarchy.

a.   One CEO

b.   Two Vice Presidents report to CEO

c.    Two directors report to Vice Presidents

d.   Three managers report to Directors

e.   Four employees report to managers

 

 

Step 1: Open neo4j browser by hitting following url in browser.

http://localhost:7474/browser/

 


Step 2: Create nodes and relationships by executing below query.

CREATE (p1:Person{name: "Rama Krishna", age: 32, city: "Bangalore", male: true, designation: "CEO"}),
(p2:Person{name: "Murali", age: 32, city: "Hyderabad", male: true, designation: "Vice President"}),
(p3:Person{name: "Gopi", age: 34, city: "Chennai", male: true, designation: "Vice President"}),
(p4:Person{name: "Kiran", age: 28, city: "Bangalore", male: true, designation: "Director"}),
(p5:Person{name: "Siva", age: 45, city: "Hyderabad", male: true, designation: "Director"}),
(p6:Person{name: "Kiran", age: 36, city: "Hyderabad", male: true, designation: "Manager"}),
(p7:Person{name: "Ritweek", age: 23, city: "Bangalore", male: true, designation: "Manager"}),
(p8:Person{name: "Sudheer", age: 24, city: "Chennai", male: true, designation: "Manager"}),
(p9:Person{name: "Naveen", age: 37, city: "Bangalore", male: true, designation: "Employee"}),
(p10:Person{name: "Sailaja", age: 31, city: "Hyderabad", female: true, designation: "Employee"}),
(p11:Person{name: "Harika", age: 36, city: "Bangalore", female: true, designation: "Employee"}),
(p12:Person{name: "Brahmam", age: 41, city: "Bangalore", male: true, designation: "Employee"})
MERGE (p2)-[:REPORT_TO]->(p1)
MERGE (p3)-[:REPORT_TO]->(p1)
MERGE (p4)-[:REPORT_TO]->(p2)
MERGE (p5)-[:REPORT_TO]->(p3)
MERGE (p6)-[:REPORT_TO]->(p4)
MERGE (p7)-[:REPORT_TO]->(p4)
MERGE (p8)-[:REPORT_TO]->(p5)
MERGE (p9)-[:REPORT_TO]->(p6)
MERGE (p10)-[:REPORT_TO]->(p6)
MERGE (p11)-[:REPORT_TO]->(p7)
MERGE (p12)-[:REPORT_TO]->(p8)
RETURN p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12





Previous                                                    Next                                                    Home

ArangoDB: Query by example

‘byExample’ method fetches all the documents that matches to given collection and return a cursor.

 

Signature

collection.byExample(example)

 

Example

db.user.byExample({"age": 32}).toArray()

db.user.byExample({"age": 32, "lastName": "Gurram"}).toArray()

 

For example, user collection has following documents.

127.0.0.1:8529@abc_org> db.user.all().toArray()
[ 
  { 
    "_key" : "15950", 
    "_id" : "user/15950", 
    "_rev" : "_cReaqJC---", 
    "id" : 1, 
    "firstname" : "Sailu", 
    "lastName" : "Ptr", 
    "age" : 32 
  }, 
  { 
    "_key" : "15970", 
    "_id" : "user/15970", 
    "_rev" : "_cRebGD2---", 
    "id" : 2, 
    "firstname" : "Gopi", 
    "lastName" : "Battu", 
    "age" : 33 
  }, 
  { 
    "_key" : "15978", 
    "_id" : "user/15978", 
    "_rev" : "_cRebX7q---", 
    "id" : 3, 
    "firstname" : "Krishna", 
    "lastName" : "Gurram", 
    "age" : 32 
  }, 
  { 
    "_key" : "15998", 
    "_id" : "user/15998", 
    "_rev" : "_cRebx9K---", 
    "id" : 4, 
    "firstname" : "Venkat", 
    "lastName" : "Ptr", 
    "age" : 35 
  } 
]

 

Example 1: Get all the users whose age is 32.

127.0.0.1:8529@abc_org> db.user.byExample({"age": 32}).toArray()
[ 
  { 
    "_key" : "15950", 
    "_id" : "user/15950", 
    "_rev" : "_cReaqJC---", 
    "id" : 1, 
    "firstname" : "Sailu", 
    "lastName" : "Ptr", 
    "age" : 32 
  }, 
  { 
    "_key" : "15978", 
    "_id" : "user/15978", 
    "_rev" : "_cRebX7q---", 
    "id" : 3, 
    "firstname" : "Krishna", 
    "lastName" : "Gurram", 
    "age" : 32 
  } 
]

 

Example 2: Get all the users whose age is 32 and lastName is Gurram.

127.0.0.1:8529@abc_org> db.user.byExample({"age": 32, "lastName": "Gurram"}).toArray()
[ 
  { 
    "_key" : "15978", 
    "_id" : "user/15978", 
    "_rev" : "_cRebX7q---", 
    "id" : 3, 
    "firstname" : "Krishna", 
    "lastName" : "Gurram", 
    "age" : 32 
  } 
]

 

You can even provide an example on nested properties like below.

 

Example

{ "a.b" : 1 }

 

Find all the documents, which contain a sub-document in ‘a’ that has an attribute ‘b’ of value 1.

 

Let’s experiment with some more complex example.

 

127.0.0.1:8529@abc_org> db.test.all().toArray()
[ 
  { 
    "_key" : "16363", 
    "_id" : "test/16363", 
    "_rev" : "_cReka5S---", 
    "a" : 1, 
    "b" : { 
      "c" : 1, 
      "d" : 2 
    } 
  }, 
  { 
    "_key" : "16365", 
    "_id" : "test/16365", 
    "_rev" : "_cRekeRa---", 
    "a" : 1, 
    "b" : { 
      "c" : 1, 
      "d" : 1 
    } 
  }, 
  { 
    "_key" : "16373", 
    "_id" : "test/16373", 
    "_rev" : "_cReksIS---", 
    "a" : { 
      "b" : 1 
    }, 
    "b" : { 
      "c" : 1, 
      "d" : 1 
    } 
  }, 
  { 
    "_key" : "16387", 
    "_id" : "test/16387", 
    "_rev" : "_cRek37y---", 
    "a" : { 
      "b" : 1, 
      "c" : 2 
    }, 
    "b" : { 
      "c" : 1, 
      "d" : 1 
    } 
  } 
]

 

{ "a" : { "b" : 1 } } is not same as {"a.b" : 1}

 

{ "a" : { "b" : 1 } } find all documents, such that the attribute a contains a document of the form {b : 1 }. For example the document

 

{ "a" : {  "b" : 1 }, "b" : { "c" : 1, "d" : 1 } }

 

Will match, but the document.

 

{ "a" : { "b" : 1, "c" : 2 }, "b" : { "c" : 1, "d" : 1 } }

 

Will not match.

127.0.0.1:8529@abc_org> db.test.byExample({ "a" : { "b" : 1 } } ).toArray()
[ 
  { 
    "_key" : "16373", 
    "_id" : "test/16373", 
    "_rev" : "_cReksIS---", 
    "a" : { 
      "b" : 1 
    }, 
    "b" : { 
      "c" : 1, 
      "d" : 1 
    } 
  } 
]

 

But  if you use

 

{ "a.b" : 1 }

then you will find all documents, which contain a sub-document in a that has an attribute b of value 1.

127.0.0.1:8529@abc_org> db.test.byExample({ "a.b" : 1 } ).toArray()
[ 
  { 
    "_key" : "16373", 
    "_id" : "test/16373", 
    "_rev" : "_cReksIS---", 
    "a" : { 
      "b" : 1 
    }, 
    "b" : { 
      "c" : 1, 
      "d" : 1 
    } 
  }, 
  { 
    "_key" : "16387", 
    "_id" : "test/16387", 
    "_rev" : "_cRek37y---", 
    "a" : { 
      "b" : 1, 
      "c" : 2 
    }, 
    "b" : { 
      "c" : 1, 
      "d" : 1 
    } 
  } 
]

Alternative syntax

collection.byExample(path1, value1, ...)

 

Example1

127.0.0.1:8529@abc_org> db.test.byExample("a.b", 1 ).toArray()
[ 
  { 
    "_key" : "16373", 
    "_id" : "test/16373", 
    "_rev" : "_cReksIS---", 
    "a" : { 
      "b" : 1 
    }, 
    "b" : { 
      "c" : 1, 
      "d" : 1 
    } 
  }, 
  { 
    "_key" : "16387", 
    "_id" : "test/16387", 
    "_rev" : "_cRek37y---", 
    "a" : { 
      "b" : 1, 
      "c" : 2 
    }, 
    "b" : { 
      "c" : 1, 
      "d" : 1 
    } 
  } 
]

Example 2

127.0.0.1:8529@abc_org> db.test.byExample("a.b", 1, "a.c", 2 ).toArray()
[ 
  { 
    "_key" : "16387", 
    "_id" : "test/16387", 
    "_rev" : "_cRek37y---", 
    "a" : { 
      "b" : 1, 
      "c" : 2 
    }, 
    "b" : { 
      "c" : 1, 
      "d" : 1 
    } 
  } 
]



 

Previous                                                    Next                                                    Home

ArangoDB: skip(): skip number of documents

‘skip(n)’ method is used to skip ‘n’ number of documents.

 

Example

127.0.0.1:8529@abc_org> db.user.all().toArray()
[ 
  { 
    "_key" : "15750", 
    "_id" : "user/15750", 
    "_rev" : "_cReWUTe---", 
    "id" : 1, 
    "name" : "Sailu" 
  }, 
  { 
    "_key" : "15758", 
    "_id" : "user/15758", 
    "_rev" : "_cReWbIK---", 
    "id" : 2, 
    "name" : "Ram" 
  }, 
  { 
    "_key" : "15766", 
    "_id" : "user/15766", 
    "_rev" : "_cReWhcu---", 
    "id" : 3, 
    "name" : "Gopi" 
  }, 
  { 
    "_key" : "15774", 
    "_id" : "user/15774", 
    "_rev" : "_cReWo3e---", 
    "id" : 4, 
    "name" : "Joel" 
  } 
]

 

As you above snippet user Collection has 4 documents in it. Let’s skip the first 2 documents.

127.0.0.1:8529@abc_org> db.user.all().skip(2).toArray()
[ 
  { 
    "_key" : "15766", 
    "_id" : "user/15766", 
    "_rev" : "_cReWhcu---", 
    "id" : 3, 
    "name" : "Gopi" 
  }, 
  { 
    "_key" : "15774", 
    "_id" : "user/15774", 
    "_rev" : "_cReWo3e---", 
    "id" : 4, 
    "name" : "Joel" 
  } 
]

 


 

Previous                                                    Next                                                    Home

ArangoDB: limit(): limit number of documents to be returned

‘limit()’ method limit the number of documents returned.

 

Example

127.0.0.1:8529@abc_org> db.user.all().toArray()
[ 
  { 
    "_key" : "15750", 
    "_id" : "user/15750", 
    "_rev" : "_cReWUTe---", 
    "id" : 1, 
    "name" : "Sailu" 
  }, 
  { 
    "_key" : "15758", 
    "_id" : "user/15758", 
    "_rev" : "_cReWbIK---", 
    "id" : 2, 
    "name" : "Ram" 
  }, 
  { 
    "_key" : "15766", 
    "_id" : "user/15766", 
    "_rev" : "_cReWhcu---", 
    "id" : 3, 
    "name" : "Gopi" 
  }, 
  { 
    "_key" : "15774", 
    "_id" : "user/15774", 
    "_rev" : "_cReWo3e---", 
    "id" : 4, 
    "name" : "Joel" 
  } 
]

 

As you above snippet user Collection has 4 documents in it. Let’s limit the results to 2 using limit method.

127.0.0.1:8529@abc_org> db.user.all().limit(2).toArray()
[ 
  { 
    "_key" : "15750", 
    "_id" : "user/15750", 
    "_rev" : "_cReWUTe---", 
    "id" : 1, 
    "name" : "Sailu" 
  }, 
  { 
    "_key" : "15758", 
    "_id" : "user/15758", 
    "_rev" : "_cReWbIK---", 
    "id" : 2, 
    "name" : "Ram" 
  } 
]

 

 

 

 

 

Previous                                                    Next                                                    Home