Skip to content
accelerando

Mockito#validateMockitoUsage

12-Sep-14

Hi, long time no see… I really had a lot of work to do, been doing some freelance work in the nighttime and then there’s certainly my family. And apart from that, my current setup for this years projects is working quite well, thanks to Spring Boot and NetBeans for example.

Recently i had some failing tests. I used to Mockito to verify method calls and though i was sure the tested code was correct, the test kept failing.

What was wrong? The verification of calls run before the next mock. So what was failing was the previous test and I was too stupid to see the navigable exception at first (and second…).

To avoid this behavior and make the test fail that actually fails verification, add the following to your test class:

@After
public void validate() {
    Mockito.validateMockitoUsage();
}

or run your test with the MockitoJUnitRunner.

Read some about this behavior in the Mockito API: Mockito.validateMockitoUsage.

How to screw up iMessages and FaceTime on your iDevice

30-May-14

Recently i managed to screw up iMessages and FaceTime on my phone. Well, not exactly i screwed up, but Apple did.

Some months back i changed my Apple ID but kept the old email address as verified other address in my Apple account. I changed all iCloud settings (Sharing etc.) on all Apple devices because they wouldn’t authorize. iMessages and FaceTime didn’t complain at this time, neither on iDevices nor on a desktop.

Some days ago i decided to change my password and then things went bad. iCloud settings complained as expected but so did iMessages and FaceTime.

The problem? Both programs kept the old Apple ID in their settings and as soon as i hit the Apple-Id button to change it, the modal dialogue that the password was wrong kept popping up. This was the case in both iMessage and FaceTime.

The solution? Hitting the dialogue and the settings button on my iPhone like a madman… I didn’t expect this to work and was ready to reset my phone but sometime my timing was right.

Well. Pretty bad UX fail on Apples side.

So, if you want to change your Apple ID, remember to change this in iMessages and FaceTime immediately if this programs don’t complain right away.

JaCoCo, Maven and NetBeans 8 integration

22-May-14

I was looking for a nice solution to measure the code coverage in my Spring Boot biking project.

It should support Java 8, Maven and for added bonus, my IDE.

I ended up using JaCoCo respectively the Maven plugin.

If you expect a lengthier post, i must disappoint you. All that was need to turn this:

before

into this

after

and also having a nice report like this (right click in NetBeans 8 on the project and choose “Code Coverage > Show Report…”)

report

was the following plugin declaration in maven:

<plugin>
	<groupId>org.jacoco</groupId>
	<artifactId>jacoco-maven-plugin</artifactId>
	<version>0.7.1.201405082137</version>
	<configuration>
	    <excludes>
		<!-- Application starter -->
		<exclude>ac/simons/biking2/Application.class</exclude>
		<!-- Configuration -->
		<exclude>ac/simons/biking2/config/*</exclude>			
	    </excludes>
	</configuration>
	<executions>
	    <execution>
		<id>pre-unit-test</id>
		<goals>
		    <goal>prepare-agent</goal>
		</goals>
	    </execution>
	    <execution>
		<id>post-unit-test</id>
		<phase>test</phase>
		<goals>
		    <goal>report</goal>		
		    <goal>check</goal>
		</goals>
		<configuration>
		    <rules>
			<!--  implmentation is needed only for Maven 2  -->
			<rule implementation="org.jacoco.maven.RuleConfiguration">
			    <element>BUNDLE</element>
			    <limits>
				<limit implementation="org.jacoco.report.check.Limit">
				    <counter>INSTRUCTION</counter>
				    <value>COVEREDRATIO</value>
				    <minimum>0.95</minimum>			
				</limit>
				<!--  implmentation is needed only for Maven 2  -->
				<limit implementation="org.jacoco.report.check.Limit">
				    <counter>COMPLEXITY</counter>
				    <value>COVEREDRATIO</value>
				    <minimum>0.75</minimum>
				</limit>
			    </limits>
			</rule>
		    </rules>
		</configuration>
	    </execution>
	</executions>
</plugin>

That’s it. NetBeans 8 recognizes JaCoCo immediately and everything works (except for my project not reaching my self set limits). No additional installs, no weird maven problems. Awesome.

Also i had no problems with JaCoCo and Java 8 features of any kind.

Java 8: Grouping stuff

06-May-14

I needed a function to sum (and therefor group) the values of a map of objects to Integers. My first solution was something like

As you can see, i use the collect method with a custom supplier, accumulator and combiner. The supplier prepares a new map, the accumulator takes the map and an entry and then uses Map#merge to sum the values.

The combiner than merges all created maps with the same logic.

There’s a nicer solution:

Use Collectors.html#groupingBy. This static helper method takes a classifier and a downstream. The classifier acts the same way as a Group-By clause in SQL, the downstream performs the actual reduction (in this case, a sum).

Neat.

Anyway, i have the slight feeling, i’m recreating a SQL syntax or at least using the idea.

Java 8: Sort or find maximum, minimum entries in maps

05-May-14

This is cool:

Map.Entry#comparingByKey and Map.Entry#comparingByValue. They both take another comparator or lambda that is used as a delegate for creating a Map.Entry comparator, that can be used to sort maps or find maximum and minimum pairs in a map by key or value like in the following example:

import java.time.LocalDate;
import java.util.Map;
import java.util.Random;
 
import java.util.TreeMap;
 
public class FindMaxMinInMaps {
    public static void main(String...a) {
	final Map<LocalDate, Integer> foobar = new TreeMap<>();
 
	// Fill a date -> int map with 12 random ints between 0 and 100, 
	new Random(System.currentTimeMillis()).ints(0,100).limit(12).forEach(value -> 
		foobar.put(
			LocalDate.now().withMonth(foobar.size() + 1), 
			value
		));
	// print them for verbosity
	foobar.entrySet().forEach(System.out::println);
	// get the maximum
	Map.Entry<LocalDate, Integer> max 
		= foobar
		    // from all entries
		    .entrySet()
		    // stream them
		    .stream()
		    // max, obviously
		    .max(
			    // this one is cool. It generates
			    // Map.Entry comparators by delegating to another
			    // comparator, exists also for keys
			    Map.Entry.comparingByValue(Integer::compareTo)
		    )
		    // Get the optional (optional because the map can be empty)
		    .get();
	System.out.println("Max is " + max);
    }
}