Sometime we have to test whether a method throws proper and required exceptions or not. One way of testing is placing the method call (to be tested) inside a try catch block, as shown below try { class2.method1(); fail(“Should have thrown exception”); } catch(IllegalArgumentException excep) { } catch(Exception excep) { fail(“Should have thrown IllegalArgumentException exception”);…… Continue reading Unit testing method exceptions
Setting timeout for test methods
Junit offers us the option to set timeout for test methods. If a test methods doesnt complete within specified time limit, an exception is thrown failing the test case. The use case scenario where this feature can be used is for example testing the performance of a method. The below code shows you how to…… Continue reading Setting timeout for test methods
Making JUnit ignore test methods
If we want JUnit to ignore certain test methods we can do that by using the @Ignore annotation applied on the methods that has to be ignored. The below test class tests a functionality of a calculator CalculatorTest package Package1; import static org.junit.Assert.assertEquals; import org.junit.Ignore; import org.junit.Test; @Ignore public class CalculatorTest { @Test public void…… Continue reading Making JUnit ignore test methods
Getting mapped class meta data
This post explains how to get hibernate related meta data information of a mapped class. Class package model; public class Series { private Integer id; private String name; private String description; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public…… Continue reading Getting mapped class meta data
Creating dynamic proxy objects
This post explains how to create proxy objects using Java Proxy api. We need the below items to create a proxy. 1) proxy interfaces (list of interfaces the dynamic proxy class will implement). 2) dynamic proxy class is proxy class generated at runtime and implements the proxy interfaces. 3) proxy instance is an instance of…… Continue reading Creating dynamic proxy objects
Listening to unmarshalling events
By subclassing Unmarshaller.Listener abstract class, we can create listener class which will listen to unmarshling events. We can add our code if we want to perform some actions before and after unmarshalling. The Unmarshaller.Listener class has two methods which can be overridden. By default the methods doesn’t perform any actions. 1) public void beforeUnmarshal(Object target,…… Continue reading Listening to unmarshalling events
Listening to marshaling events
By subclassing Marshaller.Listener abstract class, we can create listener class which will listen to marshling events. We can add our code if we want to perform some actions before and after marshalling. The Marshaller.Listener class has two methods which can be overridden. By default the methods doesn’t perform any actions. 1) public void beforeMarshal(Object source)…… Continue reading Listening to marshaling events
Unzipping a zip file and checking for data integrity
This post explains how to unzip a zip file and check whether data were correctly extracted. package ZipDemo; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Enumeration; import java.util.zip.CRC32; import java.util.zip.CheckedInputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; public class ZipDemo6 { public static void main(String[] args) throws FileNotFoundException, IOException { String destination = “E:\\Projects\\JavaProjects\\JavaConcepts\\WorkFolder\\Folder1”;…… Continue reading Unzipping a zip file and checking for data integrity
Unzipping a zip file
This post explains how to unzip a zip file. package ZipDemo; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; public class ZipDemo3 { public static void main(String[] args) throws FileNotFoundException, IOException { String destination = “E:\\Projects\\JavaProjects\\JavaConcepts\\WorkFolder\\Folder1”; try (ZipFile zipFile = new ZipFile(“E:\\Projects\\JavaProjects\\JavaConcepts\\WorkFolder\\Folder1.zip”)){ File folder = new File(destination);…… Continue reading Unzipping a zip file
Creating a zip file of a folder
This post explains how to create a zip file of a folder (also containing sub folders) The below image shows the hierarchy of the folder package ZipDemo; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class ZipDemo2 { private File startingFolder; public ZipDemo2(File startingFolder)…… Continue reading Creating a zip file of a folder