Blog

Following on from my recent article on Kotlin for Scala developers, in this blog I’ll introduce how you can use the KotlinTest framework to write unit tests in Kotlin, which is a great way to begin exploring Kotlin.

KotlinTest is based on ScalaTest (a superb jUnit like test framework created by Bill Venners) and offers the same choice of testing styles as well as many assertions and helpers for matching through the use of a light DSL.

To use KotlinTest first add the dependencies to your build. We’ll assume you’re using gradle (but obviously Maven is fine too).

compile 'io.kotlintest:kotlintest:1.1.1'

Then we can create a test. Each test must extend from one of the spec classes. Each spec class offers a different way of laying out your tests. Some are very similar and similar to jTest’s @Test annotation, and some are more complicated offering BDD style descriptions.

Testing Styles

Lets give an example of the FlatSpec, which allows you to write tests like this:

As you can see, you start with some text, then the keyword should, then some more text, and finally the test as a block. This gives you a readable description of what your test is doing. The split of the test description into two parts means that the IDE / build tools can nest the test output into something like:

ListStack.pop should

return the last element from stack ok

remove the element from the stack ok

All tests need to be placed inside the init {} block. For those of you coming from a Scala background, this is because Kotlin doesn’t allow the body of the class to be treated as the constructor like you can in Scala.

A quick example of another style is WordSpec, in which the tests are nested. So,

I think this is fairly self explanatory. The rest of the testing styles are available on the KotlinTest homepage.

Matchers

Let's move onto the matchers. When writing your tests in Java, it is common to use assertions like assertEquals(a, b). This is fine, but we can do something a bit more readable with KotlinTest. As a simple example, to assert two values are equal, we can say

"a" shouldBe "a" or "a" shouldEqual "a"

We can do clever things with strings:

someStr should start with "foo"
someStr should end with "bar"
someStr should have substring "xxx"

Or with collections

myList should have size 4
mySet should contain element "a"

There are many matchers available for collections, strings, longs, ints, etc.

Sometimes we need to assert that an exception is thrown under certain conditions. The typical way to do this in other test frameworks is to wrap the code in a try/catch and then assert false at the bottom of the try, eg

We can remove this boilerplate in KotlinTest using the shouldThrow<T> function. Just wrap your code with this function, replacing T with the type (or supertype) of Exception you want to catch, eg

shouldThrow<IllegalArgumentException> {
// code that should throw an exception
}

One of the more tricky things in testing, is when you have code that completes non-deterministically. Perhaps you’re sending off an async call and need to wait until a reply comes back.

The naive way to do this is to sprinkle your test code with Thread.sleeps but how long to wait? You don’t want the time-out to expire prematurely. So if your call takes 2 seconds to complete, you set your sleep to 5 seconds. Until the time your build server is running slow, and 5 seconds wasn't enough, so you set it to 15 seconds. But now you wait 15 seconds every time regardless.

Another tool is to use countdown latches, and these are often my preferred choice. Have some code trigger the latch once it's completed (i.e. in a callback) and have the main test thread block on the latch.

If that doesn't work, KotlinTest introduces the eventually function. This allows you to repeatedly probe for a test condition and once it passes the function will return. This means you can wait up to a set time, but no more. It’s very easy to use, you just supply the max time you’re prepared to wait before considering it a failure. Extend the Eventually interface in your test class, e.g.:

Writing that without an inspector would be at least a few lines longer.

The final thing I want to mention is setting up and tearing down tests. We can do this before and after each test, or before and after each test suite (test suite is the class file). To do this we just override whichever methods we need, e.g.

That’s all for now. This should help get you up and running and using Kotlin in your projects. In the next article, we’ll write about how you can create custom matchers to allow your own DSL syntax for complicated matchers.

Please let us know your experience of testing in Kotlin in the comments below.

Microsoft is preparing to release the second version of its Open CloudServer design for use with the the Open Compute Project (OCP). Dubbed “Project Olympus,” it lives up to its Goliath namesake by boasting significant improvements over its predecessor.

First released as a Technical Preview version on October 1, 2014 and later released under general availability on October 12, 2016, Windows Server 2016 is the newest version of Microsoft's signature server operating system. It was developed alongside Windows 10, offering many of the same functions and features. Of course, Windows Server 2016 also introduces several new features, including Active Directory Federation Services, Windows Defender antimalware, Remote Desktop Services to support OpenGL 4.4 and OpenCL 1.1., Storage Services, Failover Clustering, Web Application Proxy for preauthorization of HTTP Basic, and ISS:10 support for HTTP/2, and Windows PowerShell 5.1.

If you currently use Microsoft's Hadoop offering, you'll be pleased here about several new features and enhancements that are being added. Earlier this month, Microsoft announced that it's cloud-based Hadoop service, Azure HDInsight, would receive a security upgrade and performance boost. Microsoft claims these changes will provide greater security for users, while also speeding up Big Data queries by as much as 25x.

Microsoft and Huawei have joined forces with EastWest Institute to help IT organizations and professionals securely buy informations and communications technology (ICT) products. The powerhouse tech companies say ICT buyers are at risk for cyber attacks when using commercial products and services. And with phishing and ransomware attacks on the rise, this isn't a trend that's likely to go away anytime soon.

Red Hat, Inc., the North Carolina-based multinational software company that's best known for its enterprise operating system Linux, has released a new version of its virtualization product, Red Hat Virtualization (RHV).

Virtual machines (VM) have become a hot commodity among IT companies and organizations. They perform the functions of a standalone computer or server in an emulated environment. But if you're thinking of investing in a preemptive virtual machine, you should check out the service offered by Google, because the Mountain View company has just slashed the prices for such services.