Initialize a Typescript Interface with JSON

Posted by in Software

TL;DR: You can initialise a typescript interface using JSON objects. On my quest to load and save objects as JSON, I found that you can convert a JSON object to an interface via something called type assertion. Basically, if you have an interface:

you can load a JSON object via JSON.parse(json) straight to an instantiation of the interface. Then, you can use the said instantiation in e.g. a component. Now, all is nice, but what if you want to initialise an instance of the PathInterface interface? That’s simple. You…read more

Mock an Observable Interface

Posted by in Software, Tutorial

TL;DR: Use a doAnswer() with a new Answer<Observable<Type>>() if you have to mock observables. Today I wrote my first test where I had to mock an observable interface:

where the User model is:

Solution In my test class (named UserDatabaseTest), I first mocked the interface:

` Then, my setup() method is:

where USER_NAME is a String. To use it, you need to attach an observer and assert:

This is quite simple. Alternative Note: This I haven’t tried yet, but here it goes… If you want…read more

GoogleApiClient .connect() Interface or Class

Posted by in Software

TL;DR: If it’s your code, check the version play services (*). If you’re using a library, check if the library has a version using play services >= 8.1.0. If not, you’ll need to clone and upgrade it yourself. The other day I got an interesting error coming out when running an app: ‘GoogleApiClient.connect()‘ was expected to be of type interface but was found to be virtual The reason was a backwards incompatible change in 8.1.0 where GoogleApiClient changed its signature. Moreover, it was in a library using version 7.x while…read more

TIL: Lambda Expressions

Posted by in Software

TL;DR: A lambda expression is a shortcut notation to anonymous inner classes containing a single method. Usually, they define implementations for functional interfaces (interfaces declaring a single method). Today I finally found out what lambda expressions are (formally) by watching this tutorial. Lambda expressions are a concise way of passing pieces of code around. You can look at lambda expressions as shorthand for anonymous inner classes. There is a catch though: They are valid for functional interfaces. Functional interfaces Functional interfaces are interfaces defining a single method, like this:

read more

Use a Java Enum with Strings

Posted by in Software

In my project I wanted to log messages using predefined categories. Traditionally, I do it via classes (if the messages are specific to the functions provided by that class):

or an interface (pretty much the same thing):

These solutions are prone to errors (e.g. when performing copy-paste and forgetting to change the string), so I wondered if there is an enum-based approach. Of course there is:

Quite nice, but more verbose than the interface variant (and also prone to the copy-paste duplication). That’s why I ended up…read more