Showing posts with label invoke. Show all posts
Showing posts with label invoke. Show all posts

Sunday, 22 March 2020

Explicitly invoke the default implementation of a method

Java8 introduces default methods to interfaces. Default methods enables to add new functionality to the existing interfaces, and ensure binary compatibility with code written for older versions of those interfaces.

By using default keyword you can specify a default implementation for a method in interface. All method declarations in an interface, including default methods, are implicitly public.

Sometimes, you may want to override the functionality of default method and at the same time want to call the default method explicitly. You can call the default method explicitly using below notation.

Syntax
{InterfaceName}.super.{method}

Interface1.java
public interface Interface1 {
    default void print(){
        System.out.println("I am in Interface1");
    }
}

Interface2.java
public interface Interface2 {
     default void print(){
        System.out.println("I am in Interface2");
    }
}


MyClass.java

public class MyClass implements Interface1, Interface2{
 public void print(){
  System.out.println("I am in MyClass");
  Interface1.super.print();
  Interface2.super.print();
 }
}


You may like

Wednesday, 11 March 2020

TestNG: invocationCount: Invoke test method given number of times

Specifying ‘invocationCount’ property you can invoke the test method given number of times.

Example
@Test(invocationCount = 5)
public void a() {

}

InvocationCountDemo.java
package com.sample.app.tests;

import org.testng.annotations.Test;

public class InvocationCountDemo {

 int count = 0;

 @Test(invocationCount = 5)
 public void a() {
  count++;
  System.out.println("Invoked count : " + count);
 }
}

Run InvocationCountDemo.java, you will see below messages in console.
[RemoteTestNG] detected TestNG version 7.0.0
Invoked count : 1
Invoked count : 2
Invoked count : 3
Invoked count : 4
Invoked count : 5
PASSED: a
PASSED: a
PASSED: a
PASSED: a
PASSED: a

===============================================
    Default test
    Tests run: 5, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 5, Passes: 5, Failures: 0, Skips: 0
===============================================

From the console, it is clear that the test method ran 5 times.




Previous                                                    Next                                                    Home