Skip to content

Instantly share code, notes, and snippets.

@JorgenRingen
JorgenRingen / TimingExtension.java
Created October 12, 2018 11:46
Timing-extension to measure test performance. Include before SpringExtension to measure test including instantiation of applicationcontext.
public class TimingExtension implements AfterAllCallback, TestInstancePostProcessor {
private static final Logger logger = Logger.getLogger(TimingExtension.class.getName());
private static final String START_TIME = "start time";
@Override
public void postProcessTestInstance(Object testInstance, ExtensionContext context) {
getStore(context).put(START_TIME, System.currentTimeMillis());
}
@JorgenRingen
JorgenRingen / DatabaseCleanup.java
Created August 30, 2018 09:00
Clean up database when using @SpringBootTest and initiating transactions from the test. Test and server runs in different threads.
package no.vegvesen.kjoretoy.registrering.register.web;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Table;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.InitializingBean;
@JorgenRingen
JorgenRingen / pom.xml
Created July 20, 2018 13:56
combine.self="override" overrides the whole plugin definition instead of appending
<profile>
<id>appConfigTest</id>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration combine.self="override">
<groups>appConfigTest</groups>
</configuration>
</plugin>
Testfile located in src/test/resources/testdata/my-test-data.xml.
src/test/resources is current directory when running the test, so not needed in path.
new String(Files.readAllBytes(Paths.get(this.getClass().getResource("/testdata/my-test-data.xml").getPath())))
@JorgenRingen
JorgenRingen / SpringProfilesTest.java
Last active April 10, 2018 09:09
Test configuration for different spring profiles
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@JorgenRingen
JorgenRingen / pom.xml
Created April 5, 2018 10:21
Merging keys with yaml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>yaml-merge-keys</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
@JorgenRingen
JorgenRingen / GenericErrorHandlingWithSupplier.java
Created March 22, 2018 08:41
Do generic error-handling by wrapping method calls in a Supplier
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
class GenericErrorHandlingWithSupplier {
private static final Logger LOGGER = LoggerFactory.getLogger(GenericErrorHandlingWithSupplier.class);
public static void main(String[] args) {
// Example 1: Every caller needs to catch all relevant exceptions
@JorgenRingen
JorgenRingen / Customized http command
Last active March 15, 2018 12:31
Example of customized httpie launcher that set headers
https://httpie.org/
File that needs to be available and executable on path:
#!/bin/bash
USERID="user-id:AKRF1110"
XAUTHTOKEN="X-Auth-Token:$(cat /tmp/jwttoken.txt)"
http --timeout=9999999 "$@" $USERID $XAUTHTOKEN
Test:
@JorgenRingen
JorgenRingen / WrappedresourcesspringhateoasApplication.java
Created March 8, 2018 11:59
Using Resources.wrap from spring hateoas
@SpringBootApplication
public class WrappedresourcesspringhateoasApplication {
public static void main(String[] args) {
SpringApplication.run(WrappedresourcesspringhateoasApplication.class, args);
}
}
@RestController
class FooController {
@JorgenRingen
JorgenRingen / Java8ChainedComparatorTest.java
Created February 6, 2018 18:54
Testing chained comparators from java 8 (about 10% decrease in performance compared to pre-java8 implementation)
public class Java8ComparatorTest {
@Test
public void testJava8ChainedComparators() {
Comparator<Person> personComparator = Comparator.comparingInt(Person::getAge)
.thenComparing(Person::getName)
.thenComparing(Person::getOccupation, (o1, o2) -> {
return o1.compareTo(o2);
});