MOCKITO – MOCK vs. SPY and WHEN vs. DORETURN

If you mock a method for a unit test the actual method is not invoked, but what about you want to call the actual method but just mock the return value?

Mockito gives you the @Spy for “partial mocking, real methods are invoked but still can be verified and stubbed” [1] which is perfect, but what if you want to invoke the actual method for one of the methods from the spied object and just manipulate the return value, but also do not want to call the actual implementation for another one from the exact same spied object and just need the manipulated return value. Luckily, Mockito gives you another two options [2]: Continue reading

Advertisements

Java – String Is Immutable And Pass By Value Example

There are three popular questions in Java code interviews:

  • Is Java “pass by value” or “pass by reference”?
  • If you pass an object to a function and change the value does it really change?
  • If you pass a String object to a function and change the value does it really change?

We can find the answers with this simple example. Please try to guess what will be the output.

Continue reading

ngx-translate with Angular 2

I wanted to add internationalization (i18n) support via ngx-translate to my MEAN application that I created with ng-fullstack generator but there was a warning which said
“if you’re still on Angular <4.3, please use Http from @angular/http with http-loader@0.1.0."

So, I should use "Http" module from "@angular/http" instead of "HttpClient" module from "@angular/common/http".
Continue reading

Show PDF Files With PrimeFaces

It is pretty easy to preview pdf files with PrimeFaces media component as explained in official page.

If you try this already there are two common problems that may find you.

  • Shows the same file every time
  • Cannot download the file multiple times (if you have an extra download button even the pdf viewer already have one)

Continue reading

Struts 2 Hello World With Eclipse, Maven and Jetty

I just adapted mkyong’s Struts-2 tutorial [1] to work with embedded Jetty in Eclipse.

How Project Explorer looks like:

struts2_2

Continue reading

SOAP Web Service Server and Client Example With Apache CXF and Eclipse

Let’s create a simple standalone web service example with Apache CXF. First, we are going to build a server then generate a client to send a request to our web service.

Continue reading

Grunt Maven Integration

You may want to integrate Grunt and Maven to be able to build frontend every time just before a Maven build.

For this purpose, I prefer to use exec plugin to invoke “grunt build” in the beginning of each Maven build.

Click to read more…

RESTFUL WEB SERVICE EXAMPLE WITH SPRING 4

Creating a RESTful web service with Spring is pretty easy as mentioned in here [1] and here [2].

Let’s make an example.

Create a new Dynamic Web Project and convert to Maven Project in Eclipse.
Click to read more…

RESTFul Web Service Example with Jersey 2 and Tomcat 8

Jersey RESTful Web Services framework is open source, production quality, framework for developing RESTful Web Services in Java that provides support for JAX-RS APIs and serves as a JAX-RS (JSR 311 & JSR 339) Reference Implementation.

Let’s make an RESTFul web service example with Jersey 2 and Tomcat 8. Our web service is taking a word as parameter, reverse it and return that reversed word as response.

Click to read more…

Dockerize Apache Tomcat on Windows

This stackoverflow post [1] explains how it is possible to build and run an image for Java Web Application on Tomcat, via Docker, but if you are working on Windows, probably it is not going to work for you, because you may face with

cannot enable tty mode on non tty input. [2]

Let’s try something to get over this.

Click to read more…

Advertisements