Frameworks

Find all the Java/UI Frameworks.

Mockito for beginners

Mockito for beginners

Frameworks
Mockito for beginners: Mockito is an open source testing framework, and it is a widely used for junit testing with mocking beans/repositories in frameworks such as spring mvc & spring boot. The framework allows the creation of test double objects in automated unit tests for the purpose of test-driven development or behavior-driven development. The framework's name and logo are a play on mojitos, a type of drink. How to Mock void method ? doNothing().when(couponRepository).deleteAll(Mockito.any()); Note: deleteAll is a void method.   How to Mock public methods of a same class ? NgDeveloper ngDeveloperSpy = Mockito.spy(new NgDeveloper()); when(ngDeveloperSpy.getAuthor()).thenReturn("Mirthbees"); Note:¬†getAuthor() is the public method which we need to mock, so we have

Creating keystore.p12 from letsencrypt certificate for spring boot projects:

Frameworks, Linux/Unix, Nginx
Creating keystore.p12 from letsencrypt certificate for spring boot projects: Step 1: Stop your server If your server is nginx then use the below command to stop the server. sudo service nginx stop   Step 2: Generating new letsencrypt certificate This is to create the new letsencrypt certificate, if you already have one certificate then skip this step. ./certbot-auto certonly -a standalone \ -d ngdeveloper.com -d www.ngdeveloper.com -d admin.ngdeveloper.com here -d takes all this domains for this certificates, meaning the same certificate can be used for ngdeveloper.com/www.ngdeveloper.com and admin.ngdeveloper.com domains and subdomains. Step 3: Converting letsencrypt to keystore.p12 go to /etc/letsencrypt/live/YOUR-CERTIFICATE-FOLDER/ sudo openssl pkcs12 -export -in full...

Angular 6 access control using ngx-permission

Frameworks
Angular 6 access control using ngx-permission: We can't see any enterprise applications without access control management as this is the most important feature which all the sites must have to avoid security violations and control the views for different user groups. In angular this can be easily achieved using ngx-permission, I personally used it in my application developments and did not face any issues so far, and even if you face any issues developer is so active to respond as quick as possible. 1. Install ngx-permission [plain] npm install ngx-permissions --save [/plain]   2. Import into Main Module (app.module) [plain] //Import your library import { NgxPermissionsModule } from 'ngx-permissions'; import { HttpClientModule } from '@angular/common/http'; [/pl...

How to disable the default spring security in spring boot ?

Frameworks
How to disable the default spring security in spring boot ? To disable the spring boot's default spring security / to disable the security for all the urls then include the below method in your custom security class which extends WebSecurityConfigurerAdapter Method to include: [java] /* to permit all */ @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable().authorizeRequests().anyRequest().permitAll(); } [/java] Full class for reference:   [java] @Configuration @EnableWebSecurity @EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true) public class CustomJDSecurityConfig extends WebSecurityConfigurerAdapter { protected void configure(HttpSecurity http) throws Exception { http.csrf().disable()....

Bean validation in Spring Rest using @Valid

Frameworks, Web Services
Bean validation in Spring Rest using @Valid: Bean validation is important to validate the input bean received in the spring restful requestbody. And also default error messages may not be properly understandable by our consumers, so validating the bean and responding the proper readable message to our consumers makes our spring restful webservice better.   How are we going to do ?   1. Add the validation annotations to the bean fields(preferrably your Dto class), following are the supported annotations for validations: @DecimalMax @DecimalMin @Digits @Email @Future @FutureOrPresent @Max @Min @Negative @NegativeOrZero @NotBlank @NotEmpty @NotNull @Null @Past @PastOrPresent @Pattern @Positive @PositiveOrZero ...

Entity to DTO using Model Mapper in Spring Restful Web services

Frameworks, Web Services
Entity to DTO using Model Mapper in Spring Restful Web services: In spring restful webservices we can not directly expose our entity pojo classes as json responses as it has lot of issues like security/unwanted data's exposure etc. Data Transfer Object (DTO) is a design pattern which actually resolves this issue. I am going to do these sample in spring boot project. In highlevel idea behind this is, Create the DTO pojo classes for all/required of your Entities. 2. Write a model mapper for pojo to entity and vice versa. 3. In controller do the converstions before starting any business. Eg: If you have a entity class called coupon with lot of fields & mappings like store/category/tag inside coupon entity class and you would want to just expose only the few field...

Difference between @Inject and @Autowired

Frameworks
Difference between @Inject and @Autowired This is one of the important interview question of spring / spring boot framework interviews. Auto-wiring is the great feature we got to wire our application dependencies spring way, but it can be done by both JSR annotation and spring provided annotation. It is important to know the difference between these both to make use of it effectively in real time use-cases. @Inject: Does not have 'required' attribute Throws exception if the dependency not found. Java/Sun Provided annotation (JSR 330) Default bean scope is prototype.   @Autowired: Has 'required' attribute Exception can be avoided when no dependency found by setting required attribute to false. Spring provided annotation. Default bean scope is singlet...

When does spring create instances to be injected ?

Frameworks
When does spring create instances to be injected ? This is one of the famous spring / spring boot framework interview question. Spring creates the instances to be injected during the start up. If you have configured spring beans with lazy-init attribute then spring creates the instances of those bean only when you inject/any of it's dependencies injects it. By default, Application Context creates the instances of all the beans eagerly with singleton scope during it's initialization process only.

Reading ClassPath file in Spring Boot/Core Example

Frameworks
This below program reads the status.txt file from the classpath, that is src/main/resources in the maven projects using classpathloader in spring core/spring boot projects. Classpathloader converts it to inputstream, from inputstream we can convert to bufferedinputstream and read the file contents easily. File Structure: Here we have status.txt file inside the resources folder, and we have only the value 100 inside the txt file.   Scheduler in Spring Boot with 3 Simple Steps Example Mention the file name directly inside the classpathresource, Resource resource = new ClassPathResource("status.txt"); If the file inside src/main/resources/config then you can try, Resource resource = new ClassPathResource("/config/status.txt"); (or) Resource resource = new ClassPathReso...

5 New Things you should know in Angular 5/6:

Angular, Frameworks
5 New Things you should know in Angular 5: Even though angular 5 announced as a major release, nothing affects the angular 4 applications and developers. Because majority of the things are modified behind the scenes (than HttpClient), we do not want to worry much about angular 5.     We knew angular 1 to angular 2 transformation, that is actually a major release, because the complete things needs to redesigned. Here things are not that bad, and my angular 4 application works without any change(Changed only HttpClient from Http) after upgraded to angular 5. 1. Build optimizer - Reduce the Bundle size: This basically reduces the size of your bundle, also removes the stuffs which is actually not needed to run.     2. Build optimizer - Remove Decorato...