How-to articles, tricks, and solutions about JAVA

Mocking static methods with Mockito

Mockito is a popular mocking framework for Java. It allows you to create mock objects and set up test behavior for them.

Mockito : how to verify method was called on an object created within a method?

To verify that a method was called on an object created within a method using Mockito, you can use the Mockito.verify() method and pass it the object that you want to verify, as well as the method that you want to verify was called.

Mockito How to mock and assert a thrown exception?

To mock and assert a thrown exception in Mockito, you can use the doThrow() method and the verify() method.

MySQL JDBC Driver 5.1.33 - Time Zone Issue

If you are using MySQL JDBC Driver version 5.1.33 and you are experiencing issues with time zones, there are a few things you can try:

No appenders could be found for logger(log4j)?

This error typically occurs when you are trying to use the log4j library to log messages in your Java application, but the library is not properly configured.

No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?

This error message means that the Java compiler (javac) is not available in the current environment. This can happen if you are running a Java program from a Java Runtime Environment (JRE) rather than a Java Development Kit (JDK).

No Persistence provider for EntityManager named

If you are seeing the error "No Persistence provider for EntityManager named [persistence-unit-name]" in your Java application, it means that the persistence provider (e.g.

Non-static variable cannot be referenced from a static context

In Java, a non-static (also known as an instance) variable or method can only be accessed from an instance of the class. A static (also known as a class) variable or method, on the other hand, can be accessed directly from the class, without the need for

Number of lines in a file in Java

To get the number of lines in a file in Java, you can use the BufferedReader class and read the file line by line until the end of the file is reached.

Only using @JsonIgnore during serialization, but not deserialization

If you want to use the @JsonIgnore annotation to ignore a field during serialization but not during deserialization, you can use the @JsonIgnoreProperties annotation and set its writeOnly property to true.

org.xml.sax.SAXParseException: Content is not allowed in prolog

The error org.xml.sax.SAXParseException: Content is not allowed in prolog usually indicates that there is some content before the XML prolog in the file you are trying to parse.

Page scroll up or down in Selenium WebDriver (Selenium 2) using java

To scroll up or down in a web page using Selenium WebDriver in Java, you can use the JavascriptExecutor interface and the scrollBy method.

Parsing JSON Object in Java

To parse a JSON object in Java, you can use the org.json library.

Parsing JSON string in Java

To parse a JSON string in Java, you can use the JSONObject class from the org.json package.

Popup Message boxes

A pop-up message box is a small window that appears on a computer screen to display a message or prompt the user to take a specific action.

POST request via RestTemplate in JSON

To make a POST request with the RestTemplate in JSON, you can use the postForObject() method and pass it the URL of the request, the request body, the response type, and the HttpEntity object that represents the request headers and body.

Printing HashMap In Java

To print the contents of a HashMap in Java, you can use the entrySet() method to get a set of key-value pairs and then iterate over the set to print the keys and values.

Random shuffling of an array

To shuffle an array randomly in Java, you can use the Collections.shuffle method.

Read/Write String from/to a File in Android

To read and write a string from and to a file in Android, you can use the FileInputStream and FileOutputStream classes along with the InputStreamReader and OutputStreamWriter classes.

Reading a plain text file in Java

This code opens the file "filename.txt" and reads it line by line. Each line is printed to the console until the end of the file is reached.

Reading Properties file in Java

To read a properties file in Java, you can use the Properties class from the java.util package.

Received fatal alert: handshake_failure through SSLHandshakeException

Received fatal alert: handshake_failure is an error message that can occur during an SSL/TLS handshake.

RecyclerView onClick

To handle clicks on items in a RecyclerView, you can set an OnClickListener on the View that represents each item in the RecyclerView.

Redirect to an external URL from controller action in Spring MVC

To redirect to an external URL from a controller action in Spring MVC, you can use the RedirectView class and return it from the controller action.

Reflection generic get field value

To get the value of a generic field using reflection in Java, you can use the get() method of the Field class, which returns the value of the field as an Object.