Tag Archives: Software testing

Mock a dependency in system under test in a Spring Boot application with Mockito

System under test class example:

import my.special.sclient

class App {
  Sclient sclient = getNewSclient();
  public String methodA(String input) { 
    return sclient.getValue(input);

  protected Sclient getNewSclient() {
    return new Sclient();

Test class example:

import org.mockito.InjectMocks;
import org.mockito.Spy;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

class Test {
  private App sut;

  Sclient mockClient;

  public void setUp() {

  public void myTest() {
    String value = sut.getValue("bar");
    assertThat(value, is("foo"));

So, how does this work? I will try to explain below:

1. First we need to be able to intercept the ‘sclient’ in the App class. This is done by simply creating a ‘getNewSclient()’ function and then mock it in the Test class so that we instead can use a mocked version of it.

2. Next we need to setup our test class. Here we need both a @Spy and @InjectMocks on the App object. We also need a mocked instance of the ‘sclient’ we are going to use

3. Now we need to set upp the desired behaviour of our mocked ‘sclient’. We set it so that if you input “bar” you should get “foo” back

4. Done!

Tested on Java v1.8.0_252, Mockito v3.3.3, TestNG v6.11 and Spring Boot v2.2.4

Read the application log when testing your Spring application

From time to time I need to check so that a function correctly logs error messages to the application log. Here is one solution using Logback in a Spring application

import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.LoggingEvent;
import ch.qos.logback.core.Appender;

import org.mockito.ArgumentMatcher;

import org.slf4j.LoggerFactory;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

public void shouldWriteErrorMessageToLog() {
  Logger l = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
  Appender mockAppender = mock(Appender.class);

   * Call function that is expected to write to the application log

  verify(mockAppender).doAppend(argThat(new ArgumentMatcher() {
    public boolean matches(final Object argument) {
      return ((LoggingEvent)argument).getMessage().contains("FooBar");

Tested with Spring Boot v2.2.4 and TestNG v6.11

Install SeleniumServer as a Windows service on Windows Server 2003

In this tutorial I’m going to show how you can set up a Selenium Server as a service under Windows Server 2003. More information about Selenium testing is found here: SeleniumHQ.
One thing to point out is that you can use this way to make almost any program into a service on the Windows platform

Software needed:

  • Windows Server 2003 Resource Kit Tools. You can find it here (the programs we are going to use for this tutorial is instsrv.exe and srvany.exe)
  • Java runtime environment. You can find this here (only needed for the Selenium Server)

The first thing to do is to install a generic service:

  1. Open the command line (type ‘cmd‘ in the run menu)
  2. Navigate to the rktools folder (where you installed the Windows Server 2003 Resource Kit Tools). Usually this is in “c:\program files\rktools
  3. Type: ‘instsrv.exe SeleniumServer “c:\program files\rktools\srvany.exe” -a MACHINEuser -p password


SeleniumServer” is the name of the service (how it is displayed in Services.msc)

c:\program files\rtools\srvany.exe” path to srvany.exe

-a MACHINEuser” the user that will run the service. This kan be changed later*. Use admin at this point to save the rights problems for later ;). (* to change the user of a service: right click it and choose Preferences/login)

-p password” the user password (for the user that runs the service)

The generic service is now ready. Time to attach a program to it:

Starta regedit.exe (type ‘regexp’ in Run menu):

  1. Find the key: HKEY_LOCAL_MACHINE\SYSTEM\Current\ControlSetServices\SeleniumServer
  2. Create a new subkey called “Parameters
  3. Create a new string value (in “Parameters“) called “Application” and set it to the full path of java.exe. For me this was: “c:\Program Files\Java\jre1.5.0_04\bin\java.exe
  4. Create another string value (in “Parameters“) called “AppDirectory” and set it to the folder where the selenium server are. For me this was: “c:\Program Files\SeleniumServer
  5. Create another string value (also in “Parameters“) called “AppParameters” and set it to: -jar seleniumserver.jar (theses are the start parameters for the java.exe)

Now you are ready to start the new service. One way is to type “net start SeleniumServer” on the command line or you can open Services.msc, right click the service and choose ‘Start’

Problems? Check this first:

  1. Check the user rights – does the user have all the rights necessary to run the service/program
  2. Check that the port that is being used is open. Standard port is: 4444
  3. Check the firewall so that it do not stop the traffic on the specific port
  4. Google it! 😉