Posts

Showing posts with the label java

[Video] OOD Guided by Memes, the fun on being SOLID

Image
This is a presentation I made for my colleagues on SOLID principles and clean code. After some positive feedback I decided to record it. Constructive feedback is more than welcome :)

How to test file operations with Junit5 TempDir

In Junit4 we had TemporaryFolder. In Junit5 the way to test objects that are interacting with the file system it is pretty straightforward with the use of @TempDir. To make things easier to understand lets assume that we have a class that reads all the files, directories, sub directories and files in sub directories and gathers information about them in a form of a simple dto that contains the extension, if it is directory, the absolute path etc. and adds them in a list. So given a Path you get a list of objects that contain the file information. In order to test this functionality we are going to use the TempDir helper from Junit5. This will create a temporary directory in our filesystem that will be cleaned automatically when our testing is finished. Check out the example bellow Check on line 20 how we pass the temporary directory as a test parameter annotated accordingly. @TempDir will create a directory (depending on your OS) on a tmp folder like /tmp/junit6110933399026626368, but...

Java Primitive's little secrets

Here is a bunch of java primitives' interesting behaviour that we ofter ignore or forget! Recap time in 3 - 2 - 1... Data type promotions When you try to perform a numerical operation on a short type for example, the result will be promoted to int, therefore it wont compile if you try to store the result in a short variable unless you explicitly cast it (check line x). The same applies to byte. Adding two numbers of type byte they will automatically be promoted to short. What about int? Adding two number of type int together wont result to a long promotion (of course)... Same as int if you try to to add together two float numbers, the result wont be promoted to double. All the above are great... but... There is an interesting behaviour when you do the following the above is perfectly valid, but the bellow results in a compilation error: Yes indeed! an easier one now: Please welcome the infamous Numeric Overflow!