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:

@Before

public void setUp(){}

 

 

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())

ex1:

a=5;

(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()

ex2:

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.

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.

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”]
@POST
@Path("/statistics")
@Consumes(APPLICATION_JSON_UTF8)
public Response getStatistics(String json)
throws JsonGenerationException,
JsonMappingException,

IOException {

ObjectMapper mapper = new ObjectMapper();

SomeCustomerTO someUser = mapper.readValue(json, SomeCustomerTO .class);
return null;
}
[/code]

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

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.