Posts

Showing posts from March, 2021

Spring Boot + Mockito simple application with 100% code coverage

Image
In this article, we will show you a simple  Spring Boot example to demonstrate test methods for Controllers, Service, and Repository, And code coverage analysis using the EclEmma plugin. Technologies used: Spring Boot 2.5.5 H2DB 1.4.197 Mockito 2 Maven EclEmma plugin Junit 4 A quick overview of  Spring Boot, Mockito, and EclEmma plugin Spring boot: Spring boot to develop REST web services and microservices. Spring Boot has taken the Spring framework to the next level. It has drastically reduced the configuration and setup time required for spring projects. We can set up a project with almost zero configuration and start building the things that actually matter to your application. Mockito: Mockito is a mocking framework, a JAVA-predicated library that is utilized for efficacious unit testing of JAVA applications. Mockito is utilized to mock interfaces so that a dummy functionality can be integrated into a mock interface that can be utilized in unit testing. EclEmma : EclE

Go Language - Validate Email Address Example

Image
Email validation is required in nearly every application that has user registration in place. An email has its own structure, and before using it, we need to validate it. In Go Language, email validation can be performed by using the standard library function mail.ParseAddress() or regular expression. Solution 1: Using the standard library function mail.ParseAddress() package main import ( "fmt" "net/mail" ) // Main function func main() { fmt.Println(ValidateEmail( "knowledgefactory-gmail.com" )) fmt.Println(ValidateEmail( "knowledgefactory4u@gmail.com" )) fmt.Println(ValidateEmail( "knowledge-factory4u@gmail.com" )) } func ValidateEmail(email string ) bool { _, err := mail.ParseAddress(email) return err == nil } Output: Solution 2: Using regular expression package main import ( "fmt" "regexp" ) // Main function func main() { fmt.Println(ValidateEmail( "knowledge

How to make the first letter of a string lowercase in Go Language?

Image
How to make the first letter of a string lowercase, but not change the case of any of the other letters? For example: "Go language is awesome" → "go language is awesome" "Hello world" → "hello world" Solution 1:  package main import ( "unicode" "unicode/utf8" ) // Main function func main() { str := "Go language is awesome" println(firstToLower(str)) } func firstToLower(s string ) string { if len(s) > 0 { r, size := utf8.DecodeRuneInString(s) if r != utf8.RuneError || size > 1 { lower := unicode.ToLower(r) if lower != r { s = string(lower) + s[size:] } } } return s } Output: Solution 2: Easy package main import "strings" // Main function func main() { str := "Go language is awesome" str = strings.ToLower(string(str[ 0 ])) + str[ 1 :] println(str) } Output: Solution

How to make the first letter of a string uppercase in Go Language?

Image
How to make the first letter of a string uppercase, but not change the case of any of the other letters? For example: "go language is awesome" → "Go language is awesome" "hello world" → "Hello world" Solution 1: Easy package main import ( "unicode" ) // Main function func main() { str := "go language is awesome" runes := []rune(str) if len(runes) > 0 { runes[ 0 ] = unicode.ToUpper(runes[ 0 ]) } println(string(runes)) } Output: Solution 2: Easy package main import "strings" // Main function func main() { str := "go language is awesome" str = strings.ToUpper(string(str[ 0 ])) + str[ 1 :] println(str) } Output: Solution 3: Normal package main import ( "unicode" "unicode/utf8" ) // Main function func main() { str := "go language is awesome" println(firstToUpper(str)) } func firstToUpper(s string ) string {

String operations - Learn Java 8 with examples

For more topics, please click the below link: Learn Java 8 streams with an example - How to sort a list Learn Java 8 streams with an example - Filter a Map by keys and values Learn Java 8 streams with an example - Summing Numbers Java 8 program to print odd/even numbers from array and List Learn Java 8 streams with an example - count() and filter() method Learn Java 8 streams with an example - map() method Java Lambda Expressions Java 8, Streams to find the duplicate elements String operations - Learn Java 8 with examples Example 1: Splitting Strings With Stream API Split a white space separated String into a list of String using Stream API import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class DriverClass { public static void main(String[] args) { String str = "I love my country" ; Stream. of (str.split( " " )) .map(elem -> new String(elem)) .collect(Collectors.