Good programming practice How to Java EE JAVA SE

JUnit: Unit Testing

JUnit is used for unit testing. The unit testing is one of the testing methodologies that tests module or method that was written.

Writing the test is done by creating normal java class, except we use special annotations for it.

Note: best practice in unit testing is to have multiple tests for single method.

Let’s say you have method calculateMinAge(), it’s good idea to create multiple test methods

Unlike other java classes, JUnit test class has different structure:

@Suit or @TestRetention <– this annotation helps to include the current class file into larger test group, containing other test files

@Before <– this annotation can be used to initialize the objects that are used in different methods. Also the method name should be called setUp(). Ex:


public void setUp(){}



Good programming practice How to Java EE JAVA SE

Inline If statement

In order to save the lines of code instead of using the typical if else we can use inline if statment:

skeleton is (a condition b ? do this() : do that())



(a>3 ? peal() : leave())

In above example a will be checked if it’s bigger than 3 if so run peal() if not run leave()


getAge(a>3 ? a=10: null)

in above example if a more than 3 then a will be assigned to 10 if not then get will run with value null.

Good programming practice How to Java EE JAVA SE

Conflict merging

In case of conflicts,

1. copy Current changes from Right to left
2. right click on the file, Mark as merged.

Good programming practice Java EE JAVA SE Uncategorized

Static method

One rule-of-thumb: ask yourself “does it make sense to call this method, even if no Obj has been constructed yet?” If so, it should definitely be static.

So in a class Car you might have a method double convertMpgToKpl(double mpg) which would be static, because one might want to know what 35mpg converts to, even if nobody has ever built a Car. But void setMileage(double mpg) (which sets the efficiency of one particular Car) can’t be static since it’s inconceivable to call the method before any Car has been constructed.

Enterprise Java Beans(EJB) Good programming practice How to Java EE

Json to Java object conversion

Let’s say you have rest service that posts some data to your server. The rest services uses JSON format to send data, but your server might not understand the JSON, therefore you will need to convert the JSON to Java object. Below is the sample of such service:


[code language=”java”]
public Response getStatistics(String json)
throws JsonGenerationException,

IOException {

ObjectMapper mapper = new ObjectMapper();

SomeCustomerTO someUser = mapper.readValue(json, SomeCustomerTO .class);
return null;

Always good idea to include some logging to see if you receiving the correct json

Enterprise Java Beans(EJB) Good programming practice How to Java EE

Adding the Oracle connectors to IntelliJ IDEA

Libraries cannot be directly used in any program if not properly added to the project gradle files.

This can easily be done in smart IDEs like inteli J.

1) First as a convention add a folder names ‘libs’ under your project src file. (this can easily be done using the IDE itself)

2) then copy or add your library file (eg: .jar file) to the folder named ‘libs’

3) now you can see the library file inside the libs folder. Now right click on the file and select ‘add as library’. And this will fix all the relevant files in your program and library will be directly available for your use.

Good programming practice Uncategorized

Programming tips

1 . Use the enhanced loop

[code language=”java”]
String[] words = {"apple","pear","potato","tomato"};

for(String w : words){


System.out.println(w+" starts with fu")};

Good programming practice Java EE JAVA SE

Eclipse optimization

How do I open a page from search result in new tab?

Sol: uncheck

Window > Preferences > General > Search > [ ] Reuse editors to show matches

Good programming practice

How to pass values to .bat file

Inside the .bat file:


set variableName1=%1
set variableName2=%2
set variableName3=staticValue


call mvn install:install-file -Dfile=”%variableName1%\abcd.jar”


Company related Good programming practice

Error: Oracle SQL Developer: IO Error: The Network Adapter could not establish the connection

Error 17002

Solution: services.msc

2. right click on OracleXETNSListener

then start it, or restart it.