Skip to content

Instantly share code, notes, and snippets.

@siordache
siordache / PartialMockingAbstractClasses.groovy
Last active August 29, 2015 14:25
Spock specification for testing partial mocking of abstract classes
import spock.lang.Specification
class PartialMockingAbstractClasses extends Specification {
def "Square with stubbed getLength()"() {
given:
Square square = Spy() {
2 * getLength() >> 3
}
when:
def area = square.area
@siordache
siordache / ISquare.java
Created July 23, 2015 00:45
Spock specification for testing partial mocking of interfaces with default methods
public interface ISquare {
double getLength();
default double getArea() {
return getLength() * getLength();
}
}
@siordache
siordache / Range.java
Created August 11, 2015 15:28
A Spock specification that fails in Java 8 with CGLIB 3.1 but works with CGLIB 3.2
public class Range<T extends Number> implements Comparable<Range<T>> {
private final T minVal;
private final T maxVal;
public Range(T minVal, T maxVal) {
this.minVal = minVal;
this.maxVal = maxVal;
}
@Override
@siordache
siordache / GuiSpecification.groovy
Created December 3, 2015 01:52
TestFX 4.0.1-alpha with Spock - adapted from https://gist.github.com/hastebrot/8375513
package github.gist.testfx
import javafx.scene.Parent
import javafx.scene.Scene
import javafx.stage.Stage
import org.testfx.framework.junit.ApplicationTest
import spock.lang.Specification
abstract class GuiSpecification extends Specification {
ApplicationTest fx
@siordache
siordache / RegularPoly.java
Created January 22, 2016 16:07
Mocking method from Java abstract class
public abstract class RegularPoly {
public double getLength() {
return Math.abs(System.identityHashCode(this)) % 100;
}
public abstract double getArea();
}
@siordache
siordache / InMemoryProtocol.groovy
Last active June 2, 2016 20:21
URL protocol for in-memory resources
class InMemoryProtocol {
static final String PROTOCOL_NAME = 'inmemory'
static final Map inMemoryMap = [:]
static int inMemoryIndex = 0
static {
URL.URLStreamHandlerFactory = new URLStreamHandlerFactory() {
@Override URLStreamHandler createURLStreamHandler(String protocol) {
(protocol == PROTOCOL_NAME) ? new URLStreamHandler() {
@Override protected URLConnection openConnection(URL url) throws IOException {
@siordache
siordache / FeatureCombiner.java
Created October 13, 2016 20:50
Answer to http://stackoverflow.com/questions/40023754 using the Streamplify library
import javafx.scene.effect.BoxBlur;
import javafx.scene.effect.Effect;
import javafx.scene.effect.Shadow;
import javafx.scene.paint.Color;
import org.beryx.streamplify.product.CartesianProduct;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
@siordache
siordache / MultiCommit.java
Last active November 7, 2019 21:49
Committing multiple files with github-api (PR #361 - https://github.com/kohsuke/github-api/pull/361)
import org.apache.commons.io.IOUtils;
import org.kohsuke.github.*;
import java.net.URL;
public class MultiCommit {
public static void main(String[] args) throws Exception {
String userId = "your-user-id";
String password = "your-password";
String repoName = "your-repo-name";
package org.beryx.textio.demo;
import org.beryx.textio.TextIoFactory;
import org.beryx.textio.TextTerminal;
import java.util.Locale;
import java.util.Random;
public class TestFlickering {
private static final Random rnd = new Random();
@siordache
siordache / JooqUpsertV1.java
Last active April 30, 2018 18:41
Programmatically create updatable table with JOOQ (see https://groups.google.com/forum/#!topic/jooq-user/K1Fe3UIWLr8)
import org.hsqldb.Server;
import org.hsqldb.persist.HsqlProperties;
import org.jooq.*;
import java.nio.file.Files;
import java.nio.file.Path;
import static org.jooq.impl.DSL.*;
public class JooqUpsertV1 {