Thursday, September 30, 2021

why to use call method in Javascript?

 let's look at an example ,

var john = {

    name: 'john',

    job: 'engineer',

    age: 35,

    presentation: function (style, timeOffDay) {

        if (style === 'formal') {

            console.log('Good ' + timeOffDay + ',ladies and gentlemen i\'m ' + this.name + ' working as ' + this.job + ' and i\'m ' + this.age + ' years old');

        } else if (style === 'friendly') {

            console.log('Good ' + timeOffDay + ',ladies and gentlemen i\'m ' + this.name + ' working as ' + this.job + ' and i\'m ' + this.age + ' years old');


        }

    }

};

john.presentation('formal','morning');

var smith={

    name:'smith',

    job:'teacher',

    age:40

};

john.presentation.call(smith,'friendly','evening');

Here,john is an object which have a method presentation . Now we have created another object smith without any method .But i don't want to create new method for smith object but want to use method of john object .
So,what can we to do achive this?
We can borrow method of john by calling call method and setting this variable to smith as the first argument.

john.presentation.call(smith,'friendly','evening');



Wednesday, September 29, 2021

Closure in Javascript

 An inner function has always access to the outer function's  variable and parameters even after the outer function has returned . Lets understood with a simple example,

function retirement(retirementAge){

    var a="years left until retirement";

    return function(yearOfBirth){//inner function

        var age = 2016-yearOfBirth;

        console.log((retirementAge-age)+a);

    }

}

var retirementUS=retirement(66);

retirementUS(1990);

we already know about lexical scoping , an inner function has access to the variable of the outer function . When a function gets called it is invoked into execution context .Then VO(variable  objects) stores the variable, and the scope chain keep track of the variables to use by the function . When a function returned it will popped up from execution context . So the execution context gets cleared .When the inner function invoked then it is gets called into execution context ,then how inner function can access to the outer functions variable . It is possible only for closures. 

The scope chain always stays intact .We no need to define closure manually . It is built into javascript. Javascript is always do this for us automatically .

Tuesday, September 28, 2021

Why to use mockito?

 Suppose we have a class which have a dependency which will provide some data and based on those data we will do some work,

public class SomeBusinessImpl {

private DataService dataService;//dependency

public SomeBusinessImpl(DataService dataService) {

super();

this.dataService = dataService;

}

int[] data = dataService.retrieveAllData();

int greatest = Integer.MIN_VALUE;

int findTheGreatestFromAllData() {

for (int value : data) {

if (value > greatest) {

greatest = value;

}

}

return greatest;

}

}

interface DataService {

int[] retrieveAllData();

}

Test class:

class SomeBusinessTest {

@Test

public void findGreatestFromAllData() {

SomeBusinessImpl someBusiness = new SomeBusinessImpl(new DataServiceStub());

int result = someBusiness.findTheGreatestFromAllData();

assertEquals(24, result);

}

}

class DataServiceStub implements DataService {//Implementation of DataService

@Override

public int[] retrieveAllData() {

return new int[] { 24, 10, 12 };

}

}

What to do if the logic inside DataServiceStub changed. Every time we will need to give different implementation of the DataService . But if we can create mock using mockito . By using mockito we can easily create dummy data .


Tuesday, September 21, 2021

@BeforeClass,@AfterClass,@Before and @After annotation in Junit ?

 @BeforeClass:  This is annotation is used in that method when there need such situation to do some prior works when test class is loaded in memory. Ex: DB connection establishment.

@Before: This annotation is used when need to do some works before @Test is execute.

@After:    This annotation is used when need to do some works after @Test is execute.

@AfterClass: Is called and execute after all the operation has done in the Test class.

@BeforeClass and @AfterClass runs for once in every test class

Monday, September 20, 2021

What is Junit and how it works?

 Junit is a unit testing framework for java programing language. It is necessary for test driven development.

 To include Junit in project create another source folder and create a class for Junit testing .

Then Junit jars will include in classpath.

When runs a Junit test it first create the instance of the Junit class then call the method annotated with @Test.

Keep in mind-Absence of fail() it is success in Junit.

Saturday, September 18, 2021

IIFE(Immediately invocation function expression)

 Suppose we want to declare a variable that can not be accessed outside of a block ,that means we want a private variable then we can define a method and create a variable inside the method and then can call the method from outside .So, because of scope chain the variable cannot be accessed outside of the block .But there is a problem is that only to declare a private variable we are creating a whole function and it becomes unnecessary .So there is a new concept IIFE introduced.

Ex:

function game(){

var score = Math.random()*10; //Here score variable can't access from outside of the function.

console.log(score>=5);

}

game();


We can write above function using IIFE,

(function(){

var score = Math.random()*10; 

console.log(score>=5);

})();//calling the function .

normally if the function is annonymous and have body then javascript parser treat it as a function declaration .

But if we wrap this function into parentheses-() then javascript parser treat it as function expression .then we called it . Here score variable is no longer accessed outside of the scope.


How to load data from properties file?

 We need to store data using a key in property file .Then use @Value annotation over a variable which will hold that data .Then we should use another @PropertySource annotation is used to provide properties file into spring environment .

Ex:

Suppose we have a url in app.properties file as

external.service.url = http://helloworld.com/hello


Now we have a service class which will provide this url .

@Component

public class ExternalService{

       @Value("${external.service.url}") 

       private String url;

        public String getUrl(){

        return url;

        }

}


Now we want to load properties file while load the class, then

@Configuration 

@ComponentScan

@PropertySource("classpath:app.properties")

public class SpringFrameworkProperties{

  public void main(String[] args){

  AnnotationConfigApplicationContext context = new         AnnotationConfigApplicationContext(SpringFrameworkProperties.class);

   ExternalService service= context.getBean(ExternalService.class);

System.out.println(service.getUrl());

  }

}

Testing controller

------Controller------------- @RestController @RequestMapping("/items") public class ItemController {     private final ItemServic...