Separation of concern when using Selenium

Filed under: Java, Selenium, Test automation, — Tags: Page Object Pattern, WebDriver — Thomas Sundberg — 2016-01-14

A lot of people want to automate testing of their web applications. This is definitely a good thing. But it happens that they focus more on the tooling than the testing.

(more...)

Expected exceptions

Filed under: Java, Programming, TDD, — Tags: @Rule, Expected exception, JUnit, checked exception — Thomas Sundberg — 2015-11-20

Sometimes you want to verify that an exception is thrown in your code.

Let me show you three different ways to verify that the expected exception has been thrown.

(more...)

Different asserts are good in different situations

Filed under: Java, TDD, Teaching, — Tags: JUnit, assertThat, assertTrue — Thomas Sundberg — 2015-11-06

When should you use which assert? It is a question developers new to unit tests may ask themselves.

It is easy as a seasoned developer to think that this is obvious. It isn't when you are learning.

(more...)

Packaging a zip file from Java using Apache Commons compress

Filed under: Java, Programming, — Tags: Gradle, org.apache.commons.compress, zip — Thomas Sundberg — 2015-08-21

How do you create a zip file with Java? One option is to use Apache Commons Compress.

This example shows you how.

(more...)

Include header and footer in a Mustache template

Filed under: Java, Web, — Tags: Include template, Mustache, Template — Thomas Sundberg — 2015-06-19

A template for creating web pages is great if you want to integrate front- and backend easy. I have been using Mustache for a while and I'm quite satisfied. It works great with both Dropwizard and Spark

The documentation says how to include a snippet from another template. But searching for it was not that easy.

(more...)

Separating acceptance tests

Filed under: Gradle, Java, Programming, Test automation, Tools, — Tags: Acceptance Test Driven Development, Acceptance testing, Acceptance tests, Automation, Separating tests — Thomas Sundberg — 2015-04-29

It is very convenient to run the unit tests separated from other, slower, tests. There are different ways to do this. One way is to have a separate module for the acceptance tests.

Separating the modules is acceptable in some cases. It is not acceptable in others. There is a simple way to separate the source code for the acceptance tests while keeping the it in the same project if you use Gradle. Separate the tests with different source sets.

Separation on source sets means that you will keep all the acceptance tests in another directory structure than the unit tests. If you use the usual separation of production code and test code, then all you want to do is to add a new source set that contains the acceptance tests.

(more...)

Stubbing a var arg method using Mockito

Filed under: Java, Mockito, Programming, TDD, — Tags: Mocking, stub var arg, stubbing, var arg — Thomas Sundberg — 2015-04-28

I had a need to stub a method the other day that had a var arg method. I failed in stubbing it using Mockito. It disturb me a lot. But after mulling on the problem a while, I searched and found a simple solution.

Mockito has a anyVararg() method that you should use when you try to stub a method that takes a var arg as argument.

(more...)

A Gradle plugin written in Java

Filed under: Gradle, Java, Programming, — Tags: Automation, Gradle plugin — Thomas Sundberg — 2015-03-22

Gradle is a build automation system. You write your build script in Groovy. This is different compared to other build system such as Ant or Maven. They both use xml. Using Groovy instead of xml gives you a lot of benefits. You have an entire programming language at your disposal. This mean that you can easily customize the build behaviour.

If you, however, want to be able to do the same thing in many projects, it may be a good idea to write a plugin that you can refer to from other projects. I will show you, step by step, how to implement a Hello World Gradle plugin.

(more...)

BDD with Cucumber-JVM at GeeCON TDD 2015

Filed under: BDD, Cucumber, Java, — Tags: Behaviour driven development, Cucumber-jvm, GeeCON — Thomas Sundberg — 2015-01-30

This blog post is the same as the example I presented at GeeCON TDD in Poznan, Poland, January 2015. It is a step-by-step example that I hope you will be able to follow and implement yourself.

But before I begin with the implementation, let me reason about why you should care about BDD.

Behaviour Driven Development, BDD, is a way to try to bridge the gap between developers, who can read code, and people who are less fluent in reading code. Cucumber is not a tool only for acceptance testing. It is a communication tool where you can express examples in plain text that anyone can read. These examples can also be executed. They are the outcome from discussions between stakeholders, developers and testers.

Given this, the technical part of BDD that I will show you is the less important part. The most important part is the conversations that occurs and defines the application that should be implemented.

(more...)

An email marketing system built using test first and Cucumber-JVM

Filed under: Cucumber, Java, — Tags: BDD, Cucumber-jvm, Executable specification, JDD 2014, Java Developer Day 2014, Living documentation, Test automation — Thomas Sundberg — 2014-10-23

