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.
LIQUIBASE is a provider of Spring Boot Data for handling database schemas operations. LIQUIBASE is an Open Source tool which keep track of database schema script revisions. It can handle variety of database types and accepts a variety of file formats for defining the database structures.
After adding LIQUIBASE dependencies in your application it will automatically download and add the required other dependencies in your application and initialize with default values. You can overwrite these values through application.properties / application.yaml .
Liquibase Configuration Properties
Spring Boot load these properties in LiquibaseProperties class.
ApacheCassandra is a provider of Spring Boot Data for handling NoSQL database operations. Apache Cassandra is a NoSQL distributed database for managing large amounts of data across many servers (clusters) while providing high availability at the cost of decreased consistency. Cassandra high availability is achieved by replicating data to multiple nodes over cluster and allow one or more nodes to go down so that transaction will continue till that point even one server is running.
To use Apache Cassandra in your Spring boot application you have to add this Cassandra starter in your pom.xml .
After adding Cassandra Database starter in your application it will automatically download and add the required dependencies in your application and initialize with default values. You can overwrite these values through application.properties / application.yaml .
Cassandra Configuration Properties
Spring Boot load these properties in CassandraProperties class.
Name
Default Value
Description
spring.data.cassandra.cluster-name
Cassandra cluster Name.
spring.data.cassandra.compression
Compression supported by the Cassandra binary protocol.
Couchbase is a provider of Spring Boot for handling NoSQL database operations through JSON. Couchbase is NoSQL document oriented distributed database over the cloud or on- premises which perform transactions by JSON and provide unmatched versatility, performance and scalability. To use Couchbase in your Spring boot application you have to add this Couchbase starter in your pom.xml .
After adding Couchbase Database starter in your application it will automatically download and add the required dependencies in your application and initialize with default values. You can overwrite these values through application.properties / application.yaml .
Data Couchbase Configuration Properties
Spring Boot load these properties in CouchbaseDataProperties class.
Name
Default Value
Description
spring.data.couchbase.auto-index
false
create views and indexes automatically.
spring.data.couchbase.consistency
read-your-own-writes
By default Consistency to apply on generated queries.
spring.data.couchbase.repositories.enabled
true
Enable Couchbase repositories.
Data Couch Spring Boot properties
Couchbase Configuration Properties
Spring Boot load these properties in CouchbaseProperties class.
Name
Default Value
Description
spring.couchbase.bootstrap-hosts
Couchbase nodes host/IP address to bootstrap from.
spring.couchbase.bucket.name
default
bucket name connect to.
spring.couchbase.bucket.password
bucket password.
spring.couchbase.env.endpoints.key-value
1
Number of sockets per node for each Key/value service.
spring.couchbase.env.endpoints.query
1
Number of sockets per node for each Query (N1QL) service.
spring.couchbase.env.endpoints.view
1
Number of sockets per node for each view service.
spring.couchbase.env.ssl.enabled
Enable SSL support. Enabled automatically if a “keyStore” is provided otherwise specified otherwise.
spring.couchbase.env.ssl.key-store
Path to JVM key store which holds the certificates.
spring.couchbase.env.ssl.key-store-password
Password used to access the key store.
spring.couchbase.env.timeouts.connect
5000
Bucket connections timeout. (in milliseconds)
spring.couchbase.env.timeouts.key-value
2500
Blocking operations performed on a key timeout.( in milliseconds)
spring.couchbase.env.timeouts.query
7500
N1QL query operations timeout.( in milliseconds)
spring.couchbase.env.timeouts.socket-connect
1000
Socket connect connections timeout.( in milliseconds).
spring.couchbase.env.timeouts.view
7500
Regular and geospatial view operations timeout. (in milliseconds).
Apache SOLR is a provider of Spring Boot for handling search and indexing of data operations on SOLR content for web. SOLR is a Full Text search engine for content management and also provide REST based SOLR APIs for operations. To use SOLR in your Spring boot application you have to add this SOLR starter in your pom.xml .
After adding SOLR starter in your application it will automatically download and add the required dependencies in your application and initialize with default values. You can overwrite these values through application.properties / application.yaml .
SOLR Configuration Properties
Spring Boot load these properties in SolrProperties class.
Elasticsearch is a provider of Spring Boot for handling search and CRUD operations in Elastic Search. Elasticsearch is a Full Text search engine and also provide REST based Elasticsearch APIs for operations. To use Elastic Search in your Spring boot application you have to add this Elasticsearch starter in your pom.xml .
After adding Elasticsearch starter in your application it will automatically download and add the required dependencies in your application and initialize with default values. You can overwrite these values through application.properties / application.yaml .
ElasticSearch Configuration Properties
Spring Boot load these properties in ElasticsearchProperties class.
Name
Default Value
Description
spring.data.elasticsearch.cluster-name
elasticsearch
cluster name.
spring.data.elasticsearch.cluster-nodes
Comma-separated cluster node addresses. If not specified, starts a client node.
spring.data.elasticsearch.properties.*
Additional properties used to configure the client.
spring.data.elasticsearch.repositories.enabled
true
Enable Elasticsearch repositories.
Elastic Search Spring Boot Properties
JEST (Elasticsearch HTTP client) Configuration Properties
Spring Boot load these properties in JestProperties class.
MongoDB is a provider of Spring Boot for handling NoSQL database operations. To use MongoDB in your Spring boot application you have to add this MongoDB starter in your pom.xml .
After adding MongoDB starter in your application it will automatically download and add the required dependencies in your application and initialize with default values. You can overwrite these values through application.properties / application.yaml .
Embedded MongoDB Configuration Properties
Spring Boot load these properties in EmbeddedMongoProperties class.
Name
Default Value
Description
spring.mongodb.embedded.features
SYNC_DELAY
Comma-separated features to enable.
spring.mongodb.embedded.storage.database-dir
Directory used for data storage.
spring.mongodb.embedded.storage.oplog-size
Maximum size of the oplog in megabytes.
spring.mongodb.embedded.storage.repl-set-name
Name of the replica set.
spring.mongodb.embedded.version
2.6.10
Version of Mongo to use.
MongoDB Embedded Spring Boot Properties
MongoDB Configuration Properties
Spring Boot load these properties in MongoProperties class.
Name
Default Value
Description
spring.data.mongodb.authentication-database
Authentication database name.
spring.data.mongodb.database
test
Database name.
spring.data.mongodb.field-naming-strategy
USe Fully qualified name of the FieldNamingStrategy.
spring.data.mongodb.grid-fs-database
GridFS database name.
spring.data.mongodb.host
localhost
Mongo server host.
spring.data.mongodb.password
Login password of the mongo server.
spring.data.mongodb.port
27017
Mongo server port.
spring.data.mongodb.repositories.enabled
true
Enable Mongo repositories.
spring.data.mongodb.uri
mongodb://localhost/test
Mongo database URI.host and port are ignored when setting it.
Neo4J is a provider of Spring Boot for handling Graphical database. Neo4J is a graphical datbase where need to represent stored data in graphical relational forms. You can add Neo4J in your Spring boot application by adding Neo4J starter in your pom.xml (Maven) or build.gradle (Gradle).
After adding Neo4J starter in your application it will automatically download and add the required dependencies in your application and initialize with default values. You can overwrite these values through application.properties / application.yaml .
Neo4j Configuration Properties
Spring Boot load these properties in Neo4jProperties class.
Name
Default Value
Description
spring.data.neo4j.compiler
Compiler to use.
spring.data.neo4j.embedded.enabled
true
Enable embedded mode when embedded driver is available.
ATOMIKOS is a provider of Spring Boot for handling transactions atomicity in global transactions involving heterogeneous components. To use ATOMIKOS transaction manager in your Spring boot application you have to add this ATOMIKOS starter in your pom.xml .
After adding ATOMIKOS starter in your application it will automatically download and add the required dependencies in your application and initialize with default values. You can overwrite these values through application.properties / application.yaml .
Atomikos Configuration Properties
Spring Boot load these properties in AtomikosProperties class.
BITRONIX is a provider of Spring Boot for handling distributed transaction. To use BITRONIX transaction manager in your Spring boot application you have to add this BITRONIX starter in your pom.xml .
After adding BITRONIX starter in your application it will automatically download and add the required dependencies in your application and initialize with default values. You can overwrite these values through application.properties / application.yaml .
Narayana is a popular open source JTA transaction manager implementation supported by Red Hat. To use Narayana JTA in your Spring Boot application you can use You can use the narayana-spring-boot-starter starter to add the appropriate Narayana dependencies to your application.
Spring Boot automatically configures Narayana with default values and post-processes your beans to ensure that startup and shutdown ordering is correct.
NARAYANA Spring Boot Properties
Spring Boot load these properties in NarayanaProperties class with default values you can overwrite these values in your application application.properties/ application.yaml.
Interval in which periodic recovery scans are performed.(in second)
spring.jta.narayana.recovery-backoff-period
10
Back off period between first and second phases of the recovery scan.(in second)
spring.jta.narayana.recovery-db-pass
Database password for recovery manager.
spring.jta.narayana.recovery-db-user
Database username for recovery manager.
spring.jta.narayana.recovery-jms-pass
JMS password for recovery manager.
spring.jta.narayana.recovery-jms-user
JMS username for recovery manager.
spring.jta.narayana.recovery-modules
Comma-separated recovery modules.
spring.jta.narayana.transaction-manager-id
1
Unique transaction manager id.
spring.jta.narayana.xa-resource-orphan-filters
Comma-separated orphan filters.
By default, Narayana transaction logs written location is transaction-logs directory in your application home directory (the directory in which your application jar file resides). You can customize the location of this directory by setting the narayana.log-dir or spring.jta.log-dir similar to that you can also customize the other properties.
HR & Managerial round interview questions can cover a diverse range of discussion topics apart from Technical. These questions asked generally asked by laterals or experienced persons to check his/her personality/behaviour. In some organizations, this is also called a behaviour round of interviews. Generally, the interviewer observes these areas from your answers:
Observe your expressions
Check your leadership skills/soft skills
Team handling skills
Fit for organization cuture or not
Client handling skills
Handling on critical, stressed and pressurise work environment.
How to introduce yourself
The interviewer can create different scenarios for organization culture, team conflicts situation and some cases ask for combination with technology-related questions. Based on answered will ask for further questions. In such type questions, the interviewer wants to understand how you cope with change, approach learning, and overcome challenges and obstacles.
In this article, we share some behaviour round interview questions with sample answers and also provide an overview of some additional example questions to prepare with a scenario that will fit your profile.
Do you have any questions?
How to answer to such behaviour and situational quetions?
To prepare for these questions, take a moment to think of a number of challenging situations you have faced, such as difficulty with new technology, project, timeline, customer, ambiguity, process, or even a team member. Think of situations where you were challenged with what was the right thing to do. What did you do and how did you handle it? and how you can fit your previous experiences with current organization requirements.
Always keep in mind the STAR rule to answer such behavioural questions.
S – Situation. What was the situation? Describe in brief. (This is the challenge you were facing.)
T – Task. What was the task you were assigned? What was your responsibility?
A – Action. What action did you take?
R – Result. What happened because of your action?
You can use examples for reference and resolution you provided on a particular case.
What are your strengths?
Some of most frequently asked questions in behavioural interview
Common Interview Questions
These are common interview questions that can be asked in technical as well as in behavioural/manager/HR rounds of interviews. Through these questions, the interviewer wants to check your communication, understanding of your educational and professional background and your core technical skills instead of going through the complete CV.
What is your technical streagths and rate yourself in out of 10 points? Example
Tell us about your educational and technical background. Example
Behavioural Questions
These questions are to check your personality, how you are socially connected, motivated and performed as an individual as part of the team so that interviewer can understand your compatibility with your current position requirement.
In Spring Boot application, you can set the JVM parameters by two ways so that increase the JVM heap size, in case required you can also configure the other parameters like GC alogorithms, MetaSpace etc.
Configure Parameters in pom.xml: To pass the JVM parameters for an application , we can set these parameters in spring-boot-maven-plugin in pom.xml plugin section.
The java.lang.OutOfMemoryError: Metaspace indicates that allocated native memory for Java class metadata is exausted. That’s why issue occured in standalone and cloud based applications.
In Java 8 and later versions, the maximum amount of memory allocated for Java classes (MaxMetaspaceSize) is by default unlimited, so in most cases there is no need to change this setting. On the other hand, if you want to fix the amount of memory allocated for Java classes, you can set it as follows:
java -XX:MaxMetaspaceSize=1024m
This JVM parameter -XX:MaxMetaspaceSize is just an set the upper limit of MetaSpace. The current Metaspace size (i.e. committed) will be smaller. In fact, there is a setting called MaxMetaspaceFreeRatio (default 70%) which means that the actual metaspace size will never exceed 230% of its occupancy.
And for it to grow it first would have to fill up, forcing a garbage collection in an attempt to free objects and only when it cannot meet its MinMetaspaceFreeRatio (default 40%) goal it would expand the current metaspace. That can however not be greater than 230% of the occupancy after the GC cycle.
Monitoring MetaSpace Size with Java Native Memory tracking
A good way to monitor the exact amount of Metadata is by using the NativeMemoryTracking, which can be added through the following settings:
When Native Memory Tracking is enabled, you can request a report on the JVM memory usage using the following command:
$ jcmd <pid> VM.native_memory
OutOfMemoryError: Metaspace on OpenShift/Kubernetes
When using openjdk Image on OpenShift/Kubernetes, the default maxium value for the Metaspace is XX:MaxMetaspaceSize=100m. You might have noticed that setting this value through the JAVA_OPTIONS environment variable, doesn’t work as the default value is appended to the bottom:
VM Arguments: -Xms128m -Xmx1024m -XX:MetaspaceSize=128M -XX:MaxMetaspaceSize=256m -XX:AdaptiveSizePolicyWeight=90 -XX:MaxMetaspaceSize=100m -XX:+ExitOnOutOfMemoryError
Once the MetaSpace get full in your application it will stop the service and through exception as below in your logs.
oc logs XYZ-service-7b856cc89-kpc6k | grep -i metaspace
INFO exec java -javaagent:/usr/share/java/jolokia-jvm-agent/jolokia-jvm.jar=config=/opt/jboss/container/jolokia/etc/jolokia.properties -XX:+UseParallelOldGC -XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=20 -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -XX:MaxMetaspaceSize=100m -XX:+ExitOnOutOfMemoryError -cp "." -jar /deployments/XYZ-service-0.0.1-SNAPSHOT.jar
Picked up JAVA_TOOL_OPTIONS: -Dappdynamics.agent.accountAccessKey=600a90af-582a-4ae2-87b1-4599708b65dd -Dappdynamics.agent.reuse.nodeName=true -Dappdynamics.socket.collection.bci.enable=true -XX:MaxMetaspaceSize=1024m -javaagent:/opt/appdynamics-java/javaagent.jar
Terminating due to java.lang.OutOfMemoryError: Metaspace
Solutions
The correct way to set the MaxMetaspaceSize is through the GC_MAX_METASPACE_SIZE environment variable. Here are the different cases ti implement this solutions:
Jenkins Pipeline: For example, if you are using a jenkins pipeline to deploy your application or services then you can meke following changes in the json template to refelect these changes in deployment.yaml file to deploy your application with JKube, the following settings will override the default values for the MaxMetaspaceSize and MaxMetaspaceSize:
Note : After deploying your service over OpenShift/Kernates validate the deployment configuration file(deployment.yml) for these parameters. In case not not reflecting then delete your pods completly and reploy the application.
In case, you want make changes on other parameters also for improving the puformance of your applictaion then you can follow these documents to list of parameters for OpenShift.
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
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.
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.
The value of id parameter of @Endpoint is users. So 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:
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:
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 Endpoints
Description
/beans
Provides list of all Spring beans available in the application
/configprops
Provides a collated list of all @ConfigurationProperties
/env
Exposes all the properties from Spring’s Configurable Environment
/info
Displays arbitrary application information
/metrics
Displays metric information for the current application
/mappings
Displays a collated list of all request mapping paths
/shutdown
Allows the application to shutdown
/trace
Displays trace information, by default latest 100 HTTP requests
/health
Provides 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:
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:
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 :
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.
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:
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.
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.
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.
##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.
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.
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.
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.
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 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:
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.
Sometimes in distributed systems are connected through SFTP/MFTP so you can keep generated XML file there and another system will pick it.
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.
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.
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.
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.
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
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.
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.
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:
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:
public ResultSet executeQuery(String sql): is used to execute SELECT query. It returns the object of ResultSet.
public int executeUpdate(String sql): is used to execute specified query, it may be create, drop, insert, update, delete etc.
public boolean execute(String sql): is used to execute queries that may return multiple results.
public int[] executeBatch(): is used to execute batch of commands.
For example of statement interface follow below links:
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.
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
}
To validate custom exception or in-build exception thrown by method write separate test case by mentioning expected in Junit 4 or assertThrows in Junit 5.
JUnit 4
@Test(expected=ArithmeticException.class)
public void testCalculatorDivide()
{
assert(5, obj.devide(5,0))
}
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.
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.
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.
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 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 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
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 :
Task
Without Framework
With Framework
Preparation
Manually
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.
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.
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
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.
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.
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.
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.
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.
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
Header
Description
INSTRUCTION
BRANCH
All 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.
Consider 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
METHOD
Consider as executed when one of the instruction has been executed.
CLASS
Consider 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";
}
}
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.
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.
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.
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.
If you are using simple maven java application then you can add these lines in you pom.xml properties tag .
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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).
This tuple fruits is having five values and each element is associated with index number as below:
Apple
Banana
Grapes
Papaya
Litchi
0
1
2
3
4
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.
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.
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
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.
You must be logged in to post a comment.