In Mockito we cannot mock static class methods, so we can use PowerMock which extends the Mockito features and provides its own features. One of which is mocking static class methods. Below code will give an example how to mock static class methods Calculator package package1; public class Calculator { public static int add(int value1,…… Continue reading Mocking Static class methods
Month: August 2016
Java Predicate
Java provides a new interface named Predicate which is a functional interface. A functional interface is an interface which will have exactly one abstract method in addition to one or more default methods. In the case of Predicate interface, the abstract method is test method whose signature is as shown below boolean test(T t) In…… Continue reading Java Predicate
Getting access to all printers configured in machine
This post explains how to programmatically get access to all printers configured in a machine. Java provides lookup facitlity through PrintServiceLookup class. This class has static methods through which we can get a reference to all the printers. We use the lookupPrintServices method in the PrintServiceLookup class. The api definition is as shown below public…… Continue reading Getting access to all printers configured in machine
Executing scripts written in a file using java
This post explain how to execute javascript code stored in a file. We create an instance of FileReader representing the physical file and pass it to eval method of an instance of ScriptEngine. Please refer to previous posts to know more about ScriptEngine and ScriptEngineManager The javascript code in the file that has to be…… Continue reading Executing scripts written in a file using java
Executing scripts using java
This post explain how to execute code written in scripting language like php or javascript etc in java. The scripts are executed by calling eval on an instance of ScriptEngine interface. An instance of ScriptEngine is used to represent scripting language specific engines. For example in case of JavaScript, Mozilla Rhino provided out of the…… Continue reading Executing scripts using java
Getting access to default printer
This post explains how to programmatically get access to a printer configured as default printer in a machine. Java provides PrintServiceLookup class, which provides static methods through which we can get a reference to the printer. Each printer installed in a machine is represented by an instance of PrintService interface. The PrintService interface provides methods…… Continue reading Getting access to default printer
Writing json data using Jackson JsonGenerator
The below post explains how to write json data to file using JsonGenerator. JsonGenerator writes the data to a file in streaming way, helping the developer using the api to avoid creating an object representation of the data. We can get an instance of JsonGenerator with the help of an instance of JsonFactory as shown…… Continue reading Writing json data using Jackson JsonGenerator