Skip to content

Instantly share code, notes, and snippets.

@mike-neck
Last active December 10, 2015 06:38
Show Gist options
  • Save mike-neck/4395759 to your computer and use it in GitHub Desktop.
Save mike-neck/4395759 to your computer and use it in GitHub Desktop.
Spockの例外テストの書き方がなんとなくわかってきた
package org.mikeneck.twr.api;
import org.mikeneck.twr.exception.*;
/**
* @author : mike
* @since : 12/12/26
*/
public enum ExecutionPatterns {
ON_CONSTRUCTOR {
@Override
protected void work(Object by) throws ResourceException {
throw new ConstructorException(by);
}
@Override
public void construction(Object by) throws ConstructorException {
try {
this.work(by);
} catch (ResourceException e) {
throw ConstructorException.class.cast(e);
}
}
}, ON_OPEN {
@Override
protected void work(Object by) throws ResourceException {
throw new OpenException(by);
}
@Override
public void open(Object by) throws OpenException {
try {
this.work(by);
} catch (ResourceException e) {
throw OpenException.class.cast(e);
}
}
}, ON_EXECUTION {
@Override
protected void work(Object by) throws ResourceException {
throw new OperationalException(by);
}
@Override
public void execute(Object by) throws OperationalException {
try {
this.work(by);
} catch (ResourceException e) {
OperationalException.class.cast(e);
}
}
}, ON_CLOSE {
@Override
protected void work(Object by) throws ResourceException {
throw new CloseException(by);
}
@Override
public void close(Object by) throws CloseException {
try {
work(by);
} catch (ResourceException e) {
CloseException.class.cast(e);
}
}
};
public void construction(Object by) throws ConstructorException {}
public void open(Object by) throws OpenException {}
public void execute(Object by) throws OperationalException {}
public void close(Object by) throws CloseException {}
abstract protected void work (Object by) throws ResourceException;
}
package org.mikeneck.twr.api
import org.mikeneck.twr.api.util.MockOperator
import org.mikeneck.twr.exception.CloseException
import org.mikeneck.twr.exception.ConstructorException
import org.mikeneck.twr.exception.OpenException
import org.mikeneck.twr.exception.OperationalException
import spock.lang.Specification
import static org.mikeneck.twr.api.ExecutionPatterns.*
/**
* User: mike
* Date: 2012/12/28
*/
class ExecutionPatternsTestByGroovy extends Specification {
def "Exceptions thrown validly" () {
when :
pattern.work(new MockOperator(MockOperator, pattern))
then :
def e = thrown(exception)
assert e.class == exception
where :
pattern | exception
ON_CONSTRUCTOR | ConstructorException
ON_OPEN | OpenException
ON_EXECUTION | OperationalException
ON_CLOSE | CloseException
}
}
package org.mikeneck.twr.api
import org.mikeneck.twr.exception.CloseException
import org.mikeneck.twr.exception.ConstructorException
import org.mikeneck.twr.exception.OpenException
import org.mikeneck.twr.exception.OperationalException
import spock.lang.Specification
import static org.mikeneck.twr.api.ExecutionPatterns.*
/**
* User: mike
* Date: 2012/12/28
*/
class ExecutionPatternsTestByGroovy_OLD extends Specification {
def "Exceptions thrown validly" () {
setup :
def operator = new MockOperator(MockOperator, pattern)
def ex = new ResourceException()
try {
pattern.work(operator)
} catch (ResourceException e) {
ex = e
}
expect :
assert ex.class == exception
where :
pattern | exception
ON_CONSTRUCTOR | ConstructorException
ON_OPEN | OpenException
ON_EXECUTION | OperationalException
ON_CLOSE | CloseException
}
}
package org.mikeneck.twr.api;
import org.junit.experimental.theories.DataPoints;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;
import org.mikeneck.twr.api.util.MockOperator;
import org.mikeneck.twr.exception.*;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
/**
* @author mike
*/
@RunWith(Theories.class)
public class ExecutionPatternsTestByJava {
@DataPoints
public static Regulations[] regulations = Regulations.values();
@Theory
public void exceptionThrownValidly (Regulations regulation) {
ExecutionPatterns ptn = regulation.executePattern();
Operator operator = new MockOperator(MockOperator.class, ptn);
ResourceException exception = new ExceptionNotHappened(operator);
try {
ptn.work(operator);
} catch (ResourceException e) {
exception = e;
}
Class<? extends ResourceException> expected = regulation.expected();
assertThat(
ptn + "#work should throws " + expected,
exception, is(instanceOf(expected)));
}
public static class ExceptionNotHappened extends ResourceException {
public ExceptionNotHappened(Object o) {
super(o);
}
}
private static enum Regulations {
CONSTRUCTOR {
@Override
Class<? extends ResourceException> expected() {
return ConstructorException.class;
}
@Override
ExecutionPatterns executePattern() {
return ExecutionPatterns.ON_CONSTRUCTOR;
}
}, OPEN {
@Override
Class<? extends ResourceException> expected() {
return OpenException.class;
}
@Override
ExecutionPatterns executePattern() {
return ExecutionPatterns.ON_OPEN;
}
}, OPERATION {
@Override
Class<? extends ResourceException> expected() {
return OperationalException.class;
}
@Override
ExecutionPatterns executePattern() {
return ExecutionPatterns.ON_EXECUTION;
}
}, CLOSE {
@Override
Class<? extends ResourceException> expected() {
return CloseException.class;
}
@Override
ExecutionPatterns executePattern() {
return ExecutionPatterns.ON_CLOSE;
}
};
abstract Class <? extends ResourceException> expected ();
abstract ExecutionPatterns executePattern();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment