All posts by Saurabh Gupta

My Name is Saurabh Gupta, Done MCA from MMMEC Gorakhpur and having approx. 13 Year of experience in Information Technology World manly in Java/J2EE. During this time I have worked with multiple organization with different client, so many technology, frameworks etc.

Spring Boot: Custom Actuator EndPoints Creation


In the previous post , Spring Boot Actuator : : Monitor Health of Application you have learn about the Spring Boot actuator enabling and access health monitoring parameters through Spring Boot Actuator provided endpoints (default).

In this post, You will learn about to create custom Spring Boot Actuator end points. Sometimes these custom actuator endpoints required to get some specific information or customization of information instead of providing all detailed application health related information.

You have to follow these steps in Spring Boot application to create custom actuator endpoints.

Step 1:

Create or import any Spring Boot REST application. For Example using for User related Rest service (CRUD).

Step 2:

Enable the actuator in Spring Boot application by adding actuator starter in application pom.xml

<dependency>
	  <groupId>org.springframework.boot</groupId>
	  <artifactId>spring-boot-starter-actuator</artifactId>  
</dependency>

Step 3:

By default actuator allows external access for endpoints /actuator /health and /info, to access others custom actuator endpoints. you have add this property in your application.properties file.

management.endpoints.web.exposure.include=* 

Now, start your application and see the actuator is working fine or not by trying below url. If your server running on port 8090.

http://localhost:8090/actuator/health

{"status":"UP"}

UP status shows your application is running fine.

See More : Spring Boot Actuator : : Monitor Health of Application

Step 4:

Now to create custom actuator endpoints , you can create a class MyUserEndPoints in package com.facingIssuesOnIT.actuator as below:

To create a new endpoint you have to create a class and annotate it with @Component and @Endpoint annotation. The @Endpoint annotation has a parameter id (Ex: users) which determines the URL path of endpoint. This class contains the methods which returns the response of the endpoint. 

package com.FacingIssuesOnIT.actuators;

impert java.util.List;
impert java.util.Optional;

impert org.springframework.beans.factory.annotation.Autowired;
impert org.springframework.boot.actuate.endpoint.annotation.DeleteOperation;
impert org.springframework.boot.actuate.endpoint.annotation.Endpoint;
impert org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
impert org.springframework.boot.actuate.endpoint.annotation.Selector;
impert org.springframework.boot.actuate.endpoint.annotation.WriteOperation;
impert org.springframework.core.env.Environment;
impert org.springframework.stereotype.Component;

impert com.FacingIssuesOnIT.model.User;
impert com.FacingIssuesOnIT.repository.UserRepository;

at2Component
at2Endpoint(id = "users")
public class MyUserEndpoints {
	
		at2Autowired
		private UserRepository userRepository;
		
		at2Autowired
		private Environment environment;
		
		at2ReadOperation
		public List<User> getAllUsers(){
			List<User> users =  userRepository.findAll();			
			return users;
		}
		
		at2WriteOperation
	    public String updateUser(Integer userId, String emailId) throws Exception {
		Optional<User> optional = userRepository.findById(userId);
			User user = optional.orElseThrow(() -> new Exception("Service.USER_NOT_FOUND"));
			user.setEmail(emailId);			
		return environment.getProperty("API.UPDATE_SUCCESS");
	}
		
		at2DeleteOperation
		public String deleteUser(at2Selector Integer userId) {
			userRepository.deleteById(userId);
			return environment.getProperty("API.DELETE_SUCCESS");
		}		
} 

In above class,

  • The value of id parameter of @Endpoint is usersSo this endpoint is accessible by URL /actuator/users.
  • a method defined with @ReadOperation which will be mapped to HTTP GET method and automatically be exposed over HTTP.
  • a method defined with @WriteOperation which will be mapped to HTTP POST method and automatically be exposed over HTTP. The methods that are annotated with @WriteOperation can take parameters in JSON format alone.
  • a method defined with @DeleteOperation which will be mapped to HTTP DELETE method and automatically be exposed over HTTP.
  • technology-specific endpoints defined with @WebEndpoint/@JmxEndpoint. For ex, @WebEndpoint is exposed over HTTP only.

Step 5:

Restart the application and try with different custom actuator end points as below:

GET Method : http://localhost:8090/actuator/users

Response: Existing users list

Spring Boot Custom Actuator Endpoints : Get Method

POST Method : http://localhost:8090/actuator/users

Request Body

Spring Boot Actuator Endpoints : Post Method

It will create new user sunny with Id 345

Delete Method: http://localhost:8090/actuator/users/3

Spring Boot Actuator Endpoints : Delete Method

Here you have seen the custom actuator endpoints urls and response through the postman.

Conclusion

In this post you have learn about the below points:

  • Spring Boot Actuator configuration and enabling
  • Spring Boot custom actuator endpoints creation steps.
  • Spring Boot custom actuator endpoints access through Postman.

Let me know your thought on this post.

Happy Learning !!!

Spring Boot Actuator : : Monitor Health of Application


Once Spring Boot application deployed into production environment, you always want to monitor the application. This is because you want to ensure that the application must be always up and running and in case of any issues you want to fix it quickly. Therefore, an application monitoring tool is required so that you can analyze the health of your application.

Spring Boot has an in-built mechanism for monitoring application called as Actuator. It is a sub-project of Spring Boot. It offers several production grades features to monitor the application. Once you enable actuator in your Spring Boot application, a set of endpoints are exposed using which you can monitor and manage your application.  You can also integrate these endpoints with other application monitoring tools such Prometheus, Graphite etc.

Now let us see how you can enable Actuator in your Spring Boot application.

Spring Boot Actuator Configuration

Actuators can be easily enabled in your application by adding following spring-boot-actuator dependency in pom.xml file:

<dependency>
	  <groupId>org.springframework.boot</groupId>
	  <artifactId>spring-boot-starter-actuator</artifactId>  
</dependency> 

See More : Spring Boot: Custom Actuator EndPoints Creation

Actuator Endpoints

Once Actuator is enabled in your application, using actuator endpoints you can monitor and manage your application. These endpoints are exposed over HTTP in Spring MVC application using the ‘id’ of endpoints as the URL path along with /actuator as prefix. The following table shows some important actuator endpoints:

Actuator EndpointsDescription
/beansProvides list of all Spring beans available in the application
/configpropsProvides a collated list of all @ConfigurationProperties
/envExposes all the properties from Spring’s Configurable Environment
/infoDisplays arbitrary application information
/metricsDisplays metric information for the current application
/mappingsDisplays a collated list of all request mapping paths
/shutdownAllows the application to shutdown
/traceDisplays trace information, by default latest 100 HTTP requests
/healthProvides applications health information
Spring Boot Actuator Endpoints

Note : By default spring boot actuator only only allow the access of /health and /info end points because other end points provide application sensitive information.

You can also access the other specific end points by making below changes in your application.properties / application.yml file.

Access actuator all endpoints

You can add below properties to access all the end points:

#Access all actuator endpoints
management.endpoints.web.exposure.include=* 

Restrict specific actuator endpoints

You can add below exclude property to restrict the specific urls:

#Access all actuator endpoints
management.endpoints.web.exposure.include=*
#Retrict env endpoints to access
management.endpoints.web.exposure.exclude=env 

If you want to restrict access for more than one endpoints the specify as comma separated:

#Access all actuator endpoints
management.endpoints.web.exposure.include=*
#Retrict env endpoints to access
management.endpoints.web.exposure.exclude=env,beans

We will discuss on all the spring boot actuator end points in further sections.

If your application is running application on port number 8090, you can use below end point to get list of all the actuator endpoints and it will give you following response:

http://localhost:8090/actuator

Endpoint /actuator

By default actuator endpoint return only endpoints for /info and /health only as below.

Response:

{
	"_links": {
		"self": {
			"href": "http://localhost:8090/actuator",
			"templated": false
		},
		"health": {
			"href": "http://localhost:8090/actuator/health",
			"templated": false
		},
		"health-path": {
			"href": "http://localhost:8090/actuator/health/{*path}",
			"templated": true
		}
	}
}

If your actuator is enabled for all the endpoints as below then you will get complete list of end points of actuator:

#Access all actuator endpoints
management.endpoints.web.exposure.include=* 

Response:

{
	"_links": {
		"self": {
			"href": "http://localhost:8090/actuator",
			"templated": false
		},
		"beans": {
			"href": "http://localhost:8090/actuator/beans",
			"templated": false
		},
		"caches": {
			"href": "http://localhost:8090/actuator/caches",
			"templated": false
		},
		"caches-cache": {
			"href": "http://localhost:8090/actuator/caches/{cache}",
			"templated": true
		},
		"health": {
			"href": "http://localhost:8090/actuator/health",
			"templated": false
		},
		"health-path": {
			"href": "http://localhost:8090/actuator/health/{*path}",
			"templated": true
		},
		"info": {
			"href": "http://localhost:8090/actuator/info",
			"templated": false
		},
		"conditions": {
			"href": "http://localhost:8090/actuator/conditions",
			"templated": false
		},
		"configprops": {
			"href": "http://localhost:8090/actuator/configprops",
			"templated": false
		},
		"configprops-prefix": {
			"href": "http://localhost:8090/actuator/configprops/{prefix}",
			"templated": true
		},
		"env-toMatch": {
			"href": "http://localhost:8090/actuator/env/{toMatch}",
			"templated": true
		},
		"env": {
			"href": "http://localhost:8090/actuator/env",
			"templated": false
		},
		"loggers": {
			"href": "http://localhost:8090/actuator/loggers",
			"templated": false
		},
		"loggers-name": {
			"href": "http://localhost:8090/actuator/loggers/{name}",
			"templated": true
		},
		"heapdump": {
			"href": "http://localhost:8090/actuator/heapdump",
			"templated": false
		},
		"threaddump": {
			"href": "http://localhost:8090/actuator/threaddump",
			"templated": false
		},
		"metrics-requiredMetricName": {
			"href": "http://localhost:8090/actuator/metrics/{requiredMetricName}",
			"templated": true
		},
		"metrics": {
			"href": "http://localhost:8090/actuator/metrics",
			"templated": false
		},
		"scheduledtasks": {
			"href": "http://localhost:8090/actuator/scheduledtasks",
			"templated": false
		},
		"mappings": {
			"href": "http://localhost:8090/actuator/mappings",
			"templated": false
		}
	}
}

Now in further section will use the above endpoints of actuator and show the response of each API’s call.

Endpoint /health

This actuator health endpoint gives you the information about health of application. You can access this endpoint using the URL http://localhost:8090/actuator/health. It will give you following response:

{"status":"UP"}

The status will be UP if application is running and healthy. If application has some issues such as database is down etc. then you will get following response:

{"status":"DOWN"}

It tells only whether the status of application is UP or DOWN.  If you want to get details information about health of application then add the following property in the application.properties file:

management.endpoint.health.show-details=always
{
	"status": "UP",
	"components": {
		"db": {
			"status": "UP",
			"details": {
				"database": "H2",
				"validationQuery": "isValid()"
			}
		},
		"diskSpace": {
			"status": "UP",
			"details": {
				"total": 300662386688,
				"free": 277733244928,
				"threshold": 10485760,
				"exists": true
			}
		},
		"ping": {
			"status": "UP"
		}
	}
}

If you noticed from above response, it’s connected with database H2 and application consuming data disc spaces.

Endpoint /metrics

This actuator metrics endpoint displays various metrics options that can be checked for your application. You can access this endpoint using the URL http://localhost:8090/actuator/metrics. It will give you following response :

{
	"names": [
		"hikaricp.connections",
		"hikaricp.connections.acquire",
		"hikaricp.connections.active",
		"hikaricp.connections.creation",
		"hikaricp.connections.idle",
		"hikaricp.connections.max",
		"hikaricp.connections.min",
		"hikaricp.connections.pending",
		"hikaricp.connections.timeout",
		"hikaricp.connections.usage",
		"http.server.requests",
		"jdbc.connections.active",
		"jdbc.connections.idle",
		"jdbc.connections.max",
		"jdbc.connections.min",
		"jvm.buffer.count",
		"jvm.buffer.memory.used",
		"jvm.buffer.total.capacity",
		"jvm.classes.loaded",
		"jvm.classes.unloaded",
		"jvm.gc.live.data.size",
		"jvm.gc.max.data.size",
		"jvm.gc.memory.allocated",
		"jvm.gc.memory.promoted",
		"jvm.gc.pause",
		"jvm.memory.committed",
		"jvm.memory.max",
		"jvm.memory.used",
		"jvm.threads.daemon",
		"jvm.threads.live",
		"jvm.threads.peak",
		"jvm.threads.states",
		"logback.events",
		"process.cpu.usage",
		"process.start.time",
		"process.uptime",
		"system.cpu.count",
		"system.cpu.usage",
		"tomcat.sessions.active.current",
		"tomcat.sessions.active.max",
		"tomcat.sessions.alive.max",
		"tomcat.sessions.created",
		"tomcat.sessions.expired",
		"tomcat.sessions.rejected"
	]
}

The above metrics endpoint response has the name of individual metric. To get more information about these metrics you need to append the metric name to the URL. For example if you want to know more about jvm.memory.used metric then the URL will be http://localhost:8090/actuator/metrics/jvm.memory.used. This URL will give the following response:

{
	"name": "jvm.memory.used",
	"description": "The amount of used memory",
	"baseUnit": "bytes",
	"measurements": [
		{
			"statistic": "VALUE",
			"value": 1.67400072E8
		}
	],
	"availableTags": [
		{
			"tag": "area",
			"values": [
				"heap",
				"nonheap"
			]
		},
		{
			"tag": "id",
			"values": [
				"G1 Old Gen",
				"CodeHeap 'non-profiled nmethods'",
				"G1 Survivor Space",
				"Compressed Class Space",
				"Metaspace",
				"G1 Eden Space",
				"CodeHeap 'non-nmethods'"
			]
		}
	]
}

According to above response, you can get you application JVM memory space consumption, Garbage collectors configure etc.

Same way you can explore more about the application configuration, http requests hits, beans creation information by using actuators other endpoints.

You can also create your own Actuator endpoints. In next post you will learn about the the custom actuator end points creation.

