|
Spring Framework example source code file (FailingBeforeAndAfterMethodsTests.java)
The Spring Framework FailingBeforeAndAfterMethodsTests.java source code
/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed 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 org.springframework.test.context.junit4;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.TestExecutionListener;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.support.AbstractTestExecutionListener;
import org.springframework.test.context.transaction.AfterTransaction;
import org.springframework.test.context.transaction.BeforeTransaction;
/**
* <p>
* JUnit 4 based unit test for verifying that '<em>before' and 'after'
* methods of {@link TestExecutionListener TestExecutionListeners} as well as
* {@link BeforeTransaction @BeforeTransaction} and
* {@link AfterTransaction @AfterTransaction} methods can fail a test in a JUnit
* 4.4 environment, as requested in <a
* href="http://opensource.atlassian.com/projects/spring/browse/SPR-3960"
* target="_blank">SPR-3960</a>.
* </p>
*
* @author Sam Brannen
* @since 2.5
*/
@RunWith(Parameterized.class)
public class FailingBeforeAndAfterMethodsTests {
protected final Class<?> clazz;
public FailingBeforeAndAfterMethodsTests(final Class<?> clazz) {
this.clazz = clazz;
}
@Parameters
public static Collection<Object[]> testData() {
return Arrays.asList(new Object[][] {
{ AlwaysFailingBeforeTestMethodTestCase.class },
{ AlwaysFailingAfterTestMethodTestCase.class },
{ FailingBeforeTransactionalTestCase.class },
{ FailingAfterTransactionalTestCase.class }
});
}
@Test
public void runTestAndAssertCounters() throws Exception {
final FailureTrackingRunListener failureTrackingRunListener = new FailureTrackingRunListener();
final RunNotifier notifier = new RunNotifier();
notifier.addListener(failureTrackingRunListener);
new SpringJUnit4ClassRunner(this.clazz).run(notifier);
assertEquals("Verifying number of failures for test class [" + this.clazz + "].", 1,
failureTrackingRunListener.failureCount);
}
static class FailureTrackingRunListener extends RunListener {
int failureCount = 0;
public void testFailure(Failure failure) throws Exception {
this.failureCount++;
}
}
static class AlwaysFailingBeforeTestMethodTestExecutionListener extends AbstractTestExecutionListener {
@Override
public void beforeTestMethod(TestContext testContext) {
org.junit.Assert.fail("always failing beforeTestMethod()");
}
}
static class AlwaysFailingAfterTestMethodTestExecutionListener extends AbstractTestExecutionListener {
@Override
public void afterTestMethod(TestContext testContext) {
org.junit.Assert.fail("always failing afterTestMethod()");
}
}
@TestExecutionListeners(value = { AlwaysFailingBeforeTestMethodTestExecutionListener.class }, inheritListeners = false)
public static class AlwaysFailingBeforeTestMethodTestCase extends AbstractJUnit4SpringContextTests {
@Test
public void testNothing() {
}
}
@TestExecutionListeners(value = { AlwaysFailingAfterTestMethodTestExecutionListener.class }, inheritListeners = false)
public static class AlwaysFailingAfterTestMethodTestCase extends AbstractJUnit4SpringContextTests {
@Test
public void testNothing() {
}
}
@ContextConfiguration(locations = { "FailingBeforeAndAfterMethodsTests-context.xml" })
public static class FailingBeforeTransactionalTestCase extends AbstractTransactionalJUnit4SpringContextTests {
@Test
public void testNothing() {
}
@BeforeTransaction
public void beforeTransaction() {
org.junit.Assert.fail("always failing beforeTransaction()");
}
}
@ContextConfiguration(locations = { "FailingBeforeAndAfterMethodsTests-context.xml" })
public static class FailingAfterTransactionalTestCase extends AbstractTransactionalJUnit4SpringContextTests {
@Test
public void testNothing() {
}
@AfterTransaction
public void afterTransaction() {
org.junit.Assert.fail("always failing afterTransaction()");
}
}
}
Other Spring Framework examples (source code examples)Here is a short list of links related to this Spring Framework FailingBeforeAndAfterMethodsTests.java source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.