Wednesday, April 19, 2017

How to get all the annotated classes of a package with same attribute value?

One of the most common problem when using annotation is to find the classes based upon the attribute value of an annotation. 

Problem-statement: Find all the classes in com.annot package which have @ExampleAnnot annotation and attribute value of author in @ExampleAnnot is dht.

To achieve this we will need the following jars.

1. javassist-3.12.1.GA.jar  which can be Download  from here.

2. org.reflections.0.9.11.jar which can be Download from here

Add both the jar files in your classpath. 

Example code: 

package annot;

import java.util.Set;

import org.reflections.Reflections;
import org.testng.TestListenerAdapter;

public class Lists extends TestListenerAdapter {
 public static void main(String[] args) {
  Reflections reflections = new Reflections("com.annot");
  Set<Class<?>> annotated = reflections.getTypesAnnotatedWith(ExampleAnnot.class);
  for(Class<?> c:annotated)
   ExampleAnnot declaredAnnotation = c.getDeclaredAnnotation(ExampleAnnot.class);


The above example first finds all the classes with @ExampleAnnot annotation, then sequentially checks the author attribute from @ExampleAnnot of each class , and if author value is equal to dht, it prints the well qualified name of all those class.
Above example returns all the classes which have author name as dht. 

Happy coding. Let us know if you are stuck somewhere.


Authorize an user using LDAP authentication in Java.

LDAP authentication is now-a-days very useful for enterprises application. LDAP also acts as source of SSO [Single sign on]. Ldap stands for Lightweight Directory Access Protocol , in this post we are basically trying to authenticate an user using his windows credentials with Java. 

Prerequisite :

1. Java version 1.7 or higher

2. Springs 

3. You need to know your organizations LDAP url ,port number , username and password of manager account or service account.

Spring provides LDAP template which we can use for easy querying of LDAP server.

Below is the example code of LDAP authentication:

package dht.test.ldap.unittest;

import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.filter.AndFilter;
import org.springframework.ldap.filter.EqualsFilter;

public class LdapTest {

 public static void main(String[] args) throws Exception {

  LdapContextSource contextSource = new LdapContextSource();

  LdapTemplate ldapTemplate = new LdapTemplate(contextSource);

  boolean b = new LdapTest().authenticate(ldapTemplate, "dht3", "Dht@123");
  System.out.println("Authentication " + b);


 public boolean authenticate(LdapTemplate ldapTemplate, String userName, String password) {
  AndFilter filter = new AndFilter();
  filter.and(new EqualsFilter("objectclass", "person")).and(new EqualsFilter("sAMAccountName", userName));
  return ldapTemplate.authenticate(DistinguishedName.EMPTY_PATH, filter.toString(), password);



 Above program returns Authentication successful if user has provided valid login credentials, false otherwise. 

Happy coding. Let us know if you get stuck somewhere.

Tuesday, April 18, 2017

Solution: java.lang.AbstractMethodError: org.testng Error

How to solve 

java.lang.AbstractMethodError: org.testng.remote.RemoteTestNG$DelegatingTestRunnerFactory.newTestRunner(Lorg/testng/ISuite;Lorg/testng/xml/XmlTest;Ljava/util/Collection;)Lorg/testng/TestRunner;
    at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(
    at org.testng.SuiteRunner.init(
    at org.testng.SuiteRunner.<init>(
    at org.testng.TestNG.createSuiteRunner(
    at org.testng.TestNG.createSuiteRunners(
    at org.testng.TestNG.runSuitesLocally(
    at org.testng.remote.RemoteTestNG.initAndRun(
    at org.testng.remote.RemoteTestNG.main(

error ?

This is one of the common issue people face when running testng suites. 

Most common reason is conflicting of testng versions. May be there are 2 versions of testng in your classpath , which are causing this issue.

Check your maven dependency from Build path

[Right click on project-->Build Path -->Configure build path --> Maven dependency and check for testng]

if there are 2 version of testng, delete one. 

This should solve your problem.

If you get stuck somewhere, contact us here : Email

Saturday, September 3, 2016

How to get Test Case Detail from testlink using TestLink Java API?

Getting test case detail from testlink using API is quite needful when displaying results in Reports.

To get test case related detail you will need testlink-java-api.


  1. TestLink-Java-API
  2. Java 1.7 or higher
  3. Maven

Since, TestLink-Java-API depends on mulitple other jars, it ia adviceable to create a Maven project and add the below dependencies in POM.XML

  <!-- Commons APIs -->
  <!-- Logging APIs -->
  <!-- Configuration APIs -->
  <!-- XML-RPC related APIs -->

Use the below code to retrieve Test case description, Steps to execute, Expected results etc. using External test case id.

public void getTestDetailFromTestLink() {
  String testCaseDescription = "", expectedResults = "", steps = "";
  URL url;
  try {
   url = new URL("http://localhost/testlink/lib/api/xmlrpc/v1/xmlrpc.php");

   String devKey = "5062c3fertf9b8886e0edd0ffdf6b14";
   TestLinkAPI api = new TestLinkAPI(url, devKey);
   TestCase tCase = api.getTestCaseByExternalId("P1-46039", 1);

   testCaseDescription = tCase.getSummary();

   List<TestCaseStep> list = tCase.getSteps();
   Iterator<TestCaseStep> itr = list.iterator();
   while (itr.hasNext()) {
    TestCaseStep tcs =;
    expectedResults = tcs.getExpectedResults();
    steps = tcs.getActions();

  } catch (Exception e) {

  System.out.println("Description :"+testCaseDescription);
  System.out.println("Expected Result :"expectedResults);
  System.out.println("Steps to Execute :"+steps);

Above code produces the below output.

Description :Verify that on selecting Die Hard Techy from Side menu takes user to blog screen.

Expected Result :User should be navigated to Screen.

Steps to Execute :1.Open DieHardTechy website. 2.Select Die Hard Techy.

Monday, August 29, 2016

How to drag and drop an element to location specified by X, Y coordinate using selenium

Drag and drop is now common functionality across many web applications for interactive user interface and easy upload. 

To automate drag and drop in your automation test script, selenium provides a class called Actions for interactive actions.

1. Launch
2. Drag and release the element an X=250 and Y= 10 Pixels.

To automate the above scenario using Selenium Webdriver , we would need to use dragAndHold(WebElement element) function of selenium library.


package seleniumInteractive;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

public class DragAndDrop {

	static {
		System.setProperty("", "Chrome driver location");

	public static void main(String[] args) {

		WebDriver driver = new ChromeDriver();
		WebElement element = driver.findElement("draggable"));
		Actions action = new Actions(driver);
		action.clickAndHold(element).moveByOffset(250, 10).build().perform();


POC videos:

Please follow our other blog Tester in you for more updates and tutorials of Selenium.

Tuesday, August 9, 2016

Java client to send a file or inputstream to Jersey Restful API

We  often run in the need of Java client where we can send a file to Restful web services. To do so we will need a multi part request and send it to api. Below code demonstrate how to do it using java.

package com.test;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.HttpClientBuilder;

public class JavaClient {

 public int callAPI() throws Exception {
  String url = "http://localhost:8080/testAPI/rest/fileupload";
  HttpClient httpclient = HttpClientBuilder.create()
  HttpPost httppost = new HttpPost(url);
  MultipartEntityBuilder reqEntity = MultipartEntityBuilder.create();
  reqEntity.addBinaryBody("file", new File(""));
  HttpResponse response = httpclient.execute(httppost);
  int statuscode = response.getStatusLine().getStatusCode();
  return statuscode;


You can also use InputStream instead of File if you don't want to send File.

To send the inputstream instead of file please replace the statement  reqEntity.addBinaryBody("file", new File(filePath));

with reqEntity.addBinaryBody("file", fileIPStream); in above code snippet. 


© 2014 DieHardTechy . All rights resevered. Designed by Diehardtechy