See More : Spring Boot: Custom Actuator EndPoints Creation

Conclusion

In this post you learn about these points for Spring Boot actuators:

  • Spring Boot Actuator configuration and enabling.
  • Spring Boot Actuator endpoints
  • Spring Boot actuator specific endpoints enabling for end users.
  • Spring Boot Actuator health and metrics information retrieval.

Let me know your thoughts on this post.

Happy Learning !!!

Spring Boot Main Class Configuration


A spring boot application starts from the main class (class with main() method) that contains main method with below signature. On Startup of spring boot application it create Spring ApplicationContext and beans also for application..

@SpringBootApplication
public class SpringBootMainClassApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootMainClassApplication.class, args);
    }
}

By default spring boot search the main class in the class path at compile time. if it’s find multiple main class or none then it will throw exception as below.

Spring Boot Application without Main class

While running the application through STS/Eclipse it will show popup as “Selection does not contain a main type”.

Spring Boot Application without Main Class

Spring Boot Application with multiple main class

If you run the application manually through STS/Eclipse then you have to select one of the Main class below before execution. If you are trying to maven package of install from CMD it will throw exception as below.

mvn package -Dmaven.test.skip=true

it will through exception as below in CMD.

Spring Boot Application with Multiple Main Class

Solutions

To start a Spring Boot application, it’s mandatory to specify one Main class in your class path. If there is one Main class as above then will not be any issue to start a Spring boot application.

Now if there are multiple main class in your Spring Boot application then below solutions will help to start your application.

Solution 1: Specify the Main class in pom.xml properties

You can specify your main class in property start-class as below.

<properties>
      <!-- The spring boot main class to start by executing "java -jar" -->
      <start-class>com.FacingIssuesOnIT.SpringBootMainClassApplication</start-class>
</properties>

Note : This property configuration will help only when you added the spring-boot-starter-parent as in your pom.xml otherwise follow the solutions 2 or 3 to configure the main class.

Solution 2 : Specify the Main class in spring-boot-maven-plugin of pom.xml

You can also specify the main class of Spring Boot in spring-boot-maven-plugin of pom.xml as below:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>             
            <configuration>    
                <mainClass>com.FacingIssuesOnIT.SpringBootMainClassApplication</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>

Solution 3: Specify the Main Class on run time through CLI

You can also specify the Spring Boot Main class on runtime through CLI as below:

java -cp spring-boot-app.jar -Dloader.main=com.FacingIssuesOnIT.SpringBootMainClassApplication org.springframework.boot.loader.PropertiesLauncher

By using any of these configuration, You can decide you Spring Boot application main class for start the application.

Conclusion

In this post you learn about the different way to configure Spring Boot Application main class to start the application if your application having multiple Main classes.

Let me know your thoughts on this post.

Happy Learning !!!

[Solved] Spring Boot IllegalStateException: Ambiguous mapping. Cannot map ‘XYZController’ method


In Spring Boot Rest/MVC application, we got this exception “java.lang.IllegalStateException: Ambiguous mapping. Cannot map ‘XYZController’ method” which will not allow to create bean for your controller and also throw “UnsatisfiedDependencyException: Error creating bean with name XYZ” as given in below stack trace.

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'documentationPluginsBootstrapper' defined in URL [jar:file:/C:/Users/saurabh.gupta14/.m2/repository/io/springfox/springfox-spring-web/2.4.0/springfox-spring-web-2.4.0.jar!/springfox/documentation/spring/web/plugins/DocumentationPluginsBootstrapper.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webMvcRequestHandlerProvider' defined in URL [jar:file:/C:/Users/saurabh.gupta14/.m2/repository/io/springfox/springfox-spring-web/2.4.0/springfox-spring-web-2.4.0.jar!/springfox/documentation/spring/web/plugins/WebMvcRequestHandlerProvider.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'helloController' method 
com.FacingIssuesOnIT.controller.HelloController#sayHello()
to {GET [/api/FacingIssuesOnIT]}: There is already 'helloController' bean method
com.FacingIssuesOnIT.controller.HelloController#sayHiHello() mapped.
	at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800) ~[spring-beans-5.3.9.jar:5.3.9]
	at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:229) ~[spring-beans-5.3.9.jar:5.3.9]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1354) ~[spring-beans-5.3.9.jar:5.3.9]
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webMvcRequestHandlerProvider' defined in URL [jar:file:/C:/Users/saurabh.gupta14/.m2/repository/io/springfox/springfox-spring-web/2.4.0/springfox-spring-web-2.4.0.jar!/springfox/documentation/spring/web/plugins/WebMvcRequestHandlerProvider.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'helloController' method 
com.FacingIssuesOnIT.controller.HelloController#sayHello()
to {GET [/api/FacingIssuesOnIT]}: There is already 'helloController' bean method
com.FacingIssuesOnIT.controller.HelloController#sayHiHello() mapped.
	at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800) ~[spring-beans-5.3.9.jar:5.3.9]
	at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:229) ~[spring-beans-5.3.9.jar:5.3.9]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1354) ~[spring-beans-5.3.9.jar:5.3.9]
	... 24 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'helloController' method 
com.FacingIssuesOnIT.controller.HelloController#sayHello()
to {GET [/api/FacingIssuesOnIT]}: There is already 'helloController' bean method
com.FacingIssuesOnIT.controller.HelloController#sayHiHello() mapped.
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1786) ~[spring-beans-5.3.9.jar:5.3.9]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:602) ~[spring-beans-5.3.9.jar:5.3.9]
		... 41 common frames omitted
Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'helloController' method 
com.FacingIssuesOnIT.controller.HelloController#sayHello()
to {GET [/api/FacingIssuesOnIT]}: There is already 'helloController' bean method
com.FacingIssuesOnIT.controller.HelloController#sayHiHello() mapped.
	at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.validateMethodMapping(AbstractHandlerMethodMapping.java:665) ~[spring-webmvc-5.3.9.jar:5.3.9]
	at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.register(AbstractHandlerMethodMapping.java:631) ~[spring-webmvc-5.3.9.jar:5.3.9]
	at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.registerHandlerMethod(AbstractHandlerMethodMapping.java:328) ~[spring-webmvc-5.3.9.jar:5.3.9]
		... 55 common frames omitted

Reason of Exception

The exception “java.lang.IllegalStateException: Ambiguous mapping. Cannot map ‘xyzController’ method” is throw when there is end point of REST urls of two method are same with Verb in your application. So on time of deploy your spring boot application the dispatcher servlet will create mapping of urls with respect to method because two methods having the same name with http verb (GET/POST/PUT/DELETE) then dispatcher got confuse for particular url which method need to point and throw this exception “java.lang.IllegalStateException: Ambiguous mapping”.

Solution

As explained above the reason of this exception. Once this exception happen check for the recently added urls for REST services . This url can be same with in same controller of your application where the exception occurred or match with other controller if no request mapping on class level.

Let me know your thoughts on it.

Happy Learning !!!

Spring Boot Banner Customization Enable / Disable


In Spring Boot by default configure for Banner “Spring Boot” as below which will display on your console with version number.

Spring Boot Banner

Possible Values for Spring Boot Banner

Spring Boot support these three types of configuration for Banner:

  • OFF : Disable printing of Banner
  • CONSOLE : Display Banner to System.out
  • LOG : Display Banner on your Log also.

How to set these Spring Boot Banner Configuration?

There are two ways to configure Spring Boot Banner:

  • Banner setting by application.properties / applictaion.yml

You can add this property in application.properties/ application.yml file to change in configuration of Banner. If you set as off will not display in console and log.

application.properties

##possible values off, console, log
spring.main.banner-mode=console

application.yml

##possible values off, console, log
spring
  main
    banner-mode:console
  • Banner Setting in Spring Boot main() method

You can also change in Spring Boot class main() method to set Banner configuration as below in this case no setting required on application properties file.

Spring Boot Banner Setting
  • Customize Spring Boot Banner

By default Spring Boot show default banner “Spring Boot” with version number. If you want to customize this banner and replace it then create one banner.txt file and place it on your Spring boot application resource folder.

Example:

${Ansi.RED}                  $$$        $$$$$      $$$$         $$$$     $$$$$
${Ansi.GREEN}                  $$$       $$$$$$$     $$$$         $$$$    $$$$$$$
${Ansi.BLUE}                  $$$       $$$$$$$     $$$$$       $$$$$    $$$$$$$
${Ansi.RED}                  $$$       $$$$$$$      $$$$       $$$$     $$$$$$$
${Ansi.GREEN}                  $$$      $$$$ $$$$     $$$$$     $$$$$    $$$$ $$$$
${Ansi.BLUE}                  $$$      $$$$ $$$$      $$$$     $$$$     $$$$ $$$$
${Ansi.RED}                  $$$     $$$$$ $$$$$     $$$$     $$$$    $$$$$ $$$$$
${Ansi.GREEN}                  $$$     $$$$   $$$$     $$$$$   $$$$$    $$$$   $$$$
${Ansi.BLUE}                  $$$     $$$$   $$$$      $$$$   $$$$     $$$$   $$$$
${Ansi.RED}                  $$$    $$$$$   $$$$$     $$$$$ $$$$$    $$$$$   $$$$$
${Ansi.GREEN}                  $$$    $$$$$$$$$$$$$      $$$$ $$$$     $$$$$$$$$$$$$
${Ansi.BLUE}          $$$$   $$$$    $$$$$$$$$$$$$      $$$$ $$$$     $$$$$$$$$$$$$
${Ansi.RED}          $$$$   $$$$   $$$$$$$$$$$$$$$      $$$$$$$     $$$$$$$$$$$$$$$
${Ansi.GREEN}          $$$$$ $$$$$   $$$$       $$$$      $$$$$$$     $$$$       $$$$
${Ansi.BLUE}          $$$$$$$$$$$  $$$$$       $$$$$     $$$$$$$    $$$$$       $$$$$
${Ansi.RED}           $$$$$$$$$   $$$$         $$$$      $$$$$     $$$$         $$$$
${Ansi.GREEN}            $$$$$$$    $$$$         $$$$      $$$$$     $$$$         $$$$

${Ansi.RED} :: Spring Boot${spring-boot.formatted-version} :: ${Ansi.DEFAULT}

Once you will run your application will show updated banner as below.

Customize Spring Boot Banner

Let me know your thoughts on it.

Happy Learning !!!

[Solved] Spring Data JPA :QueryCreationException: Could not create query for public abstract method XYZ


In Spring boot Data JPA this exception QueryCreationException occurred when there is auto creation of Query or mismatch or column name in column/property in Entity class.

Spring Data JPA supports two ways of creating auto generation of query:

  • JQL in Spring data JPA or HQL when using the Hibernate.
  • Spring Data methods in Repository interface for Example : findByUserName() or findByUserNameOrDesignation() etc.

Reason for Exception

Spring Data JPA generate queries based on name of property in your Entity class , If entity based custom methods defined in repository interfaces also generate queries internally based on method name.

If there is any mismatch or incorrect name in custom methods of repository interface when comparing to entity class properties then on compile time it will not through any issue but when you run or deploy spring boot application will through QueryCreationException: Could not create query for method XYZ.

For Example : In User Entity class there is property userName while in custom methods in repository, you are trying to get detail from method findByName() so this property name not exist in User Entity class . Till compile time this method will not show any issue and compile successfully while on deploy time because using Spring Data JPA so will try to fetch data by Name and it will through exception as QueryCreationException: Could not create query for method XYZ.

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'userrepo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository' defined in com.FacingIssuesOnIT.repository.UserRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract java.util.Optional com.FacingIssuesOnIT.repository.UserRepository.findByUsername(java.lang.String)! Reason: Failed to create query for method public abstract java.util.Optional com.FacingIssuesOnIT.repository.UserRepository.findByUsername(java.lang.String)! No property username found for type User!; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.Optional com.FacingIssuesOnIT.repository.UserRepository.findByUsername(java.lang.String)! No property username found for type User!
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:660) ~[spring-beans-5.3.9.jar:5.3.9]
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[spring-beans-5.3.9.jar:5.3.9]
	Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository' defined in com.FacingIssuesOnIT.repository.UserRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract java.util.Optional com.FacingIssuesOnIT.repository.UserRepository.findByUsername(java.lang.String)! Reason: Failed to create query for method public abstract java.util.Optional com.FacingIssuesOnIT.repository.UserRepository.findByUsername(java.lang.String)! No property username found for type User!; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.Optional com.FacingIssuesOnIT.repository.UserRepository.findByUsername(java.lang.String)! No property username found for type User!
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1786) ~[spring-beans-5.3.9.jar:5.3.9]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:602) ~[spring-beans-5.3.9.jar:5.3.9]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.9.jar:5.3.9]
		... 25 common frames omitted
Caused by: org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract java.util.Optional com.FacingIssuesOnIT.repository.UserRepository.findByUsername(java.lang.String)! Reason: Failed to create query for method public abstract java.util.Optional com.FacingIssuesOnIT.repository.UserRepository.findByUsername(java.lang.String)! No property username found for type User!; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.Optional com.FacingIssuesOnIT.repository.UserRepository.findByUsername(java.lang.String)! No property username found for type User!
	at org.springframework.data.repository.query.QueryCreationException.create(QueryCreationException.java:101) ~[spring-data-commons-2.5.3.jar:2.5.3]
	at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lookupQuery(QueryExecutorMethodInterceptor.java:106) ~[spring-data-commons-2.5.3.jar:2.5.3]
	at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lambda$mapMethodsToQuery$1(QueryExecutorMethodInterceptor.java:94) ~[spring-data-commons-2.5.3.jar:2.5.3]
			... 35 common frames omitted
Caused by: java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.Optional com.FacingIssuesOnIT.repository.UserRepository.findByUsername(java.lang.String)! No property username found for type User!
	at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:96) ~[spring-data-jpa-2.5.3.jar:2.5.3]
	at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:107) ~[spring-data-jpa-2.5.3.jar:2.5.3]
		... 57 common frames omitted
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property username found for type User!
	at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:90) ~[spring-data-commons-2.5.3.jar:2.5.3]
	at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:437) ~[spring-data-commons-2.5.3.jar:2.5.3]
	at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:413) ~[spring-data-commons-2.5.3.jar:2.5.3]
		... 61 common frames omitted

