Writing unit test is very important for better software quality. For unit tests Mockito is one of the most common choices of developers. Mockito providers different way to mock methods, like do...when and when..then. Most of the time we face the question to use when-then vs do-when. We will see all the differences in detail.
In Mockito we can mock methods in 2 different ways,
Return type of doReturn(..) is Object whereas the return type of thenReturn(..) is as per the method type. So in case of doReturn we might get org.mockito.exceptions.misusing.WrongTypeOfReturnValue exception if incompatible return value is used. In case of thenReturn, if we use wrong value the application won't compile.
For mocked objects it is best practice to use when-then option as it provides return type checking and it is more readability. However it has drawbacks in case of spied objects and void methods.
Mocking methods of spy object
Spy objects are linked to the actual objects, where we dont have to mock all the methods. And actual method calls are made for the methods that are not mocked. For mocked object all the methods are mocked, and there we dont need real objects.
When we are using mock, first we need to create mocked object. Then we specify what should be returned using when-then. In that case it don’t do anything with real class.
In the previous post we saw baiscs about java 9 modules like, what is module, how to create module project, module descriptor file and so on. In this blog we will learn how to compile and run java9 module program.
Before going further let's take an example of hello world module,
Let's dive deeper with an example, for better understanding we will create two modules. We will put the modules under the folder 'project'.
First module, we will create is 'com.module.util' module. In this module we will create module-info.java and Greeting.java files. Second module will be 'com.module.app' module. In this module we will create module-info.java and Main.java files.
Let's create two folders for modules as 'com.module.util' and 'com.module.app' in the 'project' folder. These modules contain files Greeting.java and Main.java respectively. Both modules will have module-info.java at top level as shown below.
Once we execute the above command, we can see that the .class file is created under libs\com\module\util\Greeting.class
Now if we would like to run the main class created by above command, we have to specify path where jvm can find class files. Since our classes are present under libs folder we can specify path using –cp or -classpath.
D:\project>java -cp libs com.module.util.Greeting
Greeting class is working
Compile and run java 9 module program
Now we have seen how to run java package program and put the classes into separate folder, to avoid mixing .java and .class files. We will use -d option for modular program as well.
Most important thing to remember is that, while compiling module, we have to compile .java source files and module descriptor file(module-info.java) .
In windows path are separated by semi-colon(;) and in Linux and mac using colons(:)
Most important new argument options
There are few new parameters/argument types introduced in java 9 for running the modular program that we must know.
module-path: Module-path option is used to specify where the modules are located. It is used at compile and run time.
At compile time it is used with javac option to specify path where the dependent modules can be found. Syntax : javac --module-path path1;path2
At runtime it is used to specify dependent module and module which is to run Syntax : java -module-path pathslist Note: we can use -p path as shortcut for --module-path path.
module: The --module argument is used to compile list of modules or run a module.
Syntax : javac –module path1;path2
Syntax : java –module module/class Note: we can use -m as shortcut for –module.
module-source-path: the argument --module-source-path us used to specify the root directory where the source files or packages are placed. Sometimes the projects are organized in such a way that the code is placed in special folder. For example src/main/java
Note: when we are using –module or –m option it is mandatory to use –module-source-path option, even if the source files are in same folder. - - module-path == > -p - - module == > -m
Let's assume we would like to run Main class from com.module.app module. Com.module.app module is dependent on com.module.util module. Com.module.util module is compiled in libs folder Com.module.app module is compiled in app folder.
When required modules is not specified in module path
Run syntax: java -p requiredModulesPath -m module/package.MainClass
Frequently asked question FAQ:
What is Module resolution process?
When we run the module program, at start jvm checks for all required modules. This process of finding all modules at start is call as Module resolution process. We can print modules scanning process log by using command –-show-module-resolution in the run command. Note: in module resolution process first main module is searched then it keep adding required module in tree form.
What are the java restricted keyword or contextual keyword?
Newly added 10 keywords, which are considered as keyword only in case of Module descriptor file(module-info.java), are called as restricted keywords. This keywords are open, module, requires, transitive, exports, opens, to, uses, provides, and with. For backward compatibility in every other cases it is considered as identifiers. Unlike other keywords we can use this keywords is variables or method name.
What is module descriptor file?
From java 9, special file with name module-info.java is requied in root folder of module, which specifies the metadata of module, this file is called as module desciptor file.
Can we export multiple package in one line?
No, To export multiple packages separate exports keyword is required.
Can we add multiple modules dependancy using single requries keyword?
No for separate module new requires keyword is required.
In this blog we will go through one of the most important features of java 9, which is 'Modules' aka 'Java Platform Module System (JPMS)'. We will understand everything about JPMS like, what is module? How it helps to add modules? and How to create and use module? Even if you don’t know anything about module don’t worry we got it covered.
If you are scared of this new word('Modules'), don’t worry once you understand it, it will be very easy.
Difference between JDK8 and JDK9
We all know that JRE is the most important part of JDK. But, since java 9, JDK does not contain the JRE folder 😮. Yes! that is true, because from java 9 JRE is converted to multiple small modules and they are present in folder called 'jmods'.
We can list system modules or the contents of this 'jmods' folder by using the command : java --list-modules.
Module system is a part of Jigsaw Project. It adds one more abstraction level above packages. In other words, it is a 'package of Packages' that makes our code even more reusable. It is also fine to say that a module is a group of closely related packages, resources and module descriptor(module-info.java) file.
In Java 9 'java.base' is a base module. It does not depend on any other modules. By default, all modules including user defined modules are dependent on this module.
Even if we do not specify 'java.base' module, it will be imported automatically.
Features of java 9 Modules
Increases code reusability: by creating modules we can use them in different projects
Easy and meaningful grouping of packages: if we have many packages in one project it is difficult to manage and organize code, this is where modules come to the rescue
More abstraction to packages:we can decide which packages are allowed to be accessed outside and which are private or for internal use
Separation of resource: each module will have it's own required resource files like media or configuration files
Internal or secure classes can be hidden from outside world
Steps to create Module
Create a folder with module name. Generally company name in reverse with artifact name is used. eg: 'com.stacktraceguru.util'
Add file with name 'module-info.java' in module root folder. This file is called as 'Module Descriptor' file
Create java packages as per requirement
Add classes as required under the created packages
What are the rules for creating Module?
Module name must be unique
Each module must have exactly one Module Descriptor file with name 'module-info.java'
Package names must be unique. Even in the different modules we cannot have same package names
We can add media and other resource files in the module
Each module will create one jar file. For multiple jars we need to create separate modules
One project can have multiple modules
Note: Module name should not end with digits
What are theModule types?
Depending on how the modules are used, they are categorised into 4 types,
System Modules: the modules from JDK and JRE. Can be listed using java ´--list-modules
Application Modules: all the modules created in an application to achieve a functionality
Automatic Modules: existing jar files which are not modules but are added to module path. When we add non module jars to module path, module with jar name is created.
By default exports all the packages
By default can access classes from all other modules
Unnamed Module: jars and classes added into the classpath. When we add jar or class to the classpath all these classes are added to the unnamed module
Only exports to other unnamed module and automatic module. This means, application modules cannot access these classes
It can access classes from all the modules
What is Module Descriptor file?
It is a file with name module-info.java, under the root module path. This file contains the module metadata information.
This is also java file which is compileable using javac command.
This file defines following things
Public packages: list of packages that current module exports using 'exports' keyword
Dependencies on other modules: list of other modules on which the current module is dependent on. This is done using 'requires' keyword
Services offered: list of services that current module provides using 'provides' keyword
Services consumed: list of services that current module consumes using 'uses' keyword
Reflection permission: permission to specify if refection can be used to access private members using 'open' keyword
Note: Module descriptor file needs to export packages as by default all packages are private. Also, we can not use reflection on other module classes. We need to enable reflection in order to use reflection.
In this case only classes from package1 are accessible using reflection.
Opens … To
Using 'opens ...to' keyword we can open reflection permission for specific packages to specific modules only.
opens com.module.package1 to module.a, module.b, org.test.integration;
In this case only module.a, module.b, org.test.integration modules can access classes from package1 using reflection.
Note: If we need reflection access to the module, we can gain the access using command line option '–add-opens', even if we are not the owner of the module.
First of all, this is not a technical concept. It is just a convenience concept for developers to make there life easier.
Sometimes multiple modules require other multiple modules. Instead of adding these in every module descriptor, we can create one module that will add all the required dependency using 'transitive'. Then we just need to add dependency of this module wherever needed, this will add all the required modules transitive dependency. This common module is the 'Aggregator module'.
For example, we have 10 modules, modA to modJ. modP, modQ, modR needs all 10 modules, then we can create one common module as below,