This is the example I implemented at JDD 2014 in Krakow, Poland at my Cucumber-JVM tutorial.

It is a (baby) step by step tutorial. The purpose for me taking baby steps is that you should be able to follow and implement the same things. Be prepared to spend some time with the implementation, it will probably take you a few hours.

Before we dive into the example, let me define what I am aiming for. My goal is to show you how an example (or specification if you want) can be executed. The example is written in plain text and is used to automate the testing of the system I will create. These executable examples can later be relied upon for regression testing and a living documentation.

(more...)

Making life easier with a multi module Maven project

Filed under: Automation, Java, Maven, — Tags: Multi module, Slow tests — Thomas Sundberg — 2014-09-20

Working with slow modules in Maven is a problem. People will not build the module as often as they need and they will therefore not find problems as early as they could.

A solution could be to separate some of the slow stuff to a separate module. One separation can be to have a specific module for slow tests. This will, however, not solve the problem, that the module is too slow.

A solution to the problem could be to only include it in the execution when you invoke a specific Maven profile. This would separate the execution of a slow module from the execution of the rest, fast, modules.

Let me implement a simple example with two modules. There is the first module, the application, that we always want to build. It has fast unit tests and it is therefore not hindering to execute it often. Then there is the second module, the acceptance tests. It requires you to fire up your application before it can be executed. It is therefore dead slow. As a developer you will probably only want to execute the acceptance test module now and then.

Let me show you one way to achieve this.

(more...)

Run SSH from Java

Filed under: Java, Linux, — Tags: Automation, ssh — Thomas Sundberg — 2014-08-27

Suppose that you need to do something from a Java program on a remote Linux server? How can you do that?

One thing we know is that Linux servers usually supports ssh and that you can do everything you need from a command line. In other words, you need a Java implementation of ssh so you can execute whatever you need on the remote host. Next problem is to either implement ssh yourself or find an implementation that you can use. If you decide not to implement ssh yourself, you will probably prefer a self contained implementation so you don't have to include more dependencies than necessary. This is a good use case for Ganymed SSH-2 for Java. The only thing left is to:

  1. Include a dependency in your Maven, Gradle or Ant/Ivy project
  2. Implement whatever you want to do

I will show you one solution to 1 and 2 above.

(more...)

Passing non primitive objects as parameters to a unit test

Filed under: Java, TDD, Test automation, — Tags: JUnit, JUnitParams — Thomas Sundberg — 2014-04-24

How can you pass a parameter to a unit test parametrised with JUnitParams? The answer is simple: As any other parameter.

(more...)

Is the parameter used?

Filed under: Java, — Tags: JUnit — Thomas Sundberg — 2013-07-31

I watched a presentation by Zsolt Fabok a while ago and saw a technique I hadn't thought of before. How do you verify that a parameter is used in a simple way? Feed the method with something that usually doesn't work and verify that something happens.

(more...)

Test coverage - friend or foe?

Filed under: JUnit, Java, Test automation, — Tags: Bad tests, Cobertura, False positives, Good tests, No assert, Test coverage, Unit tests — Thomas Sundberg — 2012-12-18

Measuring the test coverage is something many people think is a good idea. But is it a good idea? It depends on the quality of the tests. Test coverage may be a good measurement if the tests are good. But suppose that we have a high degree of test coverage and bad tests?

I will show two examples of how to get a 100% test coverage using Cobertura. They will be based on a set of good tests and a set of bad tests.

How is test coverage calculated?

Test coverage is calculated by recording which lines of code that has been executed. If the code has been executed through a test in a testing framework then we have calculated the test coverage.

The calculation is done using these steps:

We will be able to say that 47% of the lines in the source code has been executed. If the execution is done through test code, this will give us a measurement of the test coverage.

(more...)

Selenium WebDriver - the simplest possible start?

Filed under: Java, Maven, Selenium, Test automation, — Tags: WebDriver — Thomas Sundberg — 2012-10-29

Getting started with Selenium WebDriver may be an issue. You must write some code and get the code running. I have created what I think is the smallest possible solution that could work. It consists of two files, a project definition and the actual test.

You will need to have Java and Maven installed. I will not tell you how this should be done, it depends on your environment and operating system.

(more...)

JBoss Drools - a hello world example

Filed under: J2EE, Java, Maven, — Tags: JBoss Drools — Thomas Sundberg — 2012-10-11

What is JBoss Drools? It is a framework where you can create rules that defines when a specific action should be done. This could be done in code using conditions. Creating them using a rule engine can make it easier to combine many business rules with many actions.

