In this post under Mockito, I will show with example how to configure Mockito to call mock object’s real non-static non-void method instead of stubbed one. For our example, we will use below classes IndianCash package package7; public class IndianCash { public double getCash() { return 10; } } IndianCash package package7; public class IndiaToUSACashConverter…… Continue reading how to call real non-static non-void method using mock object
Junit @TempDir cleanup mode
In this post under JUnit, I will show with example how to use @TempDir different cleanup mode and what they are. Clean up mode basically tells JUnit whether to clean up the temp directory or not, at what event to cleanup etc. Out of the box, the following cleanup modes are available1) ALWAYS –> always…… Continue reading Junit @TempDir cleanup mode
Stubbing a non-static non-void method to return something
In this post under Mockito, I will show with example how to stub a non-static non-void method of a mock object to return a hardcoded value. For our example, we will use the below classes IndianCash package package6; public class IndianCash { public double getCash() { return 10; } } IndiaToUSACashConverter package package6; public class…… Continue reading Stubbing a non-static non-void method to return something
Conditional mapping example
In this post under MapStruct, I will show with example how to achieve conditional mapping. Till now in all my previous example, I used to configure which source property should be mapped to which target property and MapStruct would follow that. What if we want to tell MapStruct, do the mapping only if a particular…… Continue reading Conditional mapping example
Junit @TempDir method level scope
In this post under JUnit, I will show with example how applying “@TempDir” to a non-static field creates a temporary directory having method level scope. When “@TempDir” is applied to a non-static field, for every test method present in the test class, JUnit will creates different temporary directory and attaches its reference to non-static field…… Continue reading Junit @TempDir method level scope
Junit @TempDir class level scope
In this post under JUnit, I will show with example how applying “@TempDir” to a static field creates a temporary directory having class level scope. When “@TempDir” is applied to a static field, JUnit creates temporary directory and attaches its reference to static field and it exists for the entire test class lifecycle. In other…… Continue reading Junit @TempDir class level scope
Mocking Interface
In this post under Mockito, I will show with example how to create mock of an interface. For our example we will use below interface DaoI package package5; public interface DaoI { public int insert(Object object); } Below is the test class where we create a mock of “DaoI” interface Test class package package5; import…… Continue reading Mocking Interface
Mocking Abstract classes
In this post under Mockito, I will show with example how to create mock of an abstract class For our example we will use below abstract class Shape package package4; public abstract class Shape { public abstract int calculateArea(); public void display() { System.out.println("Area is:" + calculateArea()); } } Below is the test class where…… Continue reading Mocking Abstract classes
Junit @TempDir annotation
In this post under Junit, I will show with example the purpose and how to use @TempDir annotation. Lets say you have written class that zip the input file as shown below FileZip package package20; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class FileZip { public void zip(File inputFile) throws Exception…… Continue reading Junit @TempDir annotation
Using @InjectMocks annotation
In this post under Mockito, I will show with example the purpose of “@InjectMocks” annotation. In the previous post, I showed with example, the purpose of “@Mock” annotation. For recap “@Mock” annotation is used to instruct Mockito framework to create a mock of an object and assign it to variable. We write unit test class…… Continue reading Using @InjectMocks annotation