Solutions

To solve “QueryCreationException” in Spring boot data JPA follow these steps:

  • Go to stack trace of exception and see which query and methods of repository interface is throwing the exception.
  • Go to repository interface where this particular query and methods are defined and also check the Entity name with respect to repository.
  • Go to Entity class and check the properties name defined in method/queries are same or not.
  • If it’s not same or some typing mistake then only you will get this exception.

Note: Whenever you change any property name / column name in Entity class then always check for dependency in Native Queries/ JQL/HQL and Custom methods in repository otherwise your application will not deploy and fail on runtime.

Let me know your thoughts on it.

Happy Learning !!!

[Solved] Disable Directory Listing in Spring Boot Tomcat server for Web application


One common tomcat web server issue is directory listing that’s can cause to hackers attacked. By default tomcat directory listing is enabled or some developers set as enabled then it’s create a Information Disclosure Issue (leakage of sensitive information).

In your website/application if directory listing is enabled and index page is not configured as index.html or index.php then on typing the context path or URL till directory will display the list of directory and file as below and user/hacker can see all these files sensitive information.

Tomcat Directory Listing Enabled

As you can see from the above screenshot, the directory listing feature generates an output similar to command (‘dir’ or ‘ls’) on an operating system. Directory listing issues can not protect from a SSL certificate. However, These types of issue can be identified by running through vulnerabilities scanner on your application through Microfocus Web inspect.

Solutions

As a solution to disable directory listing depends on the tomcat server because in some of Spring boot application use tomcat as embedded server:

  • Disable directory listing on embedded tomcat in Spring boot
  • Disable directory listing on external tomcat server

Disable directory Listing on embedded tomcat in Spring boot

In Spring boot application where tomcat is defined as embedded tomcat then the application is deployed as jar and there is no web.xml file the you have to define this property setting on your application.properties / application.yml file to disable directory listing.

server.servlet.jsp.init-parameters.listings=false

Disable directory listing on external tomcat server

When you deploy your application using package as war on external tomcat server then you have to make these changes on your web.xml file to disable the directory listing on external tomcat server.

<servlet>
      <servlet-name>default</servlet-name>
      <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
      <init-param>
           <param-name>debug</param-name>
           <param-value>0</param-value>
      </init-param>
      <init-param>
           <param-name>listings</param-name>
           <param-value>false</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
 </servlet>

Here in above xml in web.xml file the init-param tag for listing is defined as false to disable the directory listing for application on web application.

f you are using the other web servers then you can check these configuration through below link.

https://www.netsparker.com/blog/web-security/disable-directory-listing-web-servers/

Let us know your thought on this post.

Happy Learning !!!

JAXB Overview


JAXB (Java Architecture for XML Binding) provides efficient and standard API and concepts to convert java object to XML and XML to Java Object. These conversion are also called as marshalling and unmarshaling as below:

Marshalling: Java Object to XML
Unmarshalling: XML to Java Object

Current Version of JAXB: 2.0

JAXB Marshalling and Unmarshalling

JAXB is a binding framework which provides support to mapping java fields with XML attributes and properties by annotations.

Why to use JAXB?

JAXB is required when need to deal with XML over Java platform because working on XML is not easy and you have to write code for each tags, attribute and condition etc.

By using JAXB framework make your life easy by using annotations, it’s provide annotations which customize your generated XML as per your business requirement and no need to write schema for XML.

Where JAXB Can We Use?

JAXB can we use in following cases:

  1. In webservices communication where one application in Java and another may be different technologies. As XML is platform and Language independent language so through JAXB convert POJO classes to XML through JAXB and call services provided by other application.
  2. Sometimes in distributed systems are connected through SFTP/MFTP so you can keep generated XML file there and another system will pick it.
  3. Most of the third party tools support XML for input/output data medium as XML, so we can use generated XML file for data transformation.

Let me know your thought on it.

Happy Learning !!!


References

https://javaee.github.io/jaxb-v2/

[Solved] JAXBException: Implementation of JAXB-API has not been found on module path or classpath.


JAXBException is the root exception class for all the JAXB exceptions. Generally this exception occurred when the implemented code is not fulfill the pre-requisite for JAXB related dependencies.

Constructors

JAXBException class having following constructors.

  • JAXBException(String msg): Construct exception with detail message..
  • JAXBException(String msg, String errorCode) : This construct exception with detail message with error code.
  • JAXBException(String msg, String errorCode, Throwable exception) : Construct detail message with error code and stacktrace of linked exception stacktrace.
  • JAXBException(String msg, Throwable exception) :Construct exception with detail message and stacktrace of linked exception.
  • JAXBException(Throwable exception) : Construct exception with stacktrace of linked exception.

Problem

This JAXBException occurred in your code because on runtime JAXB is not able to find JAXB-API related dependency.

javax.xml.bind.JAXBException: Implementation of JAXB-API has not been found on module path or classpath.
 - with linked exception:
[java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory]
	at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:278)
	at javax.xml.bind.ContextFinder.find(ContextFinder.java:421)
	at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:721)
	at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:662)
	at com.facingIssuesOnIT.domain.EmployeeJAXBTest.marshal(EmployeeJAXBTest.java:23)
	at com.facingIssuesOnIT.domain.EmployeeJAXBTest.main(EmployeeJAXBTest.java:15)
Caused by: java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
	at javax.xml.bind.ServiceLoaderUtil.nullSafeLoadClass(ServiceLoaderUtil.java:122)
	at javax.xml.bind.ServiceLoaderUtil.safeLoadClass(ServiceLoaderUtil.java:155)
	at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:276)
	... 5 more

Solutions

To implement JAXB marshalling and unmarshalling in your application, you need to add following dependencies in you code. As above exception message explained “JAXB-API has not found in module or class path” this exception occurred when you forget to add JAXB-API dependency.

<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-core</artifactId>
    <version>2.3.0.1</version>
</dependency>

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.1</version>
</dependency>

<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>2.3.1</version>
</dependency>
 
<dependency>
    <groupId>org.javassist</groupId>
    <artifactId>javassist</artifactId>
    <version>3.25.0-GA</version>
</dependency>

Conclusion

In this post , You learn about the JAXBException cases to occurred, different constructors and example to solve JAXB-API has not found in module or class path” JAXBException.

Let me know your thought to about this post.

Happy Learning !!!

[Solved]: JAXB empty Tag without xsi:nil=”true” for null values by using jaxb.properties


Generally when we do marshaling from JAVA object to XML through JAXB, the null values String or other objects not considered while marshalling and xml tag for these objects not generated.

If we need to get xml tags also for null values object then define these property in model class with attribute nillable=true.

 @XmlElement(name = "description", nillable = true)
 private String description;

After adding nillable=true property attribute this will generate output XML tag as below

<description xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>

Problem Statement

Here problem is we don’t want the XML description tag with schema url and xsi:nil=”true” as above. We want only the empty tag for XML as below

<description />

Solutions

Here the problem is when any value in model class property is found as null the JAXB does not call any xmlAdaptor and generate tag with schema link with xsi:nil=”true”.

To solve this problem we can use third party JAXB provider EclipseLink JAXB (MOXy) . It’s required to make below changes on your application and model class.

  • Add this EclipseLink MOXy dependency in you application
<dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>org.eclipse.persistence.moxy</artifactId>
        <version>2.6.8</version>
    </dependency>
  • Add XmlNullPolicy on all the fields of model where need to generate empty tag for null values.
 @XmlElement(name = "description", nillable = true)
 @XmlNullPolicy(emptyNodeRepresentsNull = true, nullRepresentationForXml = XmlMarshalNullRepresentation.EMPTY_NODE)
 private String description;
  • Now we need to set below property in our JVM while marshaling through java object to XML.
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

There are following ways to add above property based on you application need.

Solution 1: In case of simple java application, you can add this line of code before marshaling

System.setProperty("javax.xml.bind.JAXBContextFactory", "org.eclipse.persistence.jaxb.JAXBContextFactory");

Solution 2: Create jaxb.properties file in your each model package/resource folder and add below property

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

Note: Once you will run your code in your local machine it will generate empty tag but you will face problem when deploy application after making maven build because this property file will not add in your generated classes folder model packages. In this case you have to add below lines of code in pom.xml to copy jaxb.properties file in model package.

<plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.6</version>
            <executions>
                <execution>
                    <id>copy-resources01</id>
                    <phase>validate</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${basedir}/target/classes/model package location</outputDirectory>
                        <encoding>UTF-8</encoding>
                        <resources>
                            <resource>
                                <directory>${basedir}/src/main/resources</directory>
                                <includes>
                                    <include>jaxb.properties</include>
                                </includes>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

Solution 3: If your application is Spring boot then you can also pass the below arguments while deploying application.

-Djavax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

or

Add the property in application.properties/application.yml file

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

This solution will also help when you are having multiple packages for model classes and no need to include jaxb.properties in each package.

Conclusion

In this topic you learn about the steps to generate empty tags for XML incase null values for Java objects. you can also learn about the copy of jaxb.properties file in packages by maven. Incase of having multiple packages for model classes you can follow solutions to handle it.

Let me know about your thoughts on these solutions.

Happy Learning !!!

JDBC: ResultSet Interface


The object of ResultSet maintains a cursor pointing to a particular row of data. Initially, cursor points to before the first row.

By default, ResultSet object can be moved forward only and it is not updatable.
But we can make this object to move forward and backward direction by passing either TYPE_SCROLL_INSENSITIVE or TYPE_SCROLL_SENSITIVE in createStatement(int,int) method as well as we can make this object as updatable by:


Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);

Commonly used methods of ResultSet interface:

  1. public boolean next(): is used to move the cursor to the one row next from the current position.
  2. public boolean previous(): is used to move the cursor to the one row previous from the current position.
  3. public boolean first(): is used to move the cursor to the first row in result set object.
  4. public boolean last(): is used to move the cursor to the last row in result set object.
  5. public boolean absolute(int row): is used to move the cursor to the specified row number in the ResultSet object.
  6. public boolean relative(int row): is used to move the cursor to the relative row number in the ResultSet object, it may be positive or negative.
  7. public int getInt(int columnIndex): is used to return the data of specified column index of the current row as int.
  8. public int getInt(String columnName): is used to return the data of specified column name of the current row as int.
  9. public String getString(int columnIndex): is used to return the data of specified column index of the current row as String.
  10. public String getString(String columnName): is used to return the data of specified column name of the current row as String.

For example of ResultSet follow below links:

Learn More on JDBC

Follow below links to learn more on JDBC and solving JDBC related issues :

JDBC: Statement Interface


The Statement interface provides methods to execute queries with the database. The statement interface is a factory of ResultSet i.e. it provides factory method to get the object of ResultSet.

Commonly used methods of Statement interface:

  1. public ResultSet executeQuery(String sql): is used to execute SELECT query. It returns the object of ResultSet.
  2. public int executeUpdate(String sql): is used to execute specified query, it may be create, drop, insert, update, delete etc.
  3. public boolean execute(String sql): is used to execute queries that may return multiple results.
  4. public int[] executeBatch(): is used to execute batch of commands.

For example of statement interface follow below links:

See Also :

More on JDBC

Follow below links to learn more on JDBC and solving JDBC related issues :

Junit : Best Practices


In the previous topics we learn about the unit testing by Junit and implementation of Test Cases by using Junit 5. In this topic we will learn about the some Junit Best Practices and Why it’s required.

Why JUnit Best Practice Needed?

When writing Junit test case, we need to ensure the following points:

  • Test cases should be easily readable
  • Test cases should be extremely consistent and reliable
  • It should indicate the real issues in the production code
  • Tests should fail only when there is a bug in the system
  • It will be good if more test cases executed frequently
  • All the test cases should execute very fast

You can achieve all these Junit features when apply the “Junit Best Practices” when writing the Junit Test cases.

What is the Junit Best Practices for Test Case writing?

These are some high-level points that’s need to keep in mind while writing the test cases for an application or project.

  • Test cases should be readable.
  • The name of the test class and test methods should be descriptive.
  • Develop the JUnit test cases in a way that it should execute faster.
  • Always first plan unit tests for functionalities that have fewer dependencies.
  • Plan for proper tests for all functionalities.
  • Don’t use test case constructor to setup test cases.
  • Avoid skipping test cases with the usage of @Disabled without any reason, a reason could also be passed for @Disabled for any developer to know why a particular test case was is skipped.
  • Each unit test method should have only one assertion.
  • Maintain proper order of parameters in assertions.
  • Practice the resilient assertions.
  • Make each test independent of all others.
  • Don’t go for unit-testing of any settings or configuration.
  • Confirm that unit test code is parted from production code.
  • Do not print anything out in unit tests.
  • Avoid the usage of static members inside the test class, if you have used then re-initialize on each test case.
  • Do not give your own exception that occurs only to fail a test.
  • Don’t write testcases for configuration settings.
  • Plan to run the test cases completely in-memory, avoid doing a test for availing file system and DB, and for HTTP requests, Use mocking Junit framework like Mockito etc. for external or third-party dependency.
  • Implement Code Coverage tools like Cobertura, JACOCO etc. with your project so that verify that written Junit Test cases covering complete code. As per industry standard code coverage ratio should configure as 80%.
  • Integrate test cases with build script(ant or maven or gradle) so that all Junit test case will execute before your application ready for deploy.
  • Capture the results using XML formatter so that make test case report readable with color text.

Note: While writing test case assume these test cases will not execute in sequence so never try to use one test case result on another test case.

Good Tips for Junit Test Cases Writing

Here are some good tips for Junit Test Cases Writing that will guide when writing the test cases for any method in class.

  • Write descriptive name of your test case
TestCreateUser_NullUserId_ShouldThrowException
TestCreateUser_DuplicateUserId_ShouldThrowException
TestCreateUser_ValidUserId_Should_Pass
  • When writing test cases for method arguments then check for each parameters of method for null and these methods should return Invalid parameter exception if any parameter is null.
  • After null unit test implementation write test for functional unit.
  • Each test case should have only one assertion and it should cover one functional condition of code. Like if-else, switch, loops, exception etc.
  • For some methods where need to meet SLA of time or dependent of third-party API result write test cases for @Timeout.
