Monday, November 8, 2021

Module pattern in javascript

Lets have an example -
var controller = (function () {
    var a = 10;  //private variable
    var add = function (a) {  //private function
        return a + 10;
    }

    return {
        publicTest: function (b) { //public method
            console.log(add(b));
        }
    }
})();

Here introducing a module pattern in javascript. 'controller'  is a module . 'a' variable and function 'add' both are inside an IIFE. we already know that an IIFE creates its own execution context .so variable 'a' and function 'add' can't be accessed outside of the the module 'controller'. This is called encapsulation .

Here , IIFE returns an empty object ,by using that object we can call 'publicTest' method.
This is the beauty of module pattern . 

Here,IIFE already return an object ,then how we access the private variable and function inside IIFE.
The answer they both accessed because of closure .

Sunday, November 7, 2021

Project planning in Javascript?

 Steps:

First understand about the requirement of the project .

Write what functionality should have in this project .

Then define modules for individual part .

Then define which functionality should have in which modules.

Then write down code for individual modules.

Ex:




                              Then define  the above requirement to individual modules as like below-




Monday, October 25, 2021

What is the difference between date and instant?

Instant and LocalDateTime are two entirely different animals: One represents a moment, the other does not. Instant represents a moment, a specific point in the timeline. LocalDateTime represents a date and a time-of-day.

Monday, October 18, 2021

Projection in hibernate

 Projection are used to execute aggregate operations and for single value query .

EX -

public static void main(String[] args) {
    SessionFactory factory = new Configuration().configure().addAnnotatedClass(Student.class).buildSessionFactory();
    Session session = factory.getCurrentSession();
    try {
        session.beginTransaction();
        Criteria c = session.createCriteria(Student.class);
        Projection p = Projections.property("lastName");
        List<String> students = c.setProjection(p).list();
        for(String s:students)
            System.out.println(s);
        session.getTransaction().commit();
        session.close();
    } finally {
        factory.close();
    }
}
It will return values of last name column.
Projections are useful for 
1.Fetching only certain column of the table.
2.To execute aggregate function.

Monday, October 11, 2021

@Mock,@InjectMocks and @RunWith(MockitoJunitRunner.class) in mockito

 In previous example we have created a Mock like below-

DataService dataServiceMock = mock(DataService.class);

but we can create mock using @Mock annotations.

@Mock

DataService dataServiceMock;

when mock is created then we need to inject into beans 

@InjectMocks

SomeBusinessImpl businessImpl;// dataServiceMock will inject into SomeBusinessImpl

Then we no need to inject this dependencies through constructor or setter injection.

MockitoJUnitRunner checks if all settings are ok or any mistakes have take places.

so put it to the top of the test class

@RunWith(MockitoJUnitRunner.class)


Friday, October 1, 2021

Previous example using mockito

 package com.example.mockitoEx.MockitoEx;

import static org.junit.jupiter.api.Assertions.assertEquals;

import static org.mockito.Mockito.mock;

import static org.mockito.Mockito.when;


import org.junit.jupiter.api.Test;


class SomeBusinessTestMock {

@Test

public void findGreatestFromAllData() {

DataService dataServiceMock = mock(DataService.class);

when(dataServiceMock.retrieveAllData()).thenReturn(new int[] { 24, 15, 10 });


SomeBusinessImpl businessImpl = new SomeBusinessImpl(dataServiceMock);


int result = businessImpl.findTheGreatestFromAllData();

assertEquals(24, result);

}

}

Using mock method we can create a mock object of any interface or object.


Testing controller

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