Showing posts with label parameters. Show all posts
Showing posts with label parameters. Show all posts

Wednesday, 3 February 2021

Spring Shell: Parameter Arity

Sometimes there are situations, where you may need to take multiple values to a parameter. You can do this using ‘arity’ attribute of @ShellOption annotation.

 

Example

@ShellComponent(value = "Multi Valued Parameters Demo")
public class MultiValuedParametersDemo {

	@ShellMethod(value = "Commands to Calculate Square of given numbers", key = "square", prefix = "-")
	public String SquarelOfNumbers(@ShellOption(arity=5)int[] input, String message) {

		StringBuilder builder = new StringBuilder();

		for (int i : input) {
			int result = i * i;
				
			builder.append(String.format(message, i, result)).append("\n");
		}

		return builder.toString();
	}
}

 

In the above snippet, I mentioned arity as 5 for the argument input[]. So I can call the command ‘square’ like below.

 

square -input 1 2 3 4 5 -message "Square of a number %d is %d"

shell:>square -input 1 2 3 4 5 -message "Square of a number %d is %d"
Square of a number 1 is 1
Square of a number 2 is 4
Square of a number 3 is 9
Square of a number 4 is 16
Square of a number 5 is 25

You can download complete application from this link.

https://github.com/harikrishna553/springboot/tree/master/shell/hello-world

 

Note:

Infinite Arity yet to be implemented.

 

  

Previous                                                    Next                                                    Home

Sunday, 15 March 2020

TestNG: @DataProvider: Pass parameters to test cases

Using @DataProvider annotation, we can pass parameters to test cases.

For example, let’s try to test 'add' function of calculator with different input combinations.

Step 1: Define a method that feeds input the test case.
@DataProvider(name = "inputCombinations")
private Object[][] input() {
        return new Object[][] { { 0, 0 }, { -1, -2 }, { 1, -2 }, { -2, 1 }, { 1, 2 } };
}

Step 2: Specify data provider to the test case. Data provider pass the input to the test function.
@Test(dataProvider = "inputCombinations")
public void addTest(int a, int b) {
        int result = calculator.add(a, b);
        System.out.println("Sum of a and b is : " + result);

        assertEquals(result, sum(a, b));
}

Find the below working application.

Calculator.java
package com.sample.app.arithmetic;

public class Calculator {
        public int add(int a, int b) {
                return a + b;
        }

        public int subtract(int a, int b) {
                return a - b;
        }

        public int mul(int a, int b) {
                return a * b;
        }

        public int div(int a, int b) {
                return a / b;
        }

        public int remainder(int a, int b) {
                return a % b;
        }

        public boolean isEven(int number) {
                return number % 2 == 0;
        }

        public boolean isOdd(int number) {
                return number % 2 != 0;
        }
}

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

import static org.testng.Assert.assertEquals;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.sample.app.arithmetic.Calculator;

public class DataProviderDemo {

        private Calculator calculator = new Calculator();

        @DataProvider(name = "inputCombinations")
        private Object[][] input() {
                return new Object[][] { { 0, 0 }, { -1, -2 }, { 1, -2 }, { -2, 1 }, { 1, 2 } };
        }

        @Test(dataProvider = "inputCombinations")
        public void addTest(int a, int b) {
                int result = calculator.add(a, b);
                System.out.println("Sum of a and b is : " + result);

                assertEquals(result, sum(a, b));
        }

        private static int sum(int a, int b) {
                return a + b;
        }
}

Run DataProviderDemo.java, you will see below messages in console.

[RemoteTestNG] detected TestNG version 7.0.0
Sum of a and b is : 0
Sum of a and b is : -3
Sum of a and b is : -1
Sum of a and b is : -1
Sum of a and b is : 3
PASSED: addTest(0, 0)
PASSED: addTest(-1, -2)
PASSED: addTest(1, -2)
PASSED: addTest(-2, 1)
PASSED: addTest(1, 2)

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


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


Previous                                                    Next                                                    Home

Friday, 7 February 2014

More About Methods

Methods without returning any value and without parameters
    Syntax:
    void methodName(){
        //body
    }

If a method don't return any value, then it's return type must be void

Example
class MethodEx{
 void print(){
  System.out.println("I am not returning any value");
 }
 
 public static void main(String args[]){ 
  MethodEx obj = new MethodEx();
  obj.print();  
 }
}  

Output
I am not returning any value

Methods without returning any value and with parameters
    Syntax:
    void methodName(parameters){
        //body
    }
    You can give any number of parameters to a method

 Example
class MethodEx{
 void print(int a, int b){ 
  System.out.println("The parameter value is " + a +" " + b);
 }
 public static void main(String args[]){
  MethodEx obj = new MethodEx();
  obj.print(10,20);
 } 
}
 
Output    
 The parameter value is 10 20

Methods with returning a value and without parameters
    Syntax
    returnType methodName(){
        //body
    }
Here returnType is any primitive or reference type. The method must returns the value which is compatible with the returnType of the method.

Example
class MethodEx{
 String fun(){ 
  return "Let's have fun with methods";
 }
 
 public static void main(String args[]){ 
  MethodEx obj = new MethodEx();
  String s = obj.fun();
  System.out.println(s);   
 }
}

Output
Let's have fun with methods

Observation
The method fun() return type is String, so whatever the value returns by the method fun(), we must store it in the String variable like “String s = obj.fun()”.

Methods with returning a value and with parameters
    Syntax
    returnType methodName(parameters){
        //body
    }

A method can have any number of parameters. Here returnType is any primitive or reference type. The method must returns the value which is compatible with the returnType of the method.

Example
class MethodEx{
 String fun(String firstName, String lastName, int age){
  return "I am " + firstName +" " + lastName + " and my Age is " + age;
 }
 
 public static void main(String args[]){
  MethodEx obj = new MethodEx();
  String s = obj.fun("Aravind", "Phaneendra", 25);
  System.out.println(s);
 }
}
        
Output
 I am Aravind Phaneendra and my Age is 25
    
Some Points to Remember
1. returning incompatible value causes the compiler to throw error.
     Example
class MethodEx{
 int sum(int a, int b){
  return 10.01;
 }
}
                  
When you try to compile the above program, compiler will throw the below error

MethodEx.java:4: error: possible loss of precision
 return 10.01;
 ^
 required: int
 found: double
1 error


2. You can call the method in print methods, if it is returning a value, i.e, non void type methods
    Example
class MethodEx{
 int sum(int a, int b){
  return (a+b);
 }
 public static void main(String args[]){
  MethodEx obj = new MethodEx();
  System.out.println("Sum of 10 and 20 is " + obj.sum(10,20));
 }
}

Output
Sum of 10 and 20 is 30
         
3. Calling void methods inside the print causes compile time error
     Example
class MethodEx{
 void print(){
 }
 
 public static void main(String args[]){
  MethodEx obj = new MethodEx();
  System.out.println(obj.print());
 }
} 


When you try to compile the above program, compiler will throw the below error.      
MethodEx.java:7: error: 'void' type not allowed here
 System.out.println(obj.print());
 ^
1 error

           


How to define method                                                 varargs                                                 Home