2009/7/16 James Strachan <james.strac...@gmail.com>:
> 2009/7/16 Charles Moulliard <cmoulli...@gmail.com>:
>> Hi,
>>
>> The following test case created generates the error :
>> org.apache.camel.NoSuchBeanException caused by: No bean could be found in
>> the registry for: myBean
>>
>> /**
>>  * 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 org.apache.camel.component.bean;
>>
>> import javax.naming.Context;
>>
>> import org.apache.camel.CamelExecutionException;
>> import org.apache.camel.EndpointInject;
>> import org.apache.camel.Header;
>> import org.apache.camel.Produce;
>> import org.apache.camel.ProducerTemplate;
>> import org.apache.camel.builder.RouteBuilder;
>> import org.apache.camel.component.mock.MockEndpoint;
>> import org.apache.camel.spring.javaconfig.SingleRouteCamelConfiguration;
>> import org.apache.camel.util.jndi.JndiContext;
>> import org.apache.commons.logging.Log;
>> import org.apache.commons.logging.LogFactory;
>> import org.junit.Test;
>> import org.springframework.config.java.annotation.Bean;
>> import org.springframework.config.java.annotation.Configuration;
>> import org.springframework.config.java.test.JavaConfigContextLoader;
>> import org.springframework.test.annotation.DirtiesContext;
>> import org.springframework.test.context.ContextConfiguration;
>> import
>> org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
>> import org.springframework.util.Assert;
>>
>> import static org.junit.Assert.*;
>>
>> @ContextConfiguration(locations =
>> "org.apache.camel.component.bean.BeanWithExceptionTryCatchRouteTest$ContextConfig",
>> loader = JavaConfigContextLoader.class)
>> public class BeanWithExceptionTryCatchRouteTest extends
>> AbstractJUnit4SpringContextTests {
>>
>>    private static final transient Log LOG =
>> LogFactory.getLog(BeanWithExceptionTryCatchRouteTest.class);
>>
>>   �...@produce(uri = "direct:start")
>>    protected ProducerTemplate template;
>>
>>   �...@endpointinject(uri = "mock:result")
>>    private MockEndpoint resultEndpoint;
>>
>>   �...@endpointinject(uri = "mock:exception")
>>    private MockEndpoint exceptionEndpoint;
>>
>>   �...@endpointinject(uri = "mock:npe")
>>    private MockEndpoint npeEndpoint;
>>
>>   �...@dirtiescontext
>>   �...@test
>>    public void testHeaderNull() throws Exception {
>>
>>        try {
>>            template.sendBodyAndHeader("Hello", "user", null);
>>            fail("Should have thrown a NullPointerException");
>>        } catch (CamelExecutionException e) {
>>           Assert.isInstanceOf(NullPointerException.class, e.getCause());
>>        }
>>        npeEndpoint.assertIsSatisfied();
>>    }
>>
>>   �...@dirtiescontext
>>   �...@test
>>    public void testHeaderOtherException() throws Exception {
>>
>>        try {
>>            template.sendBodyAndHeader("Hello", "user", "boss");
>>            fail("Should have thrown an Exception");
>>        } catch (CamelExecutionException e) {
>>            Assert.isInstanceOf(Exception.class, e.getCause());
>>
>>        }
>>        exceptionEndpoint.assertIsSatisfied();
>>    }
>>
>>   �...@dirtiescontext
>>   �...@test
>>    public void testHeaderNotNull() throws Exception {
>>
>>        template.sendBodyAndHeader("Hello", "user", "charles");
>>        resultEndpoint.expectedBodiesReceived("Hello charles");
>>        resultEndpoint.assertIsSatisfied();
>>    }
>>
>>    protected Context createJndiContext() throws Exception {
>>        JndiContext answer = new JndiContext();
>>        answer.bind("myBean", new MyBean());
>>        return answer;
>>    }
>>
>>    public static class MyBean {
>>
>>        public String checkHeader(@Header(value = "user") String user)
>> throws Exception {
>>
>>            if (user == null) {
>>                throw new java.lang.NullPointerException("The user is null
>> !");
>>            }
>>
>>            if ( user.equals("boss") ) {
>>                throw new java.lang.Exception("This is the boss. Not allowed
>> to connect !");
>>            }
>>
>>            return "Hello " + user;
>>        }
>>
>>    }
>>
>>   �...@configuration
>>    public static class ContextConfig extends SingleRouteCamelConfiguration
>> {
>>
>>       �...@override
>>       �...@bean
>>        public RouteBuilder route() {
>>            return new RouteBuilder() {
>>               �...@override
>>                public void configure() {
>>                    from("direct:start").
>>                    doTry().
>>                        beanRef("myBean", "checkHeader").
>>                        to("mock:result").
>>                    doCatch(NullPointerException.class).
>>                        to("mock:npe").
>>                    doCatch(Exception.class).
>>
>> to("log:org.apache.camel.BEAN_TEST?showAll=true&multiline=true").
>>                        to("mock:exception").
>>                    end();
>>                }
>>            };
>>        }
>>
>>
>>    }
>> }
>>
>>
>> How can we register the bean in a junit test using Spring javaconfig ?
>
> This is really a Spring javaconfig question; check out the Spring docs...
> http://static.springsource.org/spring-javaconfig/docs/1.0.0.M4/reference/html/

in particular
http://static.springsource.org/spring-javaconfig/docs/1.0.0.M4/reference/html/ch02s02.html#declaring-a-bean


-- 
James
-------
http://macstrac.blogspot.com/

Open Source Integration
http://fusesource.com/

Reply via email to