camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christian Schneider <>
Subject Re: CamelTestSupport for junit 4 (re)starting context per class instead of per method
Date Sat, 06 Mar 2010 08:09:06 GMT
I have converted the camel-jetty test to a standard spring inegration 
test. Interestingly I did not need the Camel test classes (at least for 
this case).
The test already only starts the spring context once. I had to reset the 
mock endpoint but that was the only change I had to do to avoid side 
The source is attached below.

Any opinions?



Am 05.03.2010 11:52, schrieb Christian Schneider:
> Hi all,
> I am currently debugging a little in the tests for jetty because I got 
> an error in a test.
> While doing so I found out that the camel context or even the spring 
> context is (re)started for each test method. Wouldn´t it be much more 
> efficient to do this only per class?
> I guess the tests could take less then half the time after this 
> change. What do you think?
> Greetings
> Christian


Christian Schneider


  * 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
  * 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.apache.camel.component.jetty;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static;

import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.camel.EndpointInject;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.Produce;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.RuntimeCamelException;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.apache.camel.test.junit4.TestSupport;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

public class SpringHttpsRouteTest {
     private static final String NULL_VALUE_MARKER = CamelTestSupport.class.getCanonicalName();
     protected String expectedBody = "<hello>world!</hello>";
     protected String pwd = "changeit";
     protected Properties originalValues = new Properties();
     protected transient Log log = LogFactory.getLog(TestSupport.class);

     MockEndpoint mockEndpoint;

     private ProducerTemplate template;

     public void setUp() throws Exception {
         // ensure jsse clients can validate the self signed dummy localhost cert,
         // use the server keystore as the trust store for these tests
         URL trustStoreUrl = Thread.currentThread().getContextClassLoader().getResource("jsse/localhost.ks");
         setSystemProp("", trustStoreUrl.getPath());

     public void tearDown() throws Exception {

     private void setSystemProp(String key, String value) {
         String originalValue = System.setProperty(key, value);
         originalValues.put(key, originalValue != null ? originalValue : NULL_VALUE_MARKER);

     private void restoreSystemProperties() {
         for (Object key : originalValues.keySet()) {
             Object value = (String) originalValues.get(key);
             if (NULL_VALUE_MARKER.equals(value)) {
             } else {
                 System.setProperty((String) key, (String) value);

     public void testEndpoint() throws Exception {

         template.sendBodyAndHeader("https://localhost:9080/test", expectedBody, "Content-Type",

         List<Exchange>  list = mockEndpoint.getReceivedExchanges();
         Exchange exchange = list.get(0);
         TestSupport.assertNotNull("exchange", exchange);

         Message in = exchange.getIn();
         assertNotNull("in", in);

         Map<String, Object>  headers = in.getHeaders();
"Headers: " + headers);

         assertTrue("Should be more than one header but was: " + headers, headers.size()>

     public void testEndpointWithoutHttps() {
         try {
             template.sendBodyAndHeader("http://localhost:9080/test", expectedBody, "Content-Type",
             fail("expect exception on access to https endpoint via http");
         } catch (RuntimeCamelException expected) {
         assertTrue("mock endpoint was not called", mockEndpoint.getExchanges().isEmpty());


View raw message