// Timeout after 3 nanoseconds
@Timeout(value = 3, unit = TimeUnit.NANOSECONDS) 
@Test
void annotationDelayInValidTest() throws InterruptedException {
		obj.delay(2); // Delay for 2 seconds
}

JUnit 4

@Test(expected=ArithmeticException.class)
public void testCalculatorDivide()
{
assert(5, obj.devide(5,0))
}

JUnit 5

@Test
void testCalculatorDivide() {
Exception exception = assertThrows(ArithmeticException.class, () -> cobj.division(5, 0));
assertEquals("divide by zero", exception.getMessage());
	}
  • If need to test particular test case for multiple parameter values then write test case as @ParameterizedTest
@ParameterizedTest
	@NullAndEmptySource
	@ValueSource(blankStrings = { " ", "   ", "\t", "\n" })
	void checkText(String sampleText) {
	    assertTrue(sampleText == null || sampleText.trim().isEmpty());
	}

  • Use @Before and @After or @BeforeAll or @AfterAll test method for Initialization and close the dependencies of test method.
  • Use Mocking Junit framework for testing external service or DB related methods.
  • Don’t write test cases for configuration and constant related classes.
  • Ensure tests that are time dependent so that test data will not consider as expired data.
  • Use local when using date and time while implementing test cases.
  • Keeps your test cases on test packages in same project so that maintain easily and skip also in case required.

Summary

No doubt writing Junit increase the code quality of the code and by following the best practices reduce the maintenance and execution time of test. If you keep above points in mind that will help you to write good quality of test cases and make your application reliable and reduce the regression testing time with less defects in your code.

Let me know your views for Junit implementation and Junit Best Practices for writing Unit test cases for your code.

JUnit 5 : Environment Setup


Let us discuss on JUnit 5 environment setup in Java Application.

Pre-Requisite

JUnit 5 does not required any major setup. It’s required following library and IDE:

  • JDK : Min 1.8 or above
  • IDE : Eclipse/STS or Netbean or IntelliJ
  • Latest version of Junit 5 is readily available with latest version of IDE

More :

Junit 5 Setup in Java Application

You can add JUnit 5 library in your Java Application by follow below steps:

  • Right clock on your project -> Properties->Build Path-> Configure Build Path
  • Select Junit Library by following below steps in screen shot.
Junit 5 Environment Setup
  • After clicking in Next button (Step 5). You will get below screen where you can select Junit 5.
JUnit 5 Environment Setup
  • After click on Finish button (Step 7) this JUnit 5 library will add on your application.
  • Now you can click on “Apply” or “Apply and Close” button to complete this process.

Now your application is setup to write test JUnit 5 test cases.

In further topic you will learn about how to write JUnit Test Cases.

Junit 5 : How to write JUnit Test Cases?


In previous topic you learn about the JUnit setup in Java Application. In this topic you will learn about the Junit test cases writing of methods for particular java class.

Lets discuss about the test cases writing in Java application.

  • Right click on your java project -> Select New -> Junit Test Case as below.
How to write Junit Test Cases in Java application
  • After selecting the Junit Test Cases you will get below screen. Here the Jupiter option (Step 1) is for implementing the Junit Test cases with Junit 5. You can write test class name (Step 2) as CalcularTest and also select Fixture (Step 3) if initialization required for your test cases.
How to write Junit Test Case with Junit 5
  • Instead of writing the test cases, If you want to generate the Test cases for particular class then select browse (step 4) option and you will get below screen where you can search for class.
How to generate Junit test cases for particular class
  • After selecting class, click on OK button. You will get next screen with names of method where you can select methods for which you want to generate the test cases.
How to generate Junit test cases
  • After selecting methods click on Finish button, you will see a class CalculatorTest.java class added in you application with some test cases code as below.

CalculatorTest.java

JUnit Test Cases Generated
  • Now your some test cases generated with some dummy code where you can write your own logic inside of these test method by using assertion or assumption and run these test cases.

How to run Test Junit Cases?

Once you will done with the test case writing. You can follow these steps to run test cases:

  • Right click on your Java Application->select run as -> Junit
  • After executing the test cases you will see the test case result as below with red and green colors. Where green represent success and red as failure of test cases.

Junit Failed Cases

Junit some test cases failed

JUnit Success Case

Junit all success

Here I have done some changes in auto generated test cases as below and after execution showing as success as above. You can try with same or update it.

JUnit for success case

In the further topic you will learn more about the Junit 5 Annotations, Fixtures, Assertions and Assumptions in detail with examples.

Junit 5 : Architecture


Junit 5 Architecture mainly divide in three modules:

JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage

JUnit 5 Architecture

In Junit 5 Architecture each module having specific purpose as below:

JUnit Platform

Junit Platform is core of JUnit 5 which provides a bunch of libraries that provide Console Launcher to launch a platform from the command line and invokes JUnit Runner for running Test Engine in JUnit 4 based environment.

JUnit Jupiter

Jupiter provides the APIs for developer to interact assertions and assumptions instead of Runner itself.

JUnit Vintage

By default, JUnit 5 does not provide backward compatibility, instead, it uses another module called as Vintage which provides APIs to write test cases supported by older version JUnit 3 or Junit 4.

Extensions (Third Party)

JUnit 5 supports third-party APIs also on its platform with the help of the Extensions module.

In further topics you will learn about the JUnit 5 Annotations and Implementation of test cases by considering different scenarios.

JUnit 5: Introduction


In the previous topic you learned about the JUnit introduction and compare between different Junit framework like Junit vs TestNG. Here we will discuss about some important points to remember about Junit 5.

  • JUnit 5 is the latest version of JUnit which require a minimum JDK 8 version.
  • Junit 5 supports popular IDEs such as Eclipse/STS and IntelliJ for test case executions.
  • Junit 5 supports all new Java Language features including Lambdas.
  • Junit 5 provides various assertions and assumptions.
  • Junit 5 provides a whole new architecture that is comprised of modules, making it suitable to be used in small memory-based systems.
  • Junit 5 vintage module supports backward compatibility with previous version Junit 3 and Junit 4.

In the further topic you will learn about the JUnit 5 Architecture, JUnit 5 Annotations and Implementation of test cases by considering different scenarios.

JUnit Introduction


  • JUnit is an open-source framework for Java programming language, used to write and run automated unit test cases. It is widely accepted as a standard for unit testing in Java.
  • JUnit is a simple testing framework created by Kent Beck and Erich Gamma during a long airplane trip.
  • It is released under IBM’s common public license version 1.0 and hosted on SourceForge.
  • Till JUnit 3.8 there were no stable versions.
  • It is the Java version of the xUnit architecture for unit and regression testing frameworks
  • According to a survey of 2013, 10,000 Java projects hosted on GitHub were using JUnit as a testing framework.
  • In this tutorial, we will perform unit testing with JUnit 5 which is the latest stable JUnit version.

In the further topics we will discuss more about the Junit basics and implementation of test cases by using JUnit 5 after considering different scenarios and backward compatibility with previous version JUnit 3 and JUnit 4.

Unit Testing


Unit testing is process of Software development which validate that each unit/line of code is performing as expected or not. On broader prospect Unit Testing is defect prevention and detection strategy to reduce software development risks, cost, and time to deliver a quality software.

Developers write Unit Test cases which covers the functionality of specific section of code, function level, class level and branches level so that ensure the implemented code is working as expected. Unit testing not completely verify the functionality of software but it’ ensure that building block of software work independently as expected from others.

Why Unit Testing Framework?

In Unit Testing, we verify each section of code for success and failure scenarios where need to implement the conditions in code Test code by considering the different cases.

Let’s take the below examples of Calculator.java class on addition() method where need to verify output of result by considering different test cases for success and failure.

Calculator.java

public class Calculator {
		public int addition(int a, int b) {
			return a + b;
		}
	}

Test Cases Implementation

In this case for testing this method we need to create one test class i.e CaculatorTest.java and implement the condition for all scenarios of every method test as follows:

Success Case: Pass arguments as a=2, b=3 return result=5, Expected Result =5

Failed Case: Pass arguments as a=2, b=3 return result=Other, Expected Result=5

class CalculatorTest {
private static final Logger logger = Logger.getLogger(CalculatorTest.class.toString());
private Calculator calc = new Calculator();
		void additionTest() {
		int sum = calc.addition(2, 3);
		if (sum == 5) {
		logger.info("Addition method test has passed.");
		} else {
		logger.info("Addition method test has failed.");
			}
		}	
		public static void main(String[] args) {
			CalculatorTest cTest = new CalculatorTest();
		cTest.additionTest();
	}	
	}

Here if you noticed for testing a single small addition() method required to write these many lines of code for creating object of Calculator class, calling method and check for pass and failed scenarios. Apart from it you need monitor the console logs also for verify the expected result. Think of when there are multiple methods in class and software having lots of classes on that case writing Junit code is cumbersome process for developers.

From the above example, Now analyze tasks that need to be done manually with and without framework :

TaskWithout FrameworkWith Framework
PreparationManually Manually
Provide Test Inputs Manually Manually
Run the tests Manually Framework
Provide expected outputs Manually Manually
Verify results Manually Framework
Notify developer if test failed Manually Framework

Now it should be clear that how important to use framework because it saves significant amount of time of developers.

There are lots of framework for Unit Testing but when we talk about the Java unit testing mainly these two are most popular.

  1. Junit
  2. TestNG

TestNG is a testing framework for Java and is inspired by JUnit and NUnit which covers a wide range of testing like unit testing, functional testing, integration testing, and system testing.

In the further topics we will discuss more about the Junit basics and implementation of test cases by using Junit 5 after considering different scenarios.

Software Testing


What is Software Testing?

When developer create a new software and deploy in market for use of public. The success of software depends on popularity of software, but it becomes terrible when users complains bugs, issues in the software. In this case developer need to debug the issues and fixed again for release the software that’s time-consuming process.

To overcome this practice “Software testing” comes in picture. This activity makes sure that every functionality or component of software is working as expected and outcome of software is as expected. The main purpose of software testing is delivered as defect free software.

Why Software Testing?

“Prevention is better better than care”

This phrase is very well suite for software development, If developers do the software testing on the time of development then all the issues will identify and fixed on the time of development which will make the bug free software. Software testing saves a lot of time for developers and reduces the frustration level caused by detecting bugs after deployment.

Software Testing Types

Software testing mainly categories in two types:

Software Testing Types
  1. Manual Testing: In this testing strategy is done manually by a tester to discover the bugs in software and check the results. This is also required because 100% automation testing is not possible.
  2. Automated Testing: In this testing, scripts and codes are written by testers to automate the test cases execution. This testing strategy saves lots of time as well manpower and generate report by automation tool.

Software Testing Hierarchy

In software testing, whether you choose automation or manual testing it can’t complete on one go. It comprises through 4 testing levels as below:

Software Testing Hierarchy

Unit Testing

Unit testing covers all independent parts or components of code i.e. known as Units. Unit testing is the first level of testing which perform on time of the development phase by developers while developing the software.

Integration Testing

Once the unit testing is working fine of all units of code then testing will be preform after integrating them i.e. called the integration testing. In this testing we check the communication between each unit are working as expected or not.

System Testing

Once the integration testing completed for all modules, then testing start for complete software i.e. called as System testing.

Acceptance Testing

After completing the System testing the software is evaluate as per business requirements i.e. called as Acceptance Testing. Once acceptance testing is completed successfully the software deployed to market for general user’s access.

The above testing hierarchy is very high level, there are other sub phases also for software testing, you will get to know by following this link https://en.wikipedia.org/wiki/Software_testing

In the further topics we will discuss more on Unit Testing by using Junit 5 and implementation on time of development.

Junit: Jacoco Code Coverage Plugin Integration with Eclipse/STS


In previous post you learn about the Junit Code Coverage Report generation by using Maven POM.xml that’s help to generate report for auditing purpose and check the status of junit test case implementation but everytime for these report you have to visit directory target/site/jacoco/index.html or target/jacoco-report/index.html

To reduce this effort, we can add this Jacoco plugin our eclipse/STS IDE and directly check the status over IDE.

Integration of JACOCO Code Coverage Plugin in Eclipse/STS

You can follow these steps to integrate this JACOCO code coverage plug in you eclipse or STS IDE.

  • Go to IDE top header Help->Eclipse Market Place it will open the pop up for Eclipse Market Place.
  • Type text as “Code Coverage” in search text box and click “Go” button. You will get below screen
JACOCO Code Coverage Plugin for Eclipse/STS
  • You will get list of plugins select the plugin “EclEmma Code Coverage” click on Install button for this plugin.
  • You will get next screen where need to accept terms and conditions and click continue.
  • After installing this code coverage plugin, It will ask to restart your IDE.
  • After restart your IDE, go to the top Windows->Show View->Others then search for “Coverage” as below then select option click on Open button.
  • You will see one additional tab “Coverage” on your console view section as below.
Coverage tab on view
  • Also get “Coverage As” option as below when right click on your application. Now you can run you Junit.
JACOCO coverage as option
  • Once you run your application Junit by using “Coverage As” option you will see the results on “Coverage” tab as below.
JUnit Code coverage report by Eclipse/STS plugin

To learn more detail about this report and metrics header you can follow this link Junit: Code coverage by using JaCoCo.

Conclusion

In this topic you learn about the steps to add code coverage plugin in you eclipse/STS IDE.

If this port help you to add Code Coverage plugin in you eclipse/STS IDE the like this post and share your comments. In case you are facing issue related to code coverage implementation share with us will try to resolve it.

Junit: Code coverage by using JaCoCo


Why Code Coverage Require while writing JUnit?

Code coverage is a matric indicator that shows the line of code are executed while running the automated JUnit test cases. This helps developer to identify that any unit test is not missing for developed code or newly added functionality of code so that improve the code quality and reduce number of bugs on time of integration testing.

While audit of your application source code auditor also asked for Code Coverage report so that observe your written Junit test cases covering complete application or not.

For Java development there are couple of tools for Code Coverage like Cobertura and Jacoco etc. Here you will learn about the integration of JACOCO with maven project.

JCOCO Code Coverage Plugin Integration with Eclipse/STS

