Category: Java

Posted on: March 29, 2022 Posted by: rahulgite Comments: 3,318

Kotlin

GitHub – https://github.com/rahulgite/Kotlin Why a New JVM Language? Wordy and full of boilerplate Requires tedious getters and setters Difficult to avoid null pointer exceptions Lacks many modern programming features Why Kotlin? 100% compatible with Java. Eliminates most null pointer exceptions. The syntax is clean and easy to read. Modern features like smart casting. The easiest way to get a taste of Kotlin on https://play.kotlinlang.org/ Create Kotlin Project in Intellij New Project Select Gradle with below framework options Select Java Select Kotlin JVM Next Enter Project Name and Finish. The project will get built and you…

Loading

Posted on: December 24, 2021 Posted by: rahulgite Comments: 12

Unit Testing with Spring Boot

Create a new controller and hello service import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @GetMapping(“/hello”) public String helloWorld(){ return “Hello World”; } } JUnit 4 Code @RunWith(SpringRunner.class) @WebMvcTest(HelloWorldController.class) JUnit 5 Code @WebMvcTest(HelloWorldController.class)   Testing Simple Hello Controller import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.RequestBuilder; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import static org.junit.jupiter.api.Assertions.*; @WebMvcTest(HelloController.class) class HelloControllerTest { @Autowired private MockMvc mockMvc; @Test void helloWorld() throws Exception { // request GET /hello and application/json RequestBuilder requestBuilder = MockMvcRequestBuilders.get(“/hello”) .accept(MediaType.APPLICATION_JSON); MvcResult result=mockMvc.perform(requestBuilder).andReturn(); //verify controller assertEquals(“Hello World”,result.getResponse().getContentAsString()); } } Controller with status and…

Loading

Posted on: December 23, 2021 Posted by: rahulgite Comments: 11

Mockito

Class to test Junit package com.unittest.unittesting.business; import com.unittest.unittesting.data.DataService; public class BusinessImpl { private DataService service; public DataService getService() { return service; } public void setService(DataService service) { this.service = service; } public int calculateSum(int data[]){ int sum=0; for(int i:data){ sum+=i; } return sum; } public int calculateSumByService(){ int data[]=service.retriveData(); int sum=0; for(int i:data){ sum+=i; } return sum; } } Basic Junit Test package com.unittest.unittesting.business; import com.unittest.unittesting.data.DataService; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; class BusinessImplTest { @Test void calculateSum() { BusinessImpl business=new BusinessImpl(); int actualResult=business.calculateSum(new int[] {1,2,3}); int expectedResult= 6; assertEquals(expectedResult,actualResult); } @Test void calculateSum_empty() { BusinessImpl…

Loading

Posted on: November 16, 2021 Posted by: rahulgite Comments: 11

GRPC – Remote Procedure Call (RPC)

Inter-service communication Modern software applications no longer reside in a monolith today.  This big block of code, which is the monolith, is divided into smaller units, which are independent and autonomous.  They can be efficiently maintained and they are highly scalable.  the messages can be passed within services in a synchronous or an asynchronous fashion. Asynchronous message passing is when you place messages in logical queues so that the services can pick them up in the background and carry on with their further processing.   Inter-service communication has evolved Remote Procedural Call.  In this, the client invokes a procedure on the server…

Loading

Posted on: October 10, 2021 Posted by: rahulgite Comments: 16

JUnit

JUnit is a unit testing framework for the Java programming language. JUnit has been important in the development of test-driven development and is one of a family of unit testing frameworks which is collectively known as xUnit that originated with SUnit. JUnit is linked as a JAR at compile-time Create Junit Project. Open Eclipse File > New > Project > Maven Project > check simple project > click Next > add group ID, artifact ID, Project name > Finish Right-click on pom.xml Run as>install maven Maven > update project Folders for project src & test…

Loading

Posted on: September 19, 2021 Posted by: rahulgite Comments: 12

Top 5 Useful Videos for JAVA Developers

Online conference Java Z-Day on April 23 united 2,000 Java lovers from all over the world. Engineers from Minsk, Novosibirsk, Zurich, Houston, Bangalore, and other cities participated in the community-driven event where 14 speakers from different countries shared their expertise. These experts talked about Microservices, Java tips and tricks, streaming apps, taking Java to Cloud, and other useful topics. Here are the most popular presentations based on participants’ voting. First Steps in Taking Java to Cloud by Ranga Karanam, Founder of in28minutes How can Java developers get started with learning the cloud – AWS, Azure, GCP, Docker,…

Loading

Posted on: July 21, 2021 Posted by: rahulgite Comments: 9

Java 8 Features

  Why Java 8? JAVA 8 was to introduce Conciseness in the code. As a result, it brings in Functional programming through Lambda expressions. (tool helps to concise code) Java 8 Features : Lambda Expression Stream API Default methods in the Interface Static methods in the Interface Functional Interface Optional Method references Date API Nashorn, JavaScript Engine ForEach() Method Lambda Expression : It is an anonymous function without a name & a return type. As a result, it helps us to reduce the complexity of code. Syntax : (No. of arguments) -> Expression Number of…

Loading