JUnit 5 – assertAll

I gave a half day JUnit 5 workshop at DevNexus last week. When I covered assertAll(), an interesting piece of code came up. In JUnit 5, there is an assertAll() which runs all of the assertions it contains so you can see all of them that failed at once.  This replaces the need for AssertJ’s SoftAssertions.

So how many assertions do you think this will fail with?

@Test
public void all() {
  assertAll("message", () -> {
    assertEquals(1, 2);
    assertEquals(3, 4);
  });
}

The fact that this is a blog post should get you nice and suspicious. The answer is one:

org.opentest4j.MultipleFailuresError: message (1 failure)
expected: <1> but was: <2>

Why is this? Well, remember that assertAll takes a vararg of Executables (lambdas in this case.) However, the above code only has one lambda. So JUnit runs the two asserts in the same block which makes them happen sequentailly.

To fix this, you need to pass separate lambdas for each assertions.

@Test
public void all() {
  assertAll("message", 
	() -> assertEquals(1, 2), 
	() -> assertEquals(3, 4));
  }
}

Ah this one tells you about both failures.

org.opentest4j.MultipleFailuresError: message (2 failures)
expected: <1> but was: <2>
expected: <3> but was: <4>

Thanks to Steve Kiley for asking the question that led to this blog post!

DevNexus 2018 – Bulletproofing your foot for Kotlin

Title: Bulletproofing your foot for Kotlin
Speakers: Enrique Zamudio

Slides (from the Devoxx version of this talk)

For more blog posts, see the DevNexus 2018 live blogging table of contents


About Kotlin

  • Created by JetBrains (makers of IntelliJ)
  • Static typing
  • JVM language
  • Can run on front end or back end
  • Good for Android

Data classes

data class Person(val name:String val birthDate:Date)
  • No need to write getters/setters.
  • toString() and equals() automatically added.
  • However hashCode() is not automatically added.

Destructuring

val p = Person("John", now)
val (n, f) = p
  • Like Scala
  • Stores each field in a variable
  • Similarly can do for ((k,v) in map) when iterating through a map

Functions

fun foo() {}

Takes no params, calls a function and returns Unit (void)

fun bar(f:() -&amp;gt; (Unit)) = f())

Method reference like in Java. Since a top level function, nothing before the ::

bar(::foo)

Passing a lambda directly to a function:

bar ( { println() })

Runs on Java 7

String interpolation

println "Name: $n"

Works like in Groovy

Type inference

Immutable (final) String:

val p = "hello"

Mutable:

var q = "hi"

Recommend specifying type for method so faster.

Smart casts

 val p:Any = "string"
if p is String) {
  println(p.toUppercase())
}
  • Any is a root class
  • Once you check the type, you can use it rather than having to cast.
  • Can still write explicit cast println((p as String).toUpperCase()). Stupid cast?

Typesafe null

var p:String? = "hello"
p = null
if (p != null) {foo(x)}
  • Want to catch errors at compile time
  • If just write var, can’t assign null.
  • Have to explicitly say it is ok by making it an “optional string”
  • Cannot pass an optional string to a method that takes a regular string [cool!]
  • Write guard to check if not null. That is a smart cast to turn String? into String so can call method that takes String
  • ?. returns null if it is null vs a runtime exception. So safe way is to write s?.length
  • The elvis operator ?: from Groovy is also supported.
  • BEWARE: There’s also a kill switch. Writing foo(p!!) you are forcing Kotlin to pass the String? into String without the check. It is a way to say you know what you are doing. Then you get a runtime error if it is null instead of a compiler error.

Operator overloading

class Foo(val x:Int) {
  operator fun plus(o:Foo) = Foo(o.x=x)
}
  • Can only overload simple operators – math operators and index (square square brackets)
  • BEWARE: only way to tell if can use overloading is to look at the class code (or release notes). It’s not in the docs.

Extension methods

fun Person.debug() { println("") {
  • Can add methods to a class even if don’t have the source code.
  • BEWARE: don’t use extension operators. Someone looking at the class won’t know they are there. Not obvious where to look for the code.

Non local returns

  • BEWARE: do not use.
  • Allows writing a return within a closure. In Groovy, it would return from the closure. In Kotlin, it returns from the whole method.
  • Can write return@forEach to return from just the closure. But yech.

My take

This was my first exposure to Kotlin. Some of the features are really cool!

DevNexus 2018 – keynote – the future of the cloud will be containerized

Title: The Future of the Cloud will be Containerized
Speakers: Kelsey Hightower

For more blog posts, see the DevNexus 2018 live blogging table of contents


The wifi was “less reliable” so he had to tether real time
. Once he connected, he showed https://github.com/kelseyhightower/nocode cute joke project if you read the issues and pull requests. The project exists to highlight why coding shouldn’t start until you know what the users want.

Ear/war can’t really be deployed anywhere but with containers you can.

Mobile got this right. You just get the app with everything to run. No need to install prereq. Containers will replace VMs [while i agree this is better, you still have to install the container provider if not cloud]

Cool accidental demo. Used phone wifi but fast because docker caches so much

Riff – severless platform. Listens on a port. Built on top of kubernetes.

Demo used docker, riff, kubernetes, git, heroku qand google voice

My take
Good live demo. I had keyboard troubles aside from internet troubles son this blog is a bit scattered