Note : Cobertura support till Java version 8 only while JACOCO support all the Java 5 and above.

Code Coverage Tools generate report in percentage ratio as below in screen shot. Each header section described in detail in this table.

Junit Code Coverage

You can further extend this package section to get more code coverage detail on class and method level. You will get to know more detail in further sections.

Here these color represent as :

  • Green: Code is tested or covered
  • Red: Code is not tested or covered
  • Yellow: Code is partially tested or covered
HeaderDescription
INSTRUCTION
BRANCHAll the if-else or switch statements are consider as branch and represent as diamond in a method that can be executed or missed.
No coverage (Red diamond): No branches in the line has been executed.
Partial coverage (Yellow diamond): Only a part of the branches in the line have been executed.
CYCLOMATIC COMPLEXITYhttps://en.wikipedia.org/wiki/Cyclomatic_complexity
LINEConsider as executed when at least one instruction that is assigned to this line has been executed.
No coverage (Red): No instruction in the line has been executed.
Partial coverage (Yellow): Only a part of the instruction in the line have been executed.
Full coverage (Green): All instructions in the line have been executed
METHODConsider as executed when one of the instruction has been executed.
CLASSConsider as executed when one of the method has been executed.
JUnit Code Coverage Criteria

Implementation of Code Coverage

In this further section you will learn about the implementation of Code Coverage for Junit. Here we are using dependency as below:

  • JUnit 5
  • Maven 4
  • Maven Surefire plugin 3.0.0-M5
  • Jacoco 0.8.2 (Code Coverage library)

POM.XML

You can make following changes in your maven java application pom.xml.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.FacingIssuesOnIT</groupId>
  <artifactId>maven-junit-code-coverage-Jacoco</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>Maven-junit-CodeCoverage-Jacoco</name>
  <description>Maven JUnit Code Coverage</description>
  <properties> 
     <maven.compiler.source>1.8</maven.compiler.source>
     <maven.compiler.target>1.8</maven.compiler.target>    
	<java.version>1.8</java.version>
	<!-- add property to enable or disable code coverage by default id false for Jacoco -->
	<jacoco.skip>false</jacoco.skip>
</properties>
<dependencies>
<!-- Junit 5 -->
	<dependency>
		<groupId>org.junit.jupiter</groupId>
		<artifactId>junit-jupiter-engine</artifactId>
		<version>5.5.2</version>
	</dependency>