I tried to get a JBoss Drools example up and running. It turned out that the examples I found on the web were either were very complicated, tried to solve all possible problems or was just incomplete. I ended up writing my own example where I have removed everything that I didn't find necessary.

I divided this example in two parts. First part is the simplest possible solution that could work, a Hello World. In the second part I try to do something that actually could be useful. I trigger a rule and instantiates a class that could perform some action.

(more...)

Test drive an implementation using an Executable Specification - revisited

Filed under: Cucumber, Java, Test automation, — Tags: Acceptance test, Acceptance test driven development, Agile Cambridge 2012, Cucumber-jvm, Specifications by example — Thomas Sundberg — 2012-09-28

An example is perhaps the best way to describe something. Concrete examples are easier to understand than abstract descriptions.

I will show how Cucumber-JVM can be used to specify an example and how the example can be connected to the system under test, SUT. The example can then be executed using any IDE or using Maven.

(more...)

Separating tests in Maven

Filed under: Java, Maven, TDD, Test automation, — Tags: Acceptance test, Integration test, Separation of concern, System tests, Test suit — Thomas Sundberg — 2012-08-21

The way Maven file structure is defined is a great way to separate unit tests and production code. Unit tests are fast. So fast that developers developing using Test Driven Development, TDD, doesn't have any performance problems when applying the Red-Green-Refactor cycle in their work. A large unit test suit is expected to be executed in seconds.

There is, however, no standard on how the files should be structured for slower tests (integration/acceptance/system et.al.). A common property for these tests is that they are slow. They often require a complete system setup in a known state. They use the filesystem, network, database and similar slow resources. Slow tests are normally not executed often. Developers seldom have the patience to wait for a build a long time before writing the next thing. Using them in a Red-Green-Refactor cycle is not practical. It takes too long time.

So what options do we have to separate the fast units tests and the slow tests? There are two main tracks that I have explored.

(more...)

Performing an action when a test fails

Filed under: Java, Selenium, TDD, Test automation, — Tags: @ClassRule, @Rule, JUnit, Screen shot on failure — Thomas Sundberg — 2012-07-08

JUnit supports annotations so a method can be executed first in a test class or before each test method is executed. It also has annotations that supports methods to be executed after each test method or after the test class. This is very good if you need a common setup and a common tear down.

The after methods don't give you access to the test result. You cannot know if a test method failed or not. This may pose a problem if you have a need to perform some specific actions after a failed test. A solution could be to implement an onError() method. But JUnit doesn't support it.

A solution is to employ a @Rule annotation.

(more...)

Adding a JUnit test template in IntelliJ IDEA

Filed under: Java, Tools, — Tags: IntelliJ IDEA, JUnit, Test template — Thomas Sundberg — 2012-06-01

A test template for JUnit tests doesn't exist out of the box for IntelliJ IDEA. Creating one is not complicated.

The goal is to create a simple template that will assist you when you need to write a test in JUnit. I want it to look something like this:

(more...)

Using Hibernate Second level cache in JBoss 5.1

Filed under: J2EE, Java, — Tags: Hibernate, JBoss, JPA, Second Level Cache — Thomas Sundberg — 2012-05-10

Enabling second level cache in a JBoss when you use JPA 1 is actually relative easy. Understanding how to set its properties is also not to difficult to do. Finding how to specify which configuration file to use when setting the additional configurations is more difficult.

(more...)

Execute tests in random order

Filed under: Java, Maven, TDD, Test automation, — Tags: JUnit, Random, Surefire, runOrder — Thomas Sundberg — 2012-05-03

It works on my machine!

Ever heard that from a developer? I have, and it happens that the reason is that it actually works on their machine. It may also be the case that the order in which tests are executed matters. Test classes depends on each other and the order they are executed in is important. The solution is to execute the tests in random order so that any dependencies between tests are found and can be removed.

(more...)

What is a good test?

Filed under: Clean code, Java, TDD, Test automation, — Tags: Bad tests, Dependency injection, Good test, JUnit, JUnit, Mockito, Parameterized JUnit, Readability, Test harness — Thomas Sundberg — 2012-03-08

A colleague asked that question the other day. What is a good test? It is a really difficult question to answer. There are a number of properties that hold true for a good test. I will list some of them and discuss why I think they are important. Others may think that the order of importance is different or that other properties are more important then those that I list.

I will show some examples using Java and JUnit of what I think are bad tests and how they can be refactored to something better.

Tests are automated in my world. I always try to avoid manually testing. The main reason for this is that manual testing is slow and hard to repeat arbitrary many times. Therefore, test should be read as automated test in for the rest of this text.

