Testing has become Mandatory (and there are no more excuses)

There are many posts out there about the value of automated testing and why it is a must through the life cycle of a software product.

Most people and teams totally agree with this statement, so what goes wrong?

It is just a prototype

It seems to be a valid statement, but if we consider how many times a prototype made it into production then it is not so valid.
In fact most prototype codebase ends up to production because this is the goal of a prototype.
Adding tests in a prototype phase makes absolutely sense.

Too difficult to test

Yes there cases where testing is really difficult due to limitations. For example the android and he iphone emulators do not give you the ability
to set accelerometer events, or you use a service which provides you with no test utilities at all, or a testing environment.
Even if mocking would not make an absolute test case, it really can assist you on making a specification on how things work.
The other scenario is testing being hard to implement due to the codebase.
Consider this as an indicator that things are not simple enough. If it is just your codebase then your are lucky, It is up to you to make it more testable and more simple.

Too much to do, too little time

This is our weakest spot and the one that we are most prone too succumb.
Suppose you develop a smartphone application. In case of manual testing each mistake will cost you 3-5 minutes. Starting the emulator or even worse deploy to a physical device, load the application and press some buttons to create events, and in case of an error repeat and loose other 3-5 minutes.
In case you develop a server application things might get even worse. Connect to the sever, upload the application, check that that the upload was successful and then manually test that it works ok, and in case of an error repeat and loose some time again.
Also keep in mind that in case of projects with more than one developers involved, mistakes are more common to happen.
We tend to believe that the best case will happen where everything will work as expected.
It might work most of the time but in case of a problem you loose big. We end up being hooked on an “It’s ok all it needs is another version upload” mode.
Next time this happens just count how much time you end up loosing by testing manually until everything is ok.
Then estimate how much time tests will cost you and how much time you win in such cases.

Everything changes so rapidly

Yes a project on its initial phase, will have a completely different codebase in two months than its initial one.
But since big changes are made more errors are likely to occur. A minimal amount of tests ensures that basic features would continue to work.

All in all testing is not as hard as it used to be.
Nowadays almost every service or utility that we use comes with some test utils.
As the software industry becomes more challenging, codebases without tests would become extinct.
Pick whatever methodology you want but just do it. Your life will become much easier.

Implement custom JMeter samplers

As we proceed on different architectures and implementations the need for versatile stress testing tools rises.

Apache Jmeter is one the most well known tools when it comes to load testing. It supports many protocols such as ftp http tcp and also it can be used easily for distributed testing.

Jmeter also provides you with an easy way to create custom samplers. For example if you need to load test a http endpoint that requires a specific procedure for signing the headers then a custom sampler will come in handy.

The goal is to implement a custom sampler project which will load test a simple function.

I use gradle for this example.

group 'com.gkatzioura.jmeter'
version '1.0-SNAPSHOT'

apply plugin: 'java'

sourceCompatibility = 1.6

repositories {
    mavenCentral()
}


dependencies {
    compile 'org.apache.jmeter:ApacheJMeter_java:2.11'
    compile 'org.json:json:20151123'
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

task copySample(type:Copy,dependsOn:[build]) {

    copy {
        from project.buildDir.getPath()+'/libs/jmeter-sampler-1.0-SNAPSHOT.jar'
        into 'pathtoyourjmeterinstallation/apache-jmeter-2.13/lib/ext/'
    }
}

I include the ApacheJMeter dependency on the project since the sampler will have to extend the AbstractJavaSamplerClient.
The copySample task will copy the jar to the lib/ext path of Jmeter where all samplers reside.

A simple function will be called by the sampler

package com.gkatzioura.jmeter;

/**
 * Created by gkatzioura on 30/1/2016.
 */
public class FunctionalityForSampling {

    public String testFunction(String arguement1,String arguement2) throws Exception {

        if (arguement1.equals(arguement2)) {
            throw new Exception();
        }

        else return arguement1+arguement2;
    }

}

The CustomSampler class extends the AbstractJavaSamplerClient class and invokes the testFunction.
By overriding the getDefaultParameters function we can apply default parameters that can be used with the request.

package com.gkatzioura.jmeter;

import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;

/**
 * Created by gkatzioura on 30/1/2016.
 */
public class CustomSampler extends AbstractJavaSamplerClient implements Serializable {

    private static final String METHOD_TAG = "method";
    private static final String ARG1_TAG = "arg1";
    private static final String ARG2_TAG = "arg2";

    private static final Logger LOGGER = LoggerFactory.getLogger(CustomSampler.class);

    @Override
    public Arguments getDefaultParameters() {

        Arguments defaultParameters = new Arguments();
        defaultParameters.addArgument(METHOD_TAG,"test");
        defaultParameters.addArgument(ARG1_TAG,"arg1");
        defaultParameters.addArgument(ARG2_TAG,"arg2");

        return defaultParameters;
    }

    @Override
    public SampleResult runTest(JavaSamplerContext javaSamplerContext) {

        String method = javaSamplerContext.getParameter(METHOD_TAG);
        String arg1 = javaSamplerContext.getParameter(ARG1_TAG);
        String arg2 = javaSamplerContext.getParameter(ARG2_TAG);

        FunctionalityForSampling functionalityForSampling = new FunctionalityForSampling();

        SampleResult sampleResult = new SampleResult();
        sampleResult.sampleStart();

        try {
            String message = functionalityForSampling.testFunction(arg1,arg2);
            sampleResult.sampleEnd();;
            sampleResult.setSuccessful(Boolean.TRUE);
            sampleResult.setResponseCodeOK();
            sampleResult.setResponseMessage(message);
        } catch (Exception e) {
            LOGGER.error("Request was not successfully processed",e);
            sampleResult.sampleEnd();
            sampleResult.setResponseMessage(e.getMessage());
            sampleResult.setSuccessful(Boolean.FALSE);

        }

        return sampleResult;
    }

}

After compile is finished the jar created must be copied to the lib/ext directory of the JMeter installation home.
Also in case there are more dependencies that have to be imported they should also be copied to the lib path of the JMeter installation home

Once the process is complete by adding Java Sampler to a JMeter Thread Group we can choose our custom sampler.

Screenshot from 2016-01-31 01:30:06

You can also find the source code here.