/*
* Copyright 2011 Daniel Josefsson
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package se.dannej.fakehttpserver.junit;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import org.hamcrest.Description;
import org.jmock.Expectations;
import org.jmock.integration.junit4.JUnitRuleMockery;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;
import se.dannej.fakehttpserver.FakeServer;
public class FakeServerRuleTest {
private static final FrameworkMethod ANY_METHOD = new FrameworkMethod(null);
private static final Object ANY_TARGET = new Object();
@Rule public final JUnitRuleMockery context = new JUnitRuleMockery();
private final FakeServer server = context.mock(FakeServer.class);
private FakeServerRule rule = new FakeServerRule();
@Test
public void runsTestsStatement() throws Throwable {
IntrospectingStatement base = new IntrospectingStatement();
Statement finalStatement = rule.apply(base, ANY_METHOD, ANY_TARGET);
finalStatement.evaluate();
assertThat(base.invoked(), is(true));
}
@Test
public void shutsdownServersAfterRunningTest() throws Throwable {
Statement finalStatement = rule.apply(anyStatement(), ANY_METHOD, testClassWith(server));
context.checking(new Expectations() {{
allowing(server).isSatisfied(with(any(Description.class)));
will(returnValue(true));
oneOf(server).stop();
}});
finalStatement.evaluate();
}
@Test
public void assertsServersAfterRunningTest() throws Throwable {
Statement finalStatement = rule.apply(anyStatement(), ANY_METHOD, testClassWith(server));
context.checking(new Expectations() {{
oneOf(server).isSatisfied(with(any(Description.class)));
will(returnValue(true));
allowing(server).stop();
}});
finalStatement.evaluate();
}
@Test
public void shutsdownServersAfterFailingTest() throws Throwable {
Statement finalStatement = rule.apply(new Statement() {
@Override
public void evaluate() throws Throwable {
fail("Failing test");
}
}, ANY_METHOD, testClassWith(server));
context.checking(new Expectations() {{
allowing(server).isSatisfied(with(any(Description.class)));
will(returnValue(true));
oneOf(server).stop();
}});
try {
finalStatement.evaluate();
fail("Expected assertion error");
} catch (AssertionError e) { }
}
@Test
public void shutsdownServersAfterExceptionThrowingTest() throws Throwable {
Statement finalStatement = rule.apply(new Statement() {
@Override
public void evaluate() throws Throwable {
throw new RuntimeException("Test throwing exception");
}
}, ANY_METHOD, testClassWith(server));
context.checking(new Expectations() {{
allowing(server).isSatisfied(with(any(Description.class)));
will(returnValue(true));
oneOf(server).stop();
}});
try {
finalStatement.evaluate();
fail("Expected exception");
} catch (RuntimeException e) { }
}
private Statement anyStatement() {
return new Statement() {
@Override
public void evaluate() throws Throwable { }
};
}
private Object testClassWith(final FakeServer server) {
return new TestClass(server);
}
private static class TestClass {
@SuppressWarnings("unused")
private FakeServer fakeServer;
public TestClass(FakeServer fakeServer) {
this.fakeServer = fakeServer;
}
}
private static class IntrospectingStatement extends Statement {
private boolean baseStatementInvoked;
@Override
public void evaluate() throws Throwable {
baseStatementInvoked = true;
}
public boolean invoked() {
return baseStatementInvoked;
}
}
}