(more...)

Test coverage in a multi module Maven project

Filed under: Java, Maven, Test automation, — Tags: Ant, Cobertura, Code coverage, JUnit, Test coverage — Thomas Sundberg — 2012-02-18

Test driving a project should result in a test coverage of 100% of the code. But how can we know what test coverage we have? Is it even important?

(more...)

How many train wrecks are lurking in your code?

Filed under: Clean code, Java, — Tags: Divide and concur, Law of Demeter, Regular expression, Train wreck — Thomas Sundberg — 2011-12-30

Train accidents are mostly considered to be bad things. People tend to get hurt when trains have accidents. Never the less, it is not so uncommon with train wrecks in software development.

Train wreck code is code that calls a method on the return value of another method call. This chain can be very long if a value is excavated deep down in a object graph.

(more...)

The simplest possible solution

Filed under: Agile, Clean code, Java, Software craftsmanship, TDD, — Tags: Simplest possible solution — Thomas Sundberg — 2011-11-16

The simplest possible solution that could work. Ever heard that expression? What does it mean? Really?

The answer is obviously something that in a really simple way satisfies the test you currently are working on. Nothing more, nothing less.

(more...)

Maven Java heap space

Filed under: Automation, Java, Maven, — Tags: Java heap space, java.lang.OutOfMemoryError, maven-failsafe-plugin, maven-surefire-plugin — Thomas Sundberg — 2011-10-14

I just had test in a Maven build fail with

java.lang.OutOfMemoryError: Java heap space
(more...)

Why isn’t my tests being executed?

Filed under: Java, Maven, — Tags: ear, ejb, par, pom, rar, test execution, war — Thomas Sundberg — 2011-10-06

Have you ever wondered why your Maven project doesn't find and execute your tests?

(more...)

Test drive an implementation using an Executable Specification

Filed under: Cucumber, Java, Programming, — Tags: #abe2011, Acceptance test driven development, Agile by Example 2011, Agile by Example 2011, Cucumber-jvm, Executable specifications, Executable specs, Specifications by example, Test automation, Test driven development — Thomas Sundberg — 2011-09-16

An example is perhaps the best way to describe something. Concrete examples are easier to understand then abstract descriptions.

I will show how Cucumber-JVM can be used to specify an example and how the example can be connected to the system under test, SUT. The example can then be executed using any IDE or using Maven.

(more...)

Test drive the implementation of a database layer

Filed under: Java, TDD, — Tags: Database integration, Database integration test, HSQL, Test driven development — Thomas Sundberg — 2011-08-07

Development guided by test is perhaps the best way to make sure that your solution works and is possible to verify. I will show how you can develop a database layer guided by tests. It will be divided into two parts. The first part will be an in memory solution and the second part will be implemented using Java Persistence API, JPA, and Hypersonic. The second implementation is supposed to replace the first one guided by the tests developed for the in memory solution.

(more...)

What is the difference between i++ and ++i?

Filed under: Clean code, Java, Programming, Teaching, — Tags: ++i, difference between i++ and ++i, i++ — Thomas Sundberg — 2011-08-05

During a coaching session I got a question about a loop.

Would there be any difference in the output from a loop defined as

for (int i = 0; i < 3; i++) {
    System.out.print(i + " ");
}

and a loop defined as

for (int i = 0; i < 3; ++i) {
    System.out.print(i + " ");
}

in Java?

(more...)

A Generics example

Filed under: Java, — Tags: Generics, JUnit, TDD, Test driven development — Thomas Sundberg — 2011-05-22

Generics has been a a part of Java since Java 5. It will allow you to write code that handles types without deciding which type upfront. This is of course very useful for collections. It used to be done using the inheritance system and casting Object to the type you knew should be used.

(more...)

FEST Assert - a fluent interface for assertions

Filed under: Java, TDD, Test automation, — Tags: Bottom up, FEST Assert, Fluent assertions, Hamcrest, Hardcoded database, In memory database, Top down, assertThat — Thomas Sundberg — 2011-04-24

Hamcrest is a great framework for assertThat and it is bundled with JUnit. It is getting some competition from another framework, the FEST assertThat framework. The idea behind FEST is to use a fluent interface. This means that you can use your development environments code completion features to build your asserts. Its main goal is to improve test code readability and make maintenance of tests easier.

(more...)

Create an executable jar from Maven

Filed under: Java, Maven, — Tags: Executable jar, maven-assembly-plugin — Thomas Sundberg — 2011-03-05

Creating an executable jar from Maven is not really hard.