</dependencies>
	<build>
		<plugins>
		<!-- Maven plugin for surefire with Dependency junit 5 -->
			<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M5</version>
            <dependencies>
                <dependency>
                    <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-engine</artifactId>
                    <version>5.5.2</version>
                </dependency>
            </dependencies>
        </plugin>
        <!-- Jacoco plug in for code coverage -->
			<plugin>
				<groupId>org.jacoco</groupId>
				<artifactId>jacoco-maven-plugin</artifactId>
				<version>0.8.2</version>
				<configuration>
					<!-- exclude classes for code coverage -->
					<excludes>
						<exclude>**/*fiot/*Application.class</exclude>
						<exclude>fiot/controller/*</exclude>
					</excludes>
				</configuration>
				<executions>
					<execution>
						<goals>
							<goal>prepare-agent</goal>
						</goals>
					</execution>
					<!-- attached to Maven test phase -->
					<execution>
						<id>jacoco-report</id>
						<phase>test</phase>
						<goals>
							<goal>report</goal>
						</goals>
						<!-- default target/site/jacoco/* you can define your directory for code coverage report -->
						<configuration>
							<outputDirectory>target/jacoco-report</outputDirectory>
							<skip>${jacoco.skip}</skip>
						</configuration>
					</execution>
					<!-- Define rule for successful build based on code code coverage criteria -->
					<execution>
						<id>jacoco-check</id>
						<goals>
							<goal>check</goal>
						</goals>
						<configuration>
							<rules>
								<rule>
								<!-- When code coverage is 90% and no class missed then consider for successful build -->
									<element>PACKAGE</element>
									<limits>
										<limit>
											<counter>LINE</counter>
											<value>COVEREDRATIO</value>
											<minimum>0.9</minimum>
										</limit>
										<limit>
											<counter>CLASS</counter>
											<value>MISSEDCOUNT</value>
											<maximum>0</maximum>
										</limit>
									</limits>
								</rule>
							</rules>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>

Here is more detail about the Jacoco code coverage implementation.

Maven Junit Code Coverage Implementation Detail

Now lets create some classes by some code implantation and check this code coverage implementation by implementing Junit.

Here i have created one Maven Java project with below directory/file structure.

Maven Junit Code Coverage Example

Implement some code as below:

Calculator.java

package com.FacingIssuesOnIT;

public class Calculator {

	   public double divide(int a, int b)
	   {
		   if(b==0)
		   {
			   throw new ArithmeticException();
		   }
		   else
		   {
		   return a/b;
		   }
	   }

	    public int multiply(int a, int b) {
	        return a * b;
	    }
	    
	    public int add(int a, int b) {
	        return a + b;
	    }
}

Address.java

package com.FacingIssuesOnIT;

public class Address {
  public String printAddress()
  {
	  return "Facing Issues On IT \n Learn from Others Experience";
  }
}

CalculatorTest.java

You can write your own Junit test cases or use in git hub by following below link. Maven JUnit Code Coverage Example

Junit Test Cases

Code Coverage report

After writing test cases you can execute same by mvn test and verify the report in folder {application path}/target/jacoco-report/index.html

It will generate report as mentioned in below screen. Here Green percentage showing code coverage for JUnit while red showing for missing Junit for code.

Maven Junit Code Coverage Report

To get in-depth detail on code coverage on class and method level you can extend by clicking package.

Maven Junit code coverage report on class level

On further extend by click on class you will het detail on method level.

Maven Junit Code Coverage report on method level

On further extend by click on particular method you will get detail about the line and branch level code coverage.

Maven JUnit Code Coverage on Line and branch level

After collecting all such information you can write junit test cases for all these missing lines/method/branch (red) of statements.

Code Coverage Rules for application build

In the above example, I have define rule for code coverage as 90% instruction execution and missing class as 0. Even If our implemented Junit executed successfully but code coverage criteria is not match with define rules then our maven install will fail and throw exception as below .

[ERROR] Failed to execute goal org.jacoco:jacoco-maven-plugin:0.8.2:check (jacoco-check) on project maven-junit-code-coverage-Jacoco: Coverage checks have not been met. See log for details. -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.

Once all the missing Junit will implement and criteria will match successful then build sucessfully.

Conclusion

In this blog you learn about the implementation of code coverage for Unit and defining rule to make build. Some times while audit of you application code quality auditor also asked about the JUnit code coverage report so that auditor can verify the written Junit covering the complete source code of your application.

References

https://www.eclemma.org/jacoco/trunk/doc/maven.html

Source Code

Download Maven Junit Code Coverage Source Code

If this example this Junit Code Coverage detail help you like post make comments. In case you are facing any issue while implementing share detail will try to help you.

[Solved]: Maven Error “Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project XYZ: There are test failures.”


While running through Run As-> Junit executing Junit test cases but when run through Maven test throwing below error.

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.916 s
[INFO] Finished at: 2021-05-08T04:50:23+05:30
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project maven-junit-code-coverage-Jacoco: There are test failures.
[ERROR] 
[ERROR] Please refer to D:\Workspace\maven-junit-code-coverage-Jacoco\target\surefire-reports for the individual test results.
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

Problem

In maven java project, maven-surefire-plugin is required for any types of reports generation because mvn test command execute the test cases but there is not added the plug in for maven-surefire-plugin that’s why maven is not able to execute test cases.

Solution

To execute your test cases with Junit in maven Java project, you can add Junit dependency with maven-surefire-plugin.

JUnit 5 Dependency

<dependencies>
	<dependency>
		<groupId>org.junit.jupiter</groupId>
		<artifactId>junit-jupiter-engine</artifactId>
		<version>5.5.2</version>
	</dependency>
</dependencies>

Maven Surefire Report Plugin

<build>
		<plugins> 
			<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M5</version>
            <dependencies>
                <dependency>
                    <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-engine</artifactId>
                    <version>5.5.2</version>
                </dependency>
            </dependencies>
        </plugin>
</plugins>
</build>

I have tested this solution with Junit 5 and working perfectly fine for me. You can change this dependency in case using Junit 4.

I have solved this problem by using above solution. If you know any other way to solve it write in comments to help others.

[Solved]: Maven Error “Source option 5 is no longer supported. Use 6 or later”


While build my maven java project getting below exception.
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project Sym360: Compilation failure: Compilation failure: 
[ERROR] Source option 5 is no longer supported. Use 6 or later.
[ERROR] Target option 1.5 is no longer supported. Use 1.6 or later.
[ERROR] -> [Help 1]

Problem

This issue is because the maven is not able to identify which version of java compiler need to use while build your code.

Solution

There are two solution to handle such problem.

  1. If you are using simple maven java application then you can add these lines in you pom.xml properties tag .
<properties> 
   <maven.compiler.source>1.8</maven.compiler.source>
   <maven.compiler.target>1.8</maven.compiler.target>
</properties>
  1. If you application is spring boot application and defining maven compiler then add these lines in your pom.xml build tag.
<build>
   <plugins>
   <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
    </plugin>
   </plugins>
   </build>

Based on your code written and compatibility of java version, you can update this java version like java 11 etc.

I have solved this problem by using these two solution. If you know any other way to solve it write in comments to help others.

[Solved]: IOError in Python


In Python, IOError is subclass of EnvironmentError. IOError is occurred when an input/output operation like open() file, or a method or a simple print statement is failed due to IO reason like “Disk full” or “File not found”

Example of Python IOError

In the below code, the xyz.txt file is not exist because of that the program will throw IOError exception. This IOError handle in the program through try-except block.

import sys
def readFile():
    try:
        f = open ( "xyz.txt", 'r' )
    except IOError as e:
        print (e)
readFile()

Output

[Errno 2] No such file or directory: 'xyz.txt'

This IOError exception message will print only when exception occurred.

Conclusion

In Python, IOError is a result of incorrect file name or file path. IOError occurred in multiple cases and these conditions can be handled using try except code block. Implementation way to handle IOError by try-except can check by above example.

[Solved]: AssertionError in Python


What is Assertion in Python?

Assertion is use in programming language to declare condition which should be validate as true by using assert statement prior to running the module or line of code. If assert condition is true then program control moves to next line in case it’s false the program stops running and throw AssertionError exception.

Python Syntax of Assertion

assert condition, error_message(optional)

Where to use Assertion?

In Python, Assertion can be use in following cases:

  • Checking valid input/type.
  • Checking values of parameters.
  • Detecting abuse of an interface by another programmer.
  • Checking output of a function.

Python AssertionError Example

x = 10
z = 0
# denominator can't be 0
assert z != 0, "Invalid Operation" 
print(x / z)

Output

Traceback (most recent call last):
  File "/home/xyz.py", line 4, in 
    assert z!=0, "Invalid Operation"
AssertionError: Invalid Operation

In this above Python program, The default exception handler will print the error message written by the programmer, or else will just handle the error without any message.

Solution AssertionError exception

AssertionError is subclass of Exception class, when this exception AssertionError occurs in program there are two ways to handle, either default exception handler or user handle it.

try:
    x = 10
    z = 0
    assert z != 0, "Invalid Operation"
    print(x / z)
  
#The configured error message will print in log
except AssertionError as err: 
    print(err)

Output

Invalid Operation

Conclusion

In this topic you learn about the Assertion in programming language and how to use it. In case any AssertionError occurs then you can handle it through Try-except exception handling.

[Solved]: Python SyntaxError: (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3: truncated \UXXXXXXXX escape


In Python, This SyntaxError occurred when you are trying to access a path with normal String. As you know ‘/’ is escape character in Python that’s having different meaning by adding with different characters for example ‘\n’ is use for ne line , ‘\t’ use for tab.

This error is considered as SyntaxError because unicode forward slash (\) is not allow in path.

In further section of topic you will learn how to handle this problem in Python while writing path of file to access it.

Example of SyntaxError of Unicode Error

Lets take below example to read CSV file in Windows operating system.

import csv
with open('C:\Users\saurabh.gupta\Desktop\Python Example\input.csv','r') as csvfile:
    reader=csv.reader(csvfile)
    for record in reader:
        print(record) 

If you notice the above code is having path for windows file system to access input.csv. In windows path mentioned by using forward slash (\) while in Python programming forward slash(\) is use for handling unicode characters. That’s why when you execute the above program will throw below exception.

Output

File "C:/Users/saurabh.gupta14/Desktop/Python Example/ReadingCSV.py", line 2
    with open('C:\Users\saurabh.gupta\Desktop\Python Example\input.csv','r') as csvfile:

Solution

The above error is occurred because of handling forward slash(\) as normal string. To handle such problem in Python , there are couple of solutions:

1: Just put r in path before your normal string it converts normal string to raw string:

with open(r'C:\Users\saurabh.gupta\Desktop\Python Example\input.csv','r') 


2: Use back slash (/) instated of forward slash(\)

with open('C:/Users/saurabh.gupta/Desktop/Python Example/input.csv','r') 

3: Use double forward slash (\\) instead of forward slash(\) because in Python the unicode double forward value convert in string as forward slash(\)’

with open('C:\\Users\\saurabh.gupta\\Desktop\\Python Example\\input.csv','r') 

If this solution help you , Please like and write in comment section or any other way you know to handle this issue write in comment so that help others.

[Solved]: StopIteration in Python


In Python, StopIteration is an exception which occurred by built-in next() and __next__() method in iterator to signal that iteration is done for all items and no more to left to iterate.

Example of StopIteration

In this example string value “FacingIssuesOnIT” is Iterating to print character. In this case while loop will run indefinitely and call next() method on iterable value to print value.

iterable_value = 'FacingIssuesOnIT'
iterable_obj = iter(iterable_value)
 
while True:
    try: 
        # Iterate by calling next
        item = next(iterable_obj)
        print(item)
    except StopIteration as err:
        print('Stop Iteration occured')
        break

Output

F
a
c
i
n
g
I
s
s
u
e
s
O
n
I
T
Stop Iteration occurred

In this program after completing the iteration next() element print of iterable_value when it goes to next element print it will throw StopIteration exception because there is no more element in iterable_value.

Solution

Whenever you apply the next() method of iterable object always check the length of iterable object then run the loop to get element by next() method.

[Solved]: AttributeError in Python


In Python, AttributeError is sub class of Exception. AttributeError occurred when an attribute reference or assignment fails because pointing object does not support attribute references or attribute assignment.

Example AttributeError

In this Python program, Test class is having attribute p and initialize with 0 inside the constructor __init__().

class Test:
    def __init__(self):
        self.p = 0
f = Test()
print(f.p)
print(f.q)

Output

    print(f.q)

AttributeError: 'Test' object has no attribute 'q'

This Python program is throwing AttributeError in line 6 because attribute q is not declared inside the class Test but trying to print through program that’s why Python throw AttributeError.

Solution

To solve this problem you can handle exception in program by using try and except block.

try:
    class Test:
        def __init__(self):
            self.p = 0
    f = Test()
    print(f.p)
    print(f.q)
except Exception as e:
    print (e)
    print ('This is an example of StandardError exception')

Output

'Test' object has no attribute 'q'
This is an example of AttributeError exception

Conclusion

In this topic you learn about the case when the AttributeError occurred in Python program and way to handle this error through exception handling.

[Solved]: Python FloatingPointError


In Python, FloatingPointError is subclass of ArithmeticError. FloatingPointError occured with floating point operations when floating point exception control (fpectl) is turned on.
To Enable fpectl in Python requires an interpreter compiled with the –with-fpectl flag.

Note : In Python, fpectl module is not built by default, you have explicitly import it to control over the floating point units from several hardware manufacturer. This allow the use to turn on the generation of SIGFPE whenever any IEEE-754 exceptions Division by Zero, Overflow, or Invalid Operation occurs.

Example FloatingPointError

The below example is created to occurs FloatingPointError in Python program where just trying exponential of number with certain values before and after enabling the fpectl.

import sys
import math
import fpectl
try:
    print ('FPECTL Control Not Enable:', math.exp(750))
    fpectl.turnon_sigfpe()
    print ('FPECTL Control Enabled:', math.exp(750))
except Exception as err:
    print (err)
    print (sys.exc_type)

Output

FPECTL Control Not Enable: 1.01423205474e+304
FPECTL Control Enabled: in math_1
<type 'exceptions.FloatingPointError'>

Solution

When dealing with any floating point number and performing operation where precision values can be more always handle Exception.

To learn more on exception handling follow the link Python: Exception Handling.

If this blog for solving FloatingPointError help you to resolve problem make comment or if you know other way to handle this problem write in comment so that it will help others.

[Solved]: Python OverflowError : (34, ‘Result too large’)


In Python, OverflowError is subclass of ArithmeticError. This error occurred for floating points numbers when size exceed the limit of variable type.

In case of Integer when size grow variable convert to long value. If variable size exceed the limit of Long also then Python throw MemoryError.

Example of OverFlowError

In this Python program we are just continually multiplying the value for floating numbers with 2 as long as for loop condition match (50 times) because it’s floating number and having limit of size once this size limit will exceed will throw exception as OverflowError.

i=1
try:
    f = (2.0**i)
    for i in range(50):
        print (i, f)
        f = f ** 2
except OverflowError as err:
    print ('Overflowed on power ', f, err)

Output

0 2.0
1 4.0
2 16.0
3 256.0
4 65536.0
5 4294967296.0
6 1.8446744073709552e+19
7 3.402823669209385e+38
8 1.157920892373162e+77
9 1.3407807929942597e+154
Overflowed on power  1.3407807929942597e+154 (34, 'Result too large')

If you noticed this Python program this is throwing error as “OverflowError: (34, ‘Result too large’)” because floating variable size is continuesly increasing once it will reach to 34 for precision will throw OverflowError.

Solution

In Python, when you are performing operation of floating number and these operations are inside the recursive method or loops then always handle the OverflowError by try catch block so that your program will not terminate.

[Solved]: Python NameError: name ‘XYZ’ is not defined


In Python, NameError occurred for identifier when it’s being used but not defined in local or global scope so Python will able to find and throw exception. NameError can be occurred by following reasons:

1: Misspelled built-in function
2: Using unidentified variables
3: Define variable after used
4: Incorrect Usage of Scope

Note : In any programming language Identifier is name of variable, function, class or object.

We will discuss in detail about all these above cases with example.

Example : Misspelled built-in function

In this example by mistake not type correct name for print function. I have written as ‘prin’ instated of ‘print’ that’s why Python with throw NameError.

sugar_weight = input("Enter how much sugar you want: ")
prin(sugar_weight)

Output

   prin(sugar_weight)

NameError: name 'prin' is not defined

Example : Using unidentified variables

In this example calculating the sub of variable ‘A’ and ‘B’ but variable B is not defined that’s why Python with throw NameError.

A=5
sum=A + B
print("Sum:"+sum)

Output

   sum=A + B

NameError: name 'B' is not defined

Example : Define variable after used

In this example the variable sum is getting printed before going declare or assigned that’s why Python with throw NameError.

A=5
B=10
print("Sum:"+add)
add=A + B

Output

 print("Sum:"+add)

NameError: name 'add' is not defined

Example : Incorrect Usage of Scope

In this example the avg variable is having local scope with in the function calculateAverage but trying to access for print outside the function. In this case This avg variable will be undefined out the calculateAverage() method then Python will throw NameException.

def calculateAverage(numbers):
    avge = sum(numbers)/len(numbers)

numbers=[10,20,30,40,50] 
calculateAverage(numbers)
print(avge)

Output

  print(avge)

NameError: name 'avge' is not defined

Solution for NameError

In Python NameError related to variable or function name must be fix by modifying the name of variable or correct the code. In case NameError can occurred on runtime then you can explicitly use try and except block to handle NameError.

def calculateAverage(numbers):
    avge = sum(numbers)/len(numbers)
try:
	numbers=[10,20,30,40,50] 
	calculateAverage(numbers)
	print(avge)
except NameError:
    print ("NameError occured in code because some variable isn't defined.")

Output

NameError occured in code because some variable isn't defined.

Conclusion

In this topic you learn about the different cases where NameError can be occurred. It’s explained with example for NameError and finally provided solution to handle NameError by exception handling.

To learn more on exception handling follow the link Python: Exception Handling.

If this blog for solving NameError help you to resolve problem make comment or if you know other way to handle this problem write in comment so that it will help others.

[Solved] : Python TypeError: ‘builtin_function_or_method’ object is not iterable


In Python, When in built-in function used it must be specify with parenthesis (()) after the name of the function. If you try to run or iterate the program over a built-in method or function without parenthesis (()) the Python will throw exception as “TypeError: builtin_function_or_method is not iterable”.

Exampe TypeError: builtin_function_or_method is not iterable

Let’s consider the scenario of successful execution of a built in function in Python.

fruits = ["Papaya", "Orange", "Grapes", "Watermelon", "Apple"]
print(", ".join(fruits))

In Python, The join() is a built-in function which turns a list into a string and adds a separator between each value in a string. The output of code as below.

Output

Papaya, Orange, Grapes, Watermelon, Apple

In case while writing code, you forget the brackets (()) in built-in function then Python will throw an error. In this below scenerio will throw exception as “TypeError: builtin_function_or_method is not iterable

user = {
    "name": "Saurabh Gupta",
    "age": 35,
    "city": "Noida"
}
#iterate user dictionary
for key, value in user.items:
    print("Key:", key)
    print("Value:", str(value))

Output

File "C:/Users/saurabh.gupta/Desktop/Python Example/test.py", line 7, in <module>
    for key, value in user.items:

TypeError: 'builtin_function_or_method' object is not iterable

The above example is throwing as “TypeError: ‘builtin_function_or_method‘ object is not iterable” because while using items function of dictionary programmer missed to write parenthesis (()) because for loop is iteration operation and it’s required Iterable object but items method is used without parenthesis that’s why Python is considering as object and throwing exception as “TypeError: ‘builtin_function_or_method’ object is not iterable“.

Solution

To resolve such problem related to built-in function or any function always write method with parenthesis (()).

You make correct the above program by writing items method with parenthesis as below in line no 7

user = {
    "name": "Saurabh Gupta",
    "age": 35,
    "city": "Noida"
}
#iterate user dictionary
for key, value in user.items():
    print("Key:", key)
    print("Value:", str(value))

Output

Key: name
Value: Saurabh Gupta
Key: age
Value: 35
Key: city
Value: Noida

Conclusion

This type of exception “TypeError: builtin_function_or_method is not iterable” is common when user forget to use parenthesis (()) while using built-in function.

To learn more on exception handling follow the link Python: Exception Handling.

If this blog for solving TypeError help you to resolve problem make comment or if you know other way to handle this problem write in comment so that it will help others.

[Solved]: Python ValueError: invalid literal for int() with base 10


Python supports explicit type conversion by converting values to different data types. In Python you can convert integers to strings, strings to integers, floats to integers but one conversion Python does not support a float as a string to an integer. In if you try to convert from float as an string to integer then it throw exception as “ValueError: invalid literal for int() with base 10“.

Example: ValueError: invalid literal for int() with base 10

Let’s take a below example where asking weight as input from user, as you know weight can be float value. In this example if user input weight more than 10 KG then user will get discount of 100 Rs. otherwise not discount.

sugar_weight = input("Enter how much sugar you want: ")

sugar_weight_as_int = int(sugar_weight)

if sugar_weight_as_int > 10:
    print("You have discount of 100/- Rs.")
else:
    print("You do not have discount on sugar.")

Output

Enter how much sugar you want: 6.5
Traceback (most recent call last):
  File "main.py", line 3, in <module>
    sugar_weight_as_int = int(sugar_weight)
ValueError: invalid literal for int() with base 10: '6.5'

In this example throw exception as “ValueError: invalid literal for int() with base 10: ‘6.5’” because here user insert the sugar weight as 6.5 which is float string value now to comparing with integer value type casting as int but Python doesn’t support type casting from float string to integer that’s why Python will throw exception as “ValueError: invalid literal for int() with base 10: ‘6.5’“.

If you noticed the above error messages have two parts:

1: ValueError: This error occured when there is an issue with the value stored in a particular object.
2: Error Message “invalid literal for int() with base 10: ‘6.5’” : This means the value we have passed through an int() method cannot be converted because the int() method does not allow you to pass a float represented as a string.

Solution

To resolve this issue, you should convert this passing float string value to float then this exception will get resolve because Python allows float string to float conversion.

sugar_weight = input("Enter how much sugar you want: ")

sugar_weight_as_int = int(float(sugar_weight))

if sugar_weight_as_int > 10:
    print("You have discount of 100/- Rs.")
else:
    print("You do not have discount on sugar.")

Output

Enter how much sugar you want: 6.5
You do not have discount on sugar.

Conclusion

In Python “ValueError: invalid literal for int() with base 10” error is occurred when you try to convert a string value that is not formatted as an integer. To overcome this issue you can use the float() method to convert a floating-point number in a string to an integer. Then, you can use int() to convert your number to an integer.

If this solution does not work, make sure the input value of a string does not contain any letters because Strings with letters cannot be converted to an integer unless those letters have a special meaning.

To learn more on exception handling follow the link Python: Exception Handling.

If this blog for solving ValueError help you to resolve problem make comment or if you know other way to handle this problem write in comment so that it will help others.

[Solved]: Python IndexError: tuple index out of range


In Python, lists, tuples are indexed. It means each value in a tuple is associated with index position (0 to n-1) to access that value. Where N represent the total number of values in list or tuple. When user try access an item in a tuple that is out of range the Python returns an error that says “IndexError: tuple index out of range”.

Example: IndexError: tuple index out of range

Lets consider a below example for tuple of fruits. Where index of value start from 0 and up to (number of element -1).

fruits = ("Apple", "Banana", "Grapes", "Papaya", "Litchi")

This tuple fruits is having five values and each element is associated with index number as below:

AppleBananaGrapesPapayaLitchi
01234

To access the value “Grapes” from fruits tuple, we would use this code:

print(fruits[2])

Our code returns: Grapes. here we accessing the value at the index position 2 and print it to the console. Same way we can try with other values in tuple.

Now lets consider an example to create this IndexError, Try to access value by using index value out of range (3 to 6) where index position 5 is out of range and this example will throw exception as “IndexError: tuple index out of range“.

fruits = ("Apple", "Banana", "Grapes", "Papaya", "Litchi")
for i in range(2, 6):
    print(fruits[i])

Output

Grapes
papaya
Lichi

Traceback (most recent call last):
  File "main.py", line 4, in <module>
    print(fruits[i])
IndexError: tuple index out of range

Our code prints out the values Grapes, Papaya and Litchi. These are the last three values in our tuple. Then throw exception as “IndexError: tuple index out of range” because index position 5 is out of the range for elements in the tuple.

The Solution

Our range() statement creates a list of numbers between the range of 2 and 6. This number list is inclusive of 2 and exclusive of 6. Our fruits tuble is only indexed up to 4. This means that our loop range will try to access a fruit at the index position 5 in our tuple because 5 is in our range.

Now lets try to run this below updated program for loop range 2 to 5 then observe the result. To learn more on for loop follow link Python: for loop

fruits = ("Apple", "Banana", "Grapes", "Papaya", "Litchi")
for i in range(2, 5):
    print(fruits[i])

Output

Grapes
papaya
Litchi

Our code successfully prints out the last three items in our list because now accessing items at the index positions 2, 3, and 4 which is in range of fruit tuple indexes.

Conclusion

The IndexError: tuple index out of range error occurs when you try to access an item in a tuple that does not exist. To solve this problem, make sure that whenever you access an item from a tuple that the item for which you are looking exists.

To learn more on exception handling follow the link Python: Exception Handling.

If this blog for solving IndexError help you to resolve problem make comment or if you know other way to handle this problem write in comment so that it will help others.

[Solved]: Python TypeError: ‘int’ object is not subscriptable


In Python some objects are subscriptable. It means that they contain, or can contain, other objects. Integers are used to store whole numbers so that are not a subscriptable object. If programmer treat an integer like a subscriptable object, an error will be raised like “TypeError : ‘int’ object is not subscriptable“.

If you noticed this is TypeError and it occurs when you try to perform operation that’s doesn’t support on object for example when you concatenate string and int then it will throw TypeError . You can more detail on [Solved] TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’.

Example : TypeError: ‘int’ object is not subscriptable

In this example, user is inserting date of birth in the format of DDMMYYYY then parse this date in Date (DD), Month(MM) and Year(YYYY).

dob = int(input("When is your date of birth? (ddmmyyyy) "))

day = dob[0:2]
month = dob[2:4]
year = dob[4:8]

print("Day:", day)
print("Month:", month)
print("Year:", year)

Output

When is your date of birth? (ddmmyyyy) 19051987
Traceback (most recent call last):

File “”, line 1, in
runfile(‘C:/Users/saurabh.gupta/Desktop/Python Example/Exception Test.py’, wdir=’C:/Users/saurabh.gupta/Desktop/Python Example’)

File “C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py”, line 705, in runfile
execfile(filename, namespace)

File “C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py”, line 102, in execfile
exec(compile(f.read(), filename, ‘exec’), namespace)

File “C:/Users/saurabh.gupta/Desktop/Python Example/Exception Test.py”, line 30, in
day = dob[0:2]

TypeError: ‘int’ object is not subscriptable

In this above example, user input date of birth (19051987) and convert this date to int. Which is now whole number and not subscriptable. now in code try to parse this date of birth in form of Day (DD), Month (MM) and Year (YYYY). Because it’s whole integer number when you try to parse this integer value will through exception as “TypeError: ‘int’ object is not subscriptable

Solution

In this above program issue is because programmer is explicitly type casting the string date of birth to integer which is whole number and not subscriptable to resolve this problem programmer need to remove this explicit integer type casting as below.

dob = input("When is your date of birth? (ddmmyyyy) ")

day = dob[0:2]
month = dob[2:4]
year = dob[4:8]

print("Day:", day)
print("Month:", month)
print("Year:", year)

Output

When is your date of birth? (ddmmyyyy) 19051987
Day: 19
Month: 05
Year: 1987

To learn more on exception handling follow the link Python: Exception Handling.

If this blog for solving KeyError help you to resolve problem make comment or if you know other way to handle this problem write in comment so that it will help others.

[Solved]: Python KeyError: XYZ in Python


In Python, KeyError occurred when try access a value in dictionary by key name but key don’t exist. If key found in dictionary will return a value if doesn’t exist then through KeyError : key_name .

How to handle KeyError in Python?

You can handle KeyError while accessing the key from dictionary by following ways:

  • Check for key in advance for accessing the key
  • Use the ‘in’ keyword to check for key
  • Use try and except block.

Example of KeyError in Python

Lets take simple example where user want to access key from dictionary to retrieve value. In this example if user input key name, age or city either of them then it will return value. if user input other value as key that doesn’t exist in dictionary then it will throw exception as KeyError: passing_key.

user = {
    "name": "Saurabh Gupta",
    "age": 35,
    "city": "Noida"
}

key_name=input("What information you want to get? (name, age, city)")
print(key_name+" :"+user[key_name])

Now as suggested above solution to KeyError, lets fix the above to problem to exception handle and show message in case user input other keys except in dictionary.

Solution 1 : Iterate all key and value

Lets take first solution to get keys from dictionary then retrieve values from dictionary.

user = {
    "name": "Saurabh Gupta",
    "age": 35,
    "city": "Noida"
}
#iterate user dictionary
for key, value in user.items():
    print("Key:", key)
    print("Value:", str(value))

In this solution user will able to print all values w.r.t each key.

Solution 2 : Advance check by in

Lets take second solution to fix this problem to check key in dictionary first by using ‘in’ in if statement. if key not available in dictionary then print else statement.

user = {
    "name": "Saurabh Gupta",
    "age": 35,
    "city": "Noida"
}

key_name=input("What information you want to get? (name, age, city)")
if key_name in user:
    print(key_name+" :"+user[key_name])
else:
    print(key_name +" key is not available in user")

in this solution if user input any key like name, age or city then print value otherwise print as key is not available.

Solution 3 : try and except

Lets take third solution to solve KeyError by using exception handling through try and except.

user = {
    "name": "Saurabh Gupta",
    "age": 35,
    "city": "Noida"
}

key_name=input("What information you want to get? (name, age, city)")
try:
    print(key_name+" :"+user[key_name])
except KeyError:
    print(key_name +" key is not available in user")

In this solution if key is correct then print value w.r.t key in dictionary. if key doesn’t exist and KeyError exception occurs the print statement as “Key is not available in user”.

To learn more on exception handling follow the link Python: Exception Handling.

If this blog for solving KeyError help you to resolve problem make comment or if you know other way to handle this problem write in comment so that it will help others.

[Solved]: Python SyntaxError


What is Syntax in programming Language?

In any programming language, Syntax are set of rules which dictate how program need to written. Every language have different syntax from other language like C, C++, Java, C# having curely bracket ({}) to represent block of statements while in Python blocks like if-else, loops, functions etc. represent by indentations. We can also think of example of our spoken language having grammar, punctuation etc.

You can identify these exceptions on runtime when you code give SyntaxError the you can fix it. In Python, Sometime programmer omit some set statement, not following indentation or programmer move from another language put curly bracket({}),semi colon (:) etc. then it show SyntaxError because python doesn’t support that.

In this blog you will learn about the some of the common Python SyntaxErrors, provide examples, and show you how to fix them.

Example 1:

In this below example where calculating average of numbers and returning final result to print but this simple example will through exception as in output

numbers = [8, 7, 9, 8, 7]

def calculate_average_age():
    average = sum(numbers) / len(numbers)
    print(average)
return average

total_average = calculate_average_age(numbers)
print(total_average)

Output

File “C:/Users/saurabh.gupta/Desktop/Python Example/Exception Test.py”, line 12
return average
^
SyntaxError: ‘return’ outside function

Solution

The above issue is because of indentation of return statement that’s why it’s showing in error as ‘return is outside the function’. To fix this problem return should be same intended as other statement of function as below.

numbers = [8, 7, 9, 8, 7]

def calculate_average_age():
    average = sum(numbers) / len(numbers)
    print(average)
    return average
total_average = calculate_average_age(numbers)
print(total_average)

Example 2

Let’s take another example of dictionary which will print the elements as key and value but this program will through exception as “SyntaxError: Invalid syntax“.

user = {
    "name": "Saurabh Gupta",
    "age": 35
    "city": "Noida"
}
#iterate user dictionary
for key, value in user.items():
    print("Key:", key)
    print("Value:", str(value))

Output

File “C:/Users/saurabh.gupta/Desktop/Python Example/Exception Test.py”, line 4
“city”: “Noida”
^
SyntaxError: invalid syntax

Solution

In this above example, if you noticed this exception occurred in the line 4 inside the dictionary. It’s because of missing commas in each element for key and value that’s why it’s showing exception as “SyntaxError: invalid syntax“.

You can modify the above code after adding comma in dictionary as below:

user = {
    "name": "Saurabh Gupta",
    "age": 35,
    "city": "Noida"
}
#iterate user dictionary
for key, value in user.items():
    print("Key:", key)
    print("Value:", str(value))

Output

The above line of updated code will return output as below:

Key: name
Value: Saurabh Gupta
Key: age
Value: 35
Key: city
Value: Noida

Summary

In this topic you learn about the Syntax inside the programming language and in case any SyntaxError happen in your code you can identify the problem by checking he stack trace of logs by line number then identify the problem accordingly fix it.

Learn Python exception handling in more detain in topic Python: Exception Handling

[Solved] Python ValueError: math domain error


In Python, some of the mathematics operation doesn’t support negative or zero values. In this case when you perform operation with negative value then on runtime Python throws exception as “ValueError: Mathematics Domain Error” .

Here are some operations like sqrt() , log() etc. method which doesn’t work with negative values.

Example:

In this below example, we will try to create scenario by passing both positive and negative numbers and see the results.

import math

number = input("Please insert a number: ")

square=math.sqrt(int(number))

print("Square of number "+str(number) +" is :"+str(square))

Output

Passing Positive Number : 5

Please insert a number: 5
Square of number 5 is :2.23606797749979

Passing Negative Number: -5

File “C:/Users/saurabh.gupta/Desktop/Python Example/Exception Test.py”, line 5, in
square=math.sqrt(int(number))

ValueError: math domain error

After passing the value as -5 it’s throwing the runtime exception as “ValueError:math domain error“.

Solution

The solution to handle such situation is apply condition which check for negative values in case any negative values pass by user the show message to user as this operation is not allow for negative values.

import math

number = input("Please insert a number: ")
if(int(number)>0):
    square=math.sqrt(int(number))
    print("Square of number "+str(number) +" is :"+str(square))
else:
    print("Negative values are not allow for SQRT() operation")

Output

Passing Positive Number : 5

Please insert a number: 5
Square of number 5 is :2.23606797749979

Passing Negative Number : -5

Please insert a number: -5
Negative values are not allow for SQRT() operation

If you noticed from above program in solution, I just put one condition to check the value of input number to resolve the problem “ValueError: math domain error

[Solved] Python ValueError: too many values to unpack (Expected X)


In Python, When bags are not unpack well then it throws exception as “ValueError: Too many values to unpack (Expected X) . This exception generally occurs in two cases:

  • When you try to iterate over a dictionary and unpack its keys and values separately.
  • When you forget to unpack every item from a list to a variable.

If you noticed this is ValueError and it’s occurred when you try to access value that doesn’t exists. Values can be any type like object, string, list or dictionary.

Example 1

In this below dictionary iteration example, print values of key and value from user dictionary but it will through exception as “ValueError: Too many values to unpack (expected 2)“. Its’ because loop is try to access two values key and value from dictionary while there is only value because dictionary consider each as value.

user = {
	"name": "Saurabh Gupta",
	"age": 35,
	"city": "Noida"
}
#iterate user dictionary
for key, value in user:
	print("Key:", key)
	print("Value:", str(value))

Output

File “C:/Users/saurabh.gupta/Desktop/Python Example/Exception Test.py”, line 13, in
for key, value in user:

ValueError: too many values to unpack (expected 2)

Solution

To resolve above problem and access key and value from dictionary each item , you can use dictionary items() method which analyze keys and values from dictionary and return these values in iteration. You can modify the code as below:

user = {
	"name": "Saurabh Gupta",
	"age": 35,
	"city": "Noida"
}
#iterate user dictionary
for key, value in user.items():
	print("Key:", key)
	print("Value:", str(value))

Output

After adding items() method in code you can observe below output which is returning key and value of dictionary.

Key: name
Value: Saurabh Gupta
Key: age
Value: 35
Key: city
Value: Noida

Note: Till Python 2.0 use method iteritems() in Python 3+ it’s replace with method items().

Example 2

In Python, allows sequence of values to assign on number of variables. In case number of values are more then variables, it throws exception as “ValueError: too many values to unpack (expected X)

grapes, bananas, apples, papaya = [250, 80, 200, 300, 500]

Output

It will throws exception as below because count of variables are less then the values on list.

File “C:/Users/saurabh.gupta/Desktop/Python Example/Exception Test.py”, line 11, in
grapes, bananas, apples, papaya = [250, 80, 200, 300, 500]

ValueError: too many values to unpack (expected 4)

In case number of values are less then variables, it throws exception as “ValueError: not enough values to unpack (expected X)”

grapes, bananas, apples, papaya = [250, 80, 200]

Output :

File “C:/Users/saurabh.gupta/Desktop/Python Example/Exception Test.py”, line 18, in
grapes, bananas, apples, papaya = [250, 80, 200]

ValueError: not enough values to unpack (expected 4, got 3)

This is because of number of values are less (3) while number of values

To resolve such type of issues, always make sure the number of variables should be equal to number of values in list.

[Solved]: Python TypeError: cannot unpack non-iterable NoneType object


In Python, TypeError is subclass of Exception. Python sequence can be unpacked. This means you can assign content of sequence to multiple variables. If you try to assign a None value to a variable by using this syntax then it throws error as “TypeError: Can not unpack Non-iterable None Type object”.

Note: Syntax error should not be handle through exception handling it should be fixed in your code.

You can check complete list of built-in exception hierarchy by following link. Python: Built-in Exceptions Hierarchy

How to unpack sequence elements to variables?

In this below unpacking sequence the elements of list will assign to variables in sequence. For example:

fruit_prices = [250, 80, 200]
grapes, bananas, apples = fruit_price

In the above code the values in fruit_prices will assign in variables as below :

grapes=250, bananas=80, apples=200

Lets take another example of unpacking sequence from function where return values from functions can be assigned in sequence of variables. For Example:

prices = [4.30, 5.90, 6.70, 3.90, 5.60, 8.30, 6.50]
def calculate_statistics(prices):
    average_price = sum(prices) / len(prices)
    largest_price = max(prices)
    return average_price, largest_price
average, largest = calculate_statistics(prices)
print("Average :"+str(average))
print("Largest :"+str(largest))

In this above example, If you will see it’s returning two values (line 5) from calculate_statistics function and returned values will assign to variables average and largest in sequence (line 6).

Average : 5.88

Largest : 8.30

Scenario for Exception

Now lets create scenario for creating exception, I have modified the above code with comments the line # 5. It will display the code as below

prices = [4.30, 5.90, 6.70, 3.90, 5.60, 8.30, 6.50]
def calculate_statistics(prices):
    average_price = sum(prices) / len(prices)
    largest_price = max(prices)
    #return average_price, largest_price
average, largest = calculate_statistics(prices)
print("Average :"+str(average))
print("Largest :"+str(largest))

When you execute the above code will through exception as below

average, largest = calculate_statistics(prices)

TypeError: ‘NoneType’ object is not iterable

In this example, Hope you understand the scenario as the number of values in sequence will assigned to value on same number of value.

Learn Python exception handling in more detain in topic Python: Exception Handling

[Solved] : Python TabError: inconsistent use of tabs and spaces in indentation


In Python, TabError is sub class of IndentationError. Python allows code style by using indentation by space or tabs. If you are using both while writing code for indentation then Python encounter “TabError : inconsistent use of tabs and spaces in indentation”.

In Python, Indentation is important because the language doesn’t depend on syntax like curly brackets to denote where a block of code starts and finishes . Indents tell Python what lines of code are part of what code blocks.

Note: Syntax error should not be handle through exception handling it should be fixed in your code.

You can see complete Python exception hierarchy through this link : Python: Built-in Exceptions Hierarchy.

Example

Consider a below scenario where indentation is use by implementing space and tab both on line 3 (used space for indentation) while in line 4 (used tabs for indentation). When you will run the below program it will throw exception as mentioned in output.

numbers = [3.50, 4.90, 6.60, 3.40]
def calculate_total(purchases):
	total = sum(numbers)
        return total
total_numbers = calculate_total(numbers)
print(total_numbers)

Output

File “C:/Users/saurabh.gupta/Desktop/Python Example/Exception Test.py”, line 10
return total
^
TabError: inconsistent use of tabs and spaces in indentation

Solution

To resolve this issue, you have done some minor change in your code for indentation by either space or tabs and run the program will work fine.

numbers = [3.50, 4.90, 6.60, 3.40]
def calculate_total(purchases):
    total = sum(numbers)
    return total
total_numbers = calculate_total(numbers)
print(total_numbers)

Output

18.4

Learn Python exception handling in more detain in topic Python: Exception Handling

Let me know your thought on it.

Happy Learning !!!

[Solved] : Python IndentationError: unexpected indent in Python


Base class of IndentationError is SyntaxError. This exception occurred in Python because of incorrect Indentation because Python don’t use curly brackets for segregate blocks for loop, if-else, functions etc. it’s identify the blocks based on indentation only. Sometime if with in same block there is difference in indentations then it can throw TabError.

Note: Syntax error should not be handle through exception handling it should be fixed in your code.

You can check complete list of built-in exception hierarchy by following link. Python: Built-in Exceptions Hierarchy

Example

Here is simple example of reading the csv file by Python csv module. It’s throwing indentation error because of not proper indentation in second statement.

import csv
   with open(r'C:\Users\saurabh.gupta14\Desktop\Python Example\input.csv','r') as csvfile:
    reader=csv.reader(csvfile)
    for record in reader:
        print(record)

Output

File “C:/Users/saurabh.gupta14/Desktop/Python Example/ReadingCSV.py”, line 2
with open(‘C:\Users\saurabh.gupta14\Desktop\Python Example’,’r’) as csvfile:
^
IndentationError: unexpected indent

Solution

In the above example the second line is start from after taking tab which is not required. It should start without taking any space or tab. To fixed this issue i have remove the space and run it again.

import csv
with open(r'C:\Users\saurabh.gupta14\Desktop\Python Example\input.csv','r') as csvfile:
    reader=csv.reader(csvfile)
    for record in reader:
        print(record) 

The above modified code with not throw the IndentationError.

Learn Python exception handling in more detain in topic Python: Exception Handling

Let me know your thought on it.

Happy Learning !!!

[Solved] Python ZeroDivisionError: division by zero in Python


The super class of ZeroDivisionError is ArithmeticError. This exception raised when the second argument of a division or modulo operation is zero. The associated value is a string indicating the type of the operands and the operation.

In simple term in any arithmetic operation when value divided by zero then in Python throw ZeroDivisionError.

You can see complete Python exception hierarchy through this link : Python: Built-in Exceptions Hierarchy.

Example :

In the Python program will throw ZeroDivisionError in case of num_list is not having any element then it’s length become 0 and while executing this program will through ZeroDivisionError.

num_list=[]
total=0
avg=total/len(num_list)
print("Average:"+avg)

Output

ZeroDivisionError : Division by Zero

Solution

While implementing any program logic and there is division operation make sure always handle ArithmeticError or ZeroDivisionError so that program will not terminate. To solve above problem follow this example:

num_list=[]
total=0
try:
    avg=total/len(num_list)
    print("Average:"+avg)
except ZeroDivisionError:
    print ("Zero Division Error occurred")

Output

Zero Division Error occurred.

In this above modified code applied exception handling for particular code section so that program will not terminate.

Learn Python exception handling in more detain in topic Python: Exception Handling

Let me know your thought on it.

Happy Learning !!!

Python: Exception Handling


What is an Exception?

In Python, Sometimes the programs may misbehave or terminate/crash unexpectedly due to some unexpected events during the execution of a program. These unexpected events are called as exceptions and the process of handling them to avoid misbehavior or crashing the program is called as exception handling.

Let’s execute the below code in python and have a look at the output.

	def calculate_expenditure(list_of_expenditure):
	    total=0
	    for expenditure in list_of_expenditure:
	        total+=expenditure
	    print(total)
	list_of_values=[100,200,300,"600",500]
	calculate_expenditure(list_of_values)

Above code will give an error, one way to take care of such error situation is to use selection constructs. The error was due to addition of a string (“600”) to an integer. If we add a condition to check whether the expenditure is of type int, that would solve this error.

But that can cause further issues. Let’s see that by executing the below code in python playground.

	def calculate_expenditure(list_of_expenditure):
	    total=0
	    for expenditure in list_of_expenditure:
	        if(type(expenditure) is int):
	            total+=expenditure
	        else:
	            print("Wrong data type")
	            break
	    print(total)
	
	list_of_values=[100,200,300,"600",500]
	calculate_expenditure(list_of_values)

Although we have handled this error using if statement, the function itself returns wrong output when there is error in the input. 
The ideal situation would be if the function can tell us that something went wrong.

try and except

In python we can create a try and except block of code to handle exceptions.
If any exception occurs in the try block of code, it will jump to except block of code. Once the except block is executed, the code continues to execute other statements outside except block.

	def calculate_expenditure(list_of_expenditure):
	    total=0
	    try:
	        for expenditure in list_of_expenditure:
	            total+=expenditure
	        print(total)
	    except:
	        print("Some error occured")
	    print("Returning back from function.")
	
	list_of_values=[100,200,300,"600",500]
	calculate_expenditure(list_of_values)

With this we will not get incorrect output like before.

Built-in Exceptions

Python has many kinds of exceptions predefined as part of the language. BaseException is the base class of all type of exception. Below are some most common types built-in exception.

You can check complete list of built-in exception hierarchy by following link. Python: Built-in Exceptions Hierarchy

Built-in exceptionWhen it will raiseExample
ZeroDivisionErrorWhen a value is divided by zero.num_list=[]
total=0
avg=total/len(num_list)
TypeErrorWhen we try to do an operation with incompatible type.total=10
total+=”20″
NameErrorWhen try to access a variable which is not defined.avg=total/10 #where total is not defined
IndexErrorWhen try to access a index value which is out of range.num_list=[1,2,3,4]
value=num_list[4]
ValueErrorWhen we use a valid data type for an argument of a built-in function but passes an invalid value for itstring is a valid data type for int() but the value “A” is invalid, as “A” can’t be converted into int.
value=”A”
num=int(value)

Python also allows us to handle different exceptions that can occur separately. That means you can have a different action or message for every unique exception that occurs.

Exception Handling Example

Here is the same expenditure calculation code with additional average expenditure calculation.

	def calculate_expenditure(list_of_expenditure):
	    total=0
	    try:
	        for expenditure in list_of_expenditure:
	            total+=expenditure
	        print("Total:",total)
	        avg=total/num_values
	        print("Average:",avg)	    
        except ZeroDivisionError:
	        print("Divide by Zero error")
	    except TypeError:
	        print("Wrong data type")
	    except:
	        print("Some error occured")
	list_of_values=[100,200,300,"400",500]
	num_values=0
	calculate_expenditure(list_of_values)

Note:

  1. Default except block is the one without any type mentioned.
  2. If an error occurs and the matching except block is found, then that is executed.
  3. If an error occurs and the matching except block is not found, it executes the default except block.
  4. If an error occurs and the matching except block is not found and if the default except block is also not found, the code crashes.
  5. The default except block, if present should be the last except block, otherwise it will result in a runtime error.

Exception handling inside a function

If an exception occurs inside a function and if the exception is not caught inside it, then the exception is transferred to the function call. We have another opportunity to catch it, if we write function call inside another try and except block.

Try the below code in python playground and observe the output.

	def calculate_sum(list_of_expenditure):
	    total=0
	    try:
	        for expenditure in list_of_expenditure:
	            total+=expenditure
	        print("Total:",total)
	        avg=total/no_values
	        print("Average:",avg)
	    except ZeroDivisionError:
	        print("Divide by Zero error")
	    except TypeError:
	        print("Wrong data type")
	
	try:
	    list_of_values=[100,200,300,400,500]
	    num_values=len(list_of_values)
	    calculate_sum(list_of_values)
	except NameError:
	    print("Name error occured")
	except:
	    print("Some error occured")

finally

Sometimes in programming we need to execute some code irrespective of whether the primary program logic itself succeeds or fails to do its job. In Python we can achieve this using a finally block. A finally block of statement is an optional part of the try-except statements. A code written inside the finally block will ALWAYS be executed.

finally block is majorly used to close the database connections in the programs which involves database connectivity.

Try the below code in python playground and observe the output.

	balance=1000
	amount="300Rs"
	
	def take_card():
	    print("Take the card out of ATM")
	try:
	    if balance>=int(amount):
	        print("Withdraw")
	    else:
	        print("Invalid amount")
	except TypeError:
	    print("Type Error Occurred")
	except ValueError:
	    print("Value Error Occurred")
	except:
	    print("Some error Occurred")
	finally:
	    take_card()

In this topic you learn about the exception, built-in exceptions and handling of exceptions in different cases.

Python: Built-in Exceptions Hierarchy


In Python version 3.9.2 is having the below class hierarchy for built-in exceptions. You will get to know more about each exception and solutions on click of it.

In Python, to learn about the exception handling follow the topic Exception Handling in Python.

Python: Variable Scope


This code has been written in Python to represent the baggage weight check process based on the weight limit specified by an airline.

You can go through the below code and guess the output.

	wt_limit=30
	
	def baggage_check(baggage_wt):
	    extra_baggage_charge=0
	    if not(baggage_wt>=0 and baggage_wt<=wt_limit):
	        extra_baggage=baggage_wt-wt_limit
	        extra_baggage_charge=extra_baggage*100
	    return extra_baggage_charge
	
	def update_baggage_limit(new_wt_limit):
	    wt_limit=new_wt_limit
	    print("This airline now allows baggage limit till",wt_limit,"kgs")
	
	print("This airline allows baggage limit till",wt_limit,"kgs")
	print("Pay the extra baggage charge of",baggage_check(35),"rupees")
	update_baggage_limit(45)
	print("Pay the extra baggage charge of",baggage_check(35),"rupees")	 

Let us go through the code now in more detail to see the scope of variables.

Python scope of variables

extra_baggage and extra_baggage_charge are created inside the function baggage_check(). Hence they are local to that function or in other words, they are local variables. They are created when owning function starts execution and remains in memory till owning function finishes execution. They can be accessed only inside that function.

wt_limit is created outside the functions. Hence it is a global variable. Global variables are created when the program execution starts and remains in memory till the program terminates. They can be read anywhere in the program – within a function or outside. But they are protected from modification inside a function. As it is available throughout the program, use of global variable should be restricted to avoid accidental misuse by developers and to minimize memory usage.

In cases where a global variable needs to be modified inside a function, like in function update_baggage_limit(), Python allows you to do that using the global keyword.

Python Variable Scope

Python: Typs of Argument


Programming languages allow controlling the ordering and default values of arguments. In python we will observe the following in different cases:

Case 1: Positional

This is default way of specifying arguments. In this, the order, count and type of actual arguments should exactly match that of the formal arguments. Else it will result error.

Python: Positional Argument

Case 2: Keyword

This allow flexibility in the order of passing the actual arguments by mentioning the argument name.

Python : Keyword in argument

Case 3: Default

This allows the specify default value for an argument in the function signature. It is used only when no value is passed for the arguments else it works normally. In Python, default arguments should be last in the order.

Python: Default Argument

Case 4: Variable argument count

This allows a function to have variable number of arguments. In Python, any argument name starting with ‘*’ is considered to be a vary length argument. It should be last in order, It works by copying all values beyond that position in a tuple.

Python: Variable argument count

Python: Argument Passing


In programming, there are two ways in which arguments can be passed to functions: 

  • Pass by value
  • Pass by reference

Some languages use pass by value by default while others use pass by reference. Some languages support both and allow you to choose.

In Python, we don’t have to think about pass by value and pass by reference as it does that automatically for you. To emulate this using Python, we use the concept of mutability. If the argument passed is immutable then it follows pass by value, else if the argument passed is mutable then it follows pass by reference.

Note: Till now we have seen int, float, string data types which immutable and mutable data types we will discuss in later part of the topics.

Pass by reference method

Pass by reference case apply on mutable type values, In this case value pass as reference to a method in case any change done with in function will reflect out side the function also. From below example you will see formal and actual argument point to same data location because of reference value.

Python Pass by reference

Pass by value method

Pass by value case apply on immutable variables/arguments in case value change with in the function then changes done on function will not reflect out side of the method. From the below example you will see the storage of formal and actual argument are separate.

Python pass by value method

Functions in Python


Functions are set of instructions to perform a specific task. A function is a block of code which only runs when it is called. You can pass data, known as parameters/argument, into a function. It also called as method.

Syntax

Below is the syntax of functions in python. In the below example you will get more clarity about the function syntax.

Define Function

def function_name([arg1,…,argn]): 
    #statements 
    [return value] 

Call Function
variable_name = function_name([val1,…,valn])

Note: Anything enclosed in [ ] (square bracket) is optional

Function can we call from any program, class or from another functions also.

Example

This is simple example of function, where calculate_sum function is performing the task of sum of two numbers/ passing arguments data1 and data2 and returning final result in value result_sum.

Python Functions Example

Output

30

Returning from a function

Let’s see how we can use values returned from a function.

Python return from a function

In the above screen display the different cases of return value from a function. It’s depend on your task in case your task required to return value then return one result value from function then assign this value to some variable where you call this function.

In case, you need return multiple value you can use reference type variable like array or you can also change value in passing argument if these are reference type and can be access out side the function. You will get more detail of reference variable in next topic Argument passing in Python.