Author: dvaleri Date: Thu Mar 1 19:07:13 2012 New Revision: 1295748 URL: http://svn.apache.org/viewvc?rev=1295748&view=rev Log: [CAMEL-4987] Updated test for use as a documentation example.
Modified: camel/trunk/components/camel-test-spring/src/test/java/org/apache/camel/test/junit4/CamelSpringJUnit4ClassRunnerPlainTest.java camel/trunk/components/camel-test-spring/src/test/java/org/apache/camel/test/junit4/CamelSpringJUnit4ClassRunnerUseAdviceWithTest.java Modified: camel/trunk/components/camel-test-spring/src/test/java/org/apache/camel/test/junit4/CamelSpringJUnit4ClassRunnerPlainTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-test-spring/src/test/java/org/apache/camel/test/junit4/CamelSpringJUnit4ClassRunnerPlainTest.java?rev=1295748&r1=1295747&r2=1295748&view=diff ============================================================================== --- camel/trunk/components/camel-test-spring/src/test/java/org/apache/camel/test/junit4/CamelSpringJUnit4ClassRunnerPlainTest.java (original) +++ camel/trunk/components/camel-test-spring/src/test/java/org/apache/camel/test/junit4/CamelSpringJUnit4ClassRunnerPlainTest.java Thu Mar 1 19:07:13 2012 @@ -28,12 +28,12 @@ import org.apache.camel.management.Defau import org.apache.camel.test.spring.StopWatchTestExecutionListener; import org.apache.camel.util.StopWatch; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.annotation.DirtiesContext.ClassMode; import org.springframework.test.context.ContextConfiguration; import static org.junit.Assert.assertEquals; @@ -41,11 +41,13 @@ import static org.junit.Assert.assertNot import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +// START SNIPPET: e1 @RunWith(CamelSpringJUnit4ClassRunner.class) @ContextConfiguration -// Put here to prevent Spring context caching across tests since some tests inherit from this test and -// therefore use the same Spring context. -@DirtiesContext +// Put here to prevent Spring context caching across tests and test methods since some tests inherit +// from this test and therefore use the same Spring context. Also because we want to reset the +// Camel context and mock endpoints between test methods automatically. +@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) public class CamelSpringJUnit4ClassRunnerPlainTest { @Autowired @@ -69,17 +71,6 @@ public class CamelSpringJUnit4ClassRunne @Produce(uri = "direct:start2", context = "camelContext2") protected ProducerTemplate start2; - /** - * Multiple test methods operate on the same mock endpoint instances since the - * test methods do not use {@link DirtiesContext}. Reset them - * between tests. - */ - @Before - public void resetMocks() { - MockEndpoint.resetMocks(camelContext); - MockEndpoint.resetMocks(camelContext2); - } - @Test public void testPositive() throws Exception { assertEquals(ServiceStatus.Started, camelContext.getStatus()); @@ -132,3 +123,4 @@ public class CamelSpringJUnit4ClassRunne assertTrue(camelContext2.isLazyLoadTypeConverters()); } } +// END SNIPPET: e1 Modified: camel/trunk/components/camel-test-spring/src/test/java/org/apache/camel/test/junit4/CamelSpringJUnit4ClassRunnerUseAdviceWithTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-test-spring/src/test/java/org/apache/camel/test/junit4/CamelSpringJUnit4ClassRunnerUseAdviceWithTest.java?rev=1295748&r1=1295747&r2=1295748&view=diff ============================================================================== --- camel/trunk/components/camel-test-spring/src/test/java/org/apache/camel/test/junit4/CamelSpringJUnit4ClassRunnerUseAdviceWithTest.java (original) +++ camel/trunk/components/camel-test-spring/src/test/java/org/apache/camel/test/junit4/CamelSpringJUnit4ClassRunnerUseAdviceWithTest.java Thu Mar 1 19:07:13 2012 @@ -17,32 +17,35 @@ package org.apache.camel.test.junit4; import org.apache.camel.ServiceStatus; +import org.apache.camel.test.spring.StopWatchTestExecutionListener; import org.apache.camel.test.spring.UseAdviceWith; +import org.apache.camel.util.StopWatch; import org.junit.Before; -import org.junit.BeforeClass; +import org.junit.Test; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; @UseAdviceWith public class CamelSpringJUnit4ClassRunnerUseAdviceWithTest extends CamelSpringJUnit4ClassRunnerPlainTest { - - protected static boolean checked; - - @BeforeClass - public static void setup() { - checked = false; - } @Before public void testContextStarted() throws Exception { - if (!checked) { - assertEquals(ServiceStatus.Stopped, camelContext.getStatus()); - camelContext.start(); - camelContext2.start(); - - Thread.sleep(2000); - - checked = true; - } + + assertEquals(ServiceStatus.Stopped, camelContext.getStatus()); + assertEquals(ServiceStatus.Stopped, camelContext2.getStatus()); + camelContext.start(); + camelContext2.start(); + + Thread.sleep(4000); + } + + @Test + public void testStopwatch() { + StopWatch stopWatch = StopWatchTestExecutionListener.getStopWatch(); + + assertNotNull(stopWatch); + assertTrue(stopWatch.taken() < 4100); } }