This example will show you how you can include stuff in a jar and make it executable while not including stuff that is needed only during the build.

(more...)

Build is platform dependent

Filed under: Java, Maven, — Tags: MacRoman, Platform dependent, Platform encoding, UTF-8 — Thomas Sundberg — 2011-02-22

Chasing the warning:

[WARNING] Using platform encoding (MacRoman actually) to copy filtered resources, i.e. build is platform dependent!
(more...)

Unmappable character for encoding UTF-8

Filed under: Java, Maven, — Tags: Cp1252, MacRoman, National characters, UTF-8 — Thomas Sundberg — 2011-02-17

We had a problem in my current Java project. Whenever somebody wrote a comment using Swedish national characters, åäö or ÅÄÖ, and later tried to build the system with Maven we got the error:

unmappable character for encoding UTF-8
(more...)

Automatically integration test an ejb with Maven

Filed under: J2EE, Java, — Tags: CI, Cargo, Continuous integration, JUnit, TDD, Test automation — Thomas Sundberg — 2010-12-21

We want to perform an integration test of a system using Maven. The application must be deployed on an application server and the application server must be started before we can perform the integration tests. The application should be undeployed and the application server should be terminated after the integration test has been performed no matter what the result of the test was.

How can this be achieved with Maven?

(more...)

Parameterized JUnits tests

Filed under: JUnit, Java, Selenium, — Tags: Integration test — Thomas Sundberg — 2010-07-11

We want to run the same test more than once and only vary the parameters. The solution is to use JUnit and run our tests with the Parameterized JUnit runner.

(more...)

Maven and JBoss ESB - Hello World

Filed under: J2EE, Java, Maven, — Tags: ESB, JBoss, xml — Thomas Sundberg — 2010-07-09

I want to build a Hello World example for JBoss ESB using Maven instead of Ant. The original example bundled with JBoss ESB is using Ant. I have created a pom.xml that will do all the magic and re-located the example files to fit into a Maven structure.

(more...)

Dependency injection

Filed under: Java, TDD, — Tags: Dependency injection, Inversion of Control, IoC, JUnit, POJO, Spring — Thomas Sundberg — 2010-05-16

Dependency injection is invaluable when building software. Decoupling components means that it is possible to test them independently.

(more...)

How to convert a POJO to XML with JAXB

Filed under: Java, TDD, — Tags: JUnit, POJO, dom, jaxb, xml, xpath — Thomas Sundberg — 2010-01-19

We want to convert a POJO, Plain Old Java Object, to xml and we don’t want to alter the POJO in any significant way. How can this be done?

(more...)

HttpComponents - a great tool with great examples based on a future release

Filed under: Java, — Tags: HttpComponents, Maven, Web application — Thomas Sundberg — 2009-08-04

Open source is a great thing when it works. It happens that the documentation is less then perfect or that the examples provided doesn't work out of the box.

(more...)

Getting started with Continuous Integration

Filed under: Continuous integration, Java, — Tags: Hudson, Maven, Mercurial, Test automation — Thomas Sundberg — 2009-04-22

A quick Continuous Integration introduction example.

(more...)

Integration test a web application with Selenium

Filed under: Java, Selenium, Test automation, — Tags: Cargo, Integration test, JUnit, Jetty, Maven, Spring, TDD, Web application — Thomas Sundberg — 2009-04-17

We want to build a web application and we want to test it automatically.

(more...)

Hibernate, Spring and Maven

Filed under: J2EE, Java, — Tags: HSQL, Hibernate, Maven, Spring, TDD — Thomas Sundberg — 2009-04-09

We will create a small Java application that connects to a database using Hibernate. We will use Spring to wire stuff together and Maven to build it.

(more...)

How can I create a simple web application using Spring MVC?

Filed under: J2EE, Java, Maven, — Tags: MVC, Spring, TDD, jsp, xml — Thomas Sundberg — 2009-04-06

I want to to create a very simple web application using Spring. I want to create it using Maven as build tool.

(more...)

Pages

About
Events
Why

Categories

Agile
Automation
BDD
Clean code
Continuous delivery
Continuous deployment
Continuous integration
Cucumber
Culture
Design
Executable specification
Gradle
Guice
J2EE
JUnit
Java
Linux
Load testing
Maven
Mockito
Pair programming
PicoContainer
Programming
Public speaking
Quality
Recruiting
Requirements
Scala
Selenium
Software craftsmanship
Software development
Spring
TDD
Teaching
Technical debt
Test automation
Tools
Web
Windows
eXtreme Programming

Authors

Thomas Sundberg
Adrian Bolboaca

Archives

Meta

rss RSS