Browse Source
git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@1077 50f2f4bb-b051-0410-bef5-90022cba6387pull/1/head
6 changed files with 454 additions and 94 deletions
@ -0,0 +1,87 @@
@@ -0,0 +1,87 @@
|
||||
/* |
||||
* Copyright 2002-2009 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.ArrayList; |
||||
|
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
import org.junit.runner.notification.RunNotifier; |
||||
import org.junit.runners.JUnit4; |
||||
import org.springframework.test.annotation.ExpectedException; |
||||
import org.springframework.test.context.TestExecutionListeners; |
||||
|
||||
/** |
||||
* Verifies proper handling of the following in conjunction with the |
||||
* {@link SpringJUnit4ClassRunner}: |
||||
* <ul> |
||||
* <li>JUnit's {@link Test#expected() @Test(expected=...)}</li> |
||||
* <li>Spring's {@link ExpectedException @ExpectedException}</li> |
||||
* </ul> |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 3.0 |
||||
*/ |
||||
@RunWith(JUnit4.class) |
||||
public class ExpectedExceptionSpringRunnerTests { |
||||
|
||||
@Test |
||||
public void timedTests() throws Exception { |
||||
Class<ExpectedExceptionSpringRunnerTestCase> testClass = ExpectedExceptionSpringRunnerTestCase.class; |
||||
TrackingRunListener listener = new TrackingRunListener(); |
||||
RunNotifier notifier = new RunNotifier(); |
||||
notifier.addListener(listener); |
||||
|
||||
new SpringJUnit4ClassRunner(testClass).run(notifier); |
||||
assertEquals("Verifying number of failures for test class [" + testClass + "].", 1, |
||||
listener.getTestFailureCount()); |
||||
assertEquals("Verifying number of tests started for test class [" + testClass + "].", 3, |
||||
listener.getTestStartedCount()); |
||||
assertEquals("Verifying number of tests finished for test class [" + testClass + "].", 3, |
||||
listener.getTestFinishedCount()); |
||||
} |
||||
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class) |
||||
@TestExecutionListeners( {}) |
||||
public static final class ExpectedExceptionSpringRunnerTestCase { |
||||
|
||||
// Should Pass.
|
||||
@Test(expected = IndexOutOfBoundsException.class) |
||||
public void verifyJUnitExpectedException() { |
||||
new ArrayList<Object>().get(1); |
||||
} |
||||
|
||||
// Should Pass.
|
||||
@Test |
||||
@ExpectedException(IndexOutOfBoundsException.class) |
||||
public void verifySpringExpectedException() { |
||||
new ArrayList<Object>().get(1); |
||||
} |
||||
|
||||
// Should Fail due to duplicate configuration.
|
||||
@Test(expected = IllegalStateException.class) |
||||
@ExpectedException(IllegalStateException.class) |
||||
public void verifyJUnitAndSpringExpectedException() { |
||||
new ArrayList<Object>().get(1); |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,151 @@
@@ -0,0 +1,151 @@
|
||||
/* |
||||
* Copyright 2002-2009 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.io.IOException; |
||||
import java.util.Arrays; |
||||
import java.util.Collection; |
||||
import java.util.concurrent.atomic.AtomicInteger; |
||||
|
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
import org.junit.runner.notification.RunNotifier; |
||||
import org.junit.runners.Parameterized; |
||||
import org.junit.runners.Parameterized.Parameters; |
||||
import org.springframework.test.annotation.Repeat; |
||||
import org.springframework.test.annotation.Timed; |
||||
import org.springframework.test.context.ContextConfiguration; |
||||
import org.springframework.test.context.TestExecutionListeners; |
||||
|
||||
/** |
||||
* Verifies proper handling of the following in conjunction with the |
||||
* {@link SpringJUnit4ClassRunner}: |
||||
* <ul> |
||||
* <li>Spring's {@link Repeat @Repeat}</li> |
||||
* <li>Spring's {@link Timed @Timed}</li> |
||||
* </ul> |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 3.0 |
||||
*/ |
||||
@RunWith(Parameterized.class) |
||||
public class RepeatedSpringRunnerTests { |
||||
|
||||
private static final AtomicInteger invocationCount = new AtomicInteger(); |
||||
|
||||
private final Class<? extends AbstractRepeatedTestCase> testClass; |
||||
|
||||
private final int expectedFailureCount; |
||||
|
||||
private final int expectedTestStartedCount; |
||||
|
||||
private final int expectedTestFinishedCount; |
||||
|
||||
private final int expectedInvocationCount; |
||||
|
||||
|
||||
public RepeatedSpringRunnerTests(Class<? extends AbstractRepeatedTestCase> testClass, int expectedFailureCount, |
||||
int expectedTestStartedCount, int expectedTestFinishedCount, int expectedInvocationCount) { |
||||
this.testClass = testClass; |
||||
this.expectedFailureCount = expectedFailureCount; |
||||
this.expectedTestStartedCount = expectedTestStartedCount; |
||||
this.expectedTestFinishedCount = expectedTestFinishedCount; |
||||
this.expectedInvocationCount = expectedInvocationCount; |
||||
} |
||||
|
||||
@Parameters |
||||
public static Collection<Object[]> repetitionData() { |
||||
return Arrays.asList(new Object[][] {//
|
||||
//
|
||||
{ NonAnnotatedRepeatedTestCase.class, 0, 1, 1, 1 },//
|
||||
{ DefaultRepeatValueRepeatedTestCase.class, 0, 1, 1, 1 },//
|
||||
{ NegativeRepeatValueRepeatedTestCase.class, 0, 1, 1, 1 },//
|
||||
{ RepeatedFiveTimesRepeatedTestCase.class, 0, 1, 1, 5 } //
|
||||
}); |
||||
} |
||||
|
||||
@Test |
||||
public void assertRepetitions() throws Exception { |
||||
TrackingRunListener listener = new TrackingRunListener(); |
||||
RunNotifier notifier = new RunNotifier(); |
||||
notifier.addListener(listener); |
||||
invocationCount.set(0); |
||||
|
||||
new SpringJUnit4ClassRunner(this.testClass).run(notifier); |
||||
assertEquals("Verifying number of failures for test class [" + this.testClass + "].", |
||||
this.expectedFailureCount, listener.getTestFailureCount()); |
||||
assertEquals("Verifying number of tests started for test class [" + this.testClass + "].", |
||||
this.expectedTestStartedCount, listener.getTestStartedCount()); |
||||
assertEquals("Verifying number of tests finished for test class [" + this.testClass + "].", |
||||
this.expectedTestFinishedCount, listener.getTestFinishedCount()); |
||||
assertEquals("Verifying number of invocations for test class [" + this.testClass + "].", |
||||
this.expectedInvocationCount, invocationCount.get()); |
||||
} |
||||
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class) |
||||
@TestExecutionListeners( {}) |
||||
@ContextConfiguration(locations = {}) |
||||
public abstract static class AbstractRepeatedTestCase { |
||||
|
||||
protected void incrementInvocationCount() throws IOException { |
||||
invocationCount.incrementAndGet(); |
||||
} |
||||
} |
||||
|
||||
public static final class NonAnnotatedRepeatedTestCase extends AbstractRepeatedTestCase { |
||||
|
||||
@Test |
||||
@Timed(millis = 10000) |
||||
public void testNonAnnotated() throws Exception { |
||||
incrementInvocationCount(); |
||||
} |
||||
} |
||||
|
||||
public static final class DefaultRepeatValueRepeatedTestCase extends AbstractRepeatedTestCase { |
||||
|
||||
@Test |
||||
@Repeat |
||||
@Timed(millis = 10000) |
||||
public void testDefaultRepeatValue() throws Exception { |
||||
incrementInvocationCount(); |
||||
} |
||||
} |
||||
|
||||
public static final class NegativeRepeatValueRepeatedTestCase extends AbstractRepeatedTestCase { |
||||
|
||||
@Test |
||||
@Repeat(-5) |
||||
@Timed(millis = 10000) |
||||
public void testNegativeRepeatValue() throws Exception { |
||||
incrementInvocationCount(); |
||||
} |
||||
} |
||||
|
||||
public static final class RepeatedFiveTimesRepeatedTestCase extends AbstractRepeatedTestCase { |
||||
|
||||
@Test |
||||
@Repeat(5) |
||||
@Timed(millis = 10000) |
||||
public void testRepeatedFiveTimes() throws Exception { |
||||
incrementInvocationCount(); |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,97 @@
@@ -0,0 +1,97 @@
|
||||
/* |
||||
* Copyright 2002-2009 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 org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
import org.junit.runner.notification.RunNotifier; |
||||
import org.junit.runners.JUnit4; |
||||
import org.springframework.test.annotation.Timed; |
||||
import org.springframework.test.context.TestExecutionListeners; |
||||
|
||||
/** |
||||
* Verifies proper handling of the following in conjunction with the |
||||
* {@link SpringJUnit4ClassRunner}: |
||||
* <ul> |
||||
* <li>JUnit's {@link Test#timeout() @Test(timeout=...)}</li> |
||||
* <li>Spring's {@link Timed @Timed}</li> |
||||
* </ul> |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 3.0 |
||||
*/ |
||||
@RunWith(JUnit4.class) |
||||
public class TimedSpringRunnerTests { |
||||
|
||||
@Test |
||||
public void timedTests() throws Exception { |
||||
Class<TimedSpringRunnerTestCase> testClass = TimedSpringRunnerTestCase.class; |
||||
TrackingRunListener listener = new TrackingRunListener(); |
||||
RunNotifier notifier = new RunNotifier(); |
||||
notifier.addListener(listener); |
||||
|
||||
new SpringJUnit4ClassRunner(testClass).run(notifier); |
||||
assertEquals("Verifying number of failures for test class [" + testClass + "].", 3, |
||||
listener.getTestFailureCount()); |
||||
assertEquals("Verifying number of tests started for test class [" + testClass + "].", 5, |
||||
listener.getTestStartedCount()); |
||||
assertEquals("Verifying number of tests finished for test class [" + testClass + "].", 5, |
||||
listener.getTestFinishedCount()); |
||||
} |
||||
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class) |
||||
@TestExecutionListeners( {}) |
||||
public static final class TimedSpringRunnerTestCase { |
||||
|
||||
// Should Pass.
|
||||
@Test(timeout = 2000) |
||||
public void testJUnitTimeoutWithNoOp() { |
||||
/* no-op */ |
||||
} |
||||
|
||||
// Should Pass.
|
||||
@Test |
||||
@Timed(millis = 2000) |
||||
public void testSpringTimeoutWithNoOp() { |
||||
/* no-op */ |
||||
} |
||||
|
||||
// Should Fail due to timeout.
|
||||
@Test(timeout = 200) |
||||
public void testJUnitTimeoutWithOneSecondWait() throws Exception { |
||||
Thread.sleep(1000); |
||||
} |
||||
|
||||
// Should Fail due to timeout.
|
||||
@Test |
||||
@Timed(millis = 200) |
||||
public void testSpringTimeoutWithOneSecondWait() throws Exception { |
||||
Thread.sleep(1000); |
||||
} |
||||
|
||||
// Should Fail due to duplicate configuration.
|
||||
@Test(timeout = 200) |
||||
@Timed(millis = 200) |
||||
public void testSpringAndJUnitTimeout() { |
||||
/* no-op */ |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,68 @@
@@ -0,0 +1,68 @@
|
||||
/* |
||||
* Copyright 2002-2009 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 java.util.concurrent.atomic.AtomicInteger; |
||||
|
||||
import org.junit.runner.Description; |
||||
import org.junit.runner.notification.Failure; |
||||
import org.junit.runner.notification.RunListener; |
||||
|
||||
/** |
||||
* Simple {@link RunListener} which tracks how many times certain JUnit callback |
||||
* methods were called: only intended for the integration test suite. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 3.0 |
||||
*/ |
||||
public class TrackingRunListener extends RunListener { |
||||
|
||||
private final AtomicInteger testFailureCount = new AtomicInteger(); |
||||
|
||||
private final AtomicInteger testStartedCount = new AtomicInteger(); |
||||
|
||||
private final AtomicInteger testFinishedCount = new AtomicInteger(); |
||||
|
||||
|
||||
public int getTestFailureCount() { |
||||
return this.testFailureCount.get(); |
||||
} |
||||
|
||||
public int getTestStartedCount() { |
||||
return this.testStartedCount.get(); |
||||
} |
||||
|
||||
public int getTestFinishedCount() { |
||||
return this.testFinishedCount.get(); |
||||
} |
||||
|
||||
@Override |
||||
public void testFailure(Failure failure) throws Exception { |
||||
this.testFailureCount.incrementAndGet(); |
||||
} |
||||
|
||||
@Override |
||||
public void testStarted(Description description) throws Exception { |
||||
this.testStartedCount.incrementAndGet(); |
||||
} |
||||
|
||||
@Override |
||||
public void testFinished(Description description) throws Exception { |
||||
this.testFinishedCount.incrementAndGet(); |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue