Why should I use Hamcrest-Matcher and assertThat() instead of traditional assertXXX()-Methods

There’s no big advantage for those cases where an assertFoo exists that exactly matches your intent. In those cases they behave almost the same. But when you come to checks that are somewhat more complex, then the advantage becomes more visible: val foo = List.of(“someValue”); assertTrue(foo.contains(“someValue”) && foo.contains(“anotherValue”)); Expected: is <true> but: was <false> vs. … Read more

Spring Test & Security: How to mock authentication?

Seaching for answer I couldn’t find any to be easy and flexible at the same time, then I found the Spring Security Reference and I realized there are near to perfect solutions. AOP solutions often are the greatest ones for testing, and Spring provides it with @WithMockUser, @WithUserDetails and @WithSecurityContext, in this artifact: <dependency> <groupId>org.springframework.security</groupId> … Read more

Java verify void method calls n times with Mockito

The necessary method is Mockito#verify: public static <T> T verify(T mock, VerificationMode mode) mock is your mocked object and mode is the VerificationMode that describes how the mock should be verified. Possible modes are: verify(mock, times(5)).someMethod(“was called five times”); verify(mock, never()).someMethod(“was never called”); verify(mock, atLeastOnce()).someMethod(“was called at least once”); verify(mock, atLeast(2)).someMethod(“was called at least twice”); … Read more

Configuring IntelliJ IDEA for unit testing with JUnit

If you already have a test class, but missing the JUnit library dependency, please refer to Configuring Libraries for Unit Testing documentation section. Pressing Alt+Enter on the red code should give you an intention action to add the missing jar. However, IDEA offers much more. If you don’t have a test class yet and want … Read more

Getting “Skipping JaCoCo execution due to missing execution data file” upon executing JaCoCo

jacoco-maven-plugin:0.7.10-SNAPSHOT From jacoco:prepare-agent that says: One of the ways to do this in case of maven-surefire-plugin – is to use syntax for late property evaluation: <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <argLine>@{argLine} -your -extra -arguments</argLine> </configuration> </plugin> Note the @{argLine} that’s added to -your -extra -arguments. Thanks Slava Semushin for noticing the change and reporting in the … Read more

How to test code dependent on environment variables using JUnit?

The library System Lambda has a method withEnvironmentVariable for setting environment variables. import static com.github.stefanbirkner.systemlambda.SystemLambda.*; public void EnvironmentVariablesTest { @Test public void setEnvironmentVariable() { String value = withEnvironmentVariable(“name”, “value”) .execute(() -> System.getenv(“name”)); assertEquals(“value”, value); } } For Java 5 to 7 the library System Rules has a JUnit rule called EnvironmentVariables. import org.junit.contrib.java.lang.system.EnvironmentVariables; public class … Read more