Capturing and comparing entire objects with Mockito and AssertJ

Let’s say we have a project in which we have the following classes:

We don’t yet have an implementation of the Runway interface (that will be created by another team), but we would like to test if our code creates a correct Plane object and passes it to the Runway. We’ll be using Mockito to mock the Runway implementation to test only the Airport class (so, write an actual unit test). I really like AssertJ so I’ll be using that one for assertions instead of usual JUnit assertions.

Let’s start with testing if plane departs correctly:

We now know that a plane departed, but we don’t yet know if that was the right plane. Let’s try comparing to the plane we expect:

The result is a failing test – not that we wouldn’t expect that.

There are two separate instances of Plane created here. While their fields are all the same, they are different objects and Mockito’s verify method checks that. Moreover, if we wanted to check only if a specific field of passed object has an expected value, we wouldn’t have an ability to check that with Mockito’s verify. There is a class called ArgumentCaptor though that can help us. Also, AssertJ has a neat method for comparing if all fields of two objects are the same:

The dish is ready to be served, enjoy.

Choose your licence, Luke!

Open Source community is all about sharing. If you take a look at the definition of Open Source software, it is computer software with its source code made available with a licence in which the copyright holder provides the rights to study, change, and distribute the software to anyone and for any purpose (source). I bolded the word 'licence’ as this is quite an important part of the definition. Github terms of service state the following:

By setting your repositories to be viewed publicly, you agree to allow others to view and „fork” your repositories (this means that others may make their own copies of your Content in repositories they control).
If you set your pages and repositories to be viewed publicly, you grant each User of GitHub a nonexclusive, worldwide license to access your Content through the GitHub Service, and to use, display and perform your Content, and to reproduce your Content solely on GitHub as permitted through GitHub’s functionality. You may grant further rights if you adopt a license.

It basically means that while people can fork your repository, they aren’t allowed to experiment with it, modify it or share it in different locations.

How many participants have licences?

I wrote a Python script to check that (MIT-licensed ;)). Let’s take a look at the stats:

Licence Count
MIT 114
GNU GPL 46
Public Domain 1
GNU Affero GPL 3
GNU LGPL 2
NO LICENCE 492
EMPTY REPO 155
Apache 16
BSD 3-clause Licence 3

Out of 832 repositories at the moment of writing, 492 (59%) had some code but didn’t have any licence yet (there might be some that used non-standard locations for licences – I checked for the existence and content of LICENSE file in root directory). 155 other repositories (19%) still are empty. That means that only 22% of contest code is now licensed in a way that is actually Open Source.

 

Which licences do people use most often?

How do I choose a licence?

The simplest option is MIT – you basically allow others to use the code as they wish, also in commercial projects. A companies-friendly licence is also Apache Licence. If you want to force others that use your code (i.e. as a library) to also publish their code under a compatible licence, think about GNU GPL. That might mean that commercial entities will hesitate from using your code. At the company I’m working at we’re always making sure we don’t use GPL licenced libraries in our projects. It doesn’t mean though that you cannot use GPL-based software (such as GIMP or NetBeans) in the commercial environment without sharing your results – it’s only if you use it as a dependency.

If you need better guidance, take a look at choosealicence.com.

Feel free to correct me if I got anything wrong, and, for those of you that haven’t done it yet, choose a licence, Luke!