简体   繁体   中英

How to test Camel using ScalaTest

I want to unit test Camel code using ScalaTest and the FunSpec test style.

To do so, I need to extend from both FunSpec and CamelTestSupport. However, both these are classes and at least one needs to be a trait in order to do this in Scala. For example, this does not work:

class MySpec extends FunSpec with CamelTestSupport {}

Note: It appears that many online references to FunSpec suggest it is a trait, but it is a class in scalatest_2.11-3.0.0-M15.

How can I use ScalaTest FunSpec to test Camel?

The same test written using JUnit would look as follows:

public class DataLakeEventListenerRouteIT extends CamelTestSupport {
    @Autowired
    private MyRouteBuilder myRouteBuilder;
    private MockEndpoint myEndpointMock;

    @Override
    public boolean isUseAdviceWith() {
        return true;
    }

    @Override
    protected JndiRegistry createRegistry() throws Exception {
        JndiRegistry registry = super.createRegistry();
        //do some required bindings here
        return registry;
    }

    @Before
    public void startup() throws Exception {    
        AdviceWithRouteBuilder mock = new AdviceWithRouteBuilder() {
            public void configure() throws Exception {
                mockEndpointsAndSkip(myRouteBuilder.MY_ROUTE_URI);
            }
        };

        context.addRoutes(myRouteBuilder);
        context.getRouteDefinition(myRouteBuilder.MY_ROUTE_ID)
                .adviceWith(context, mock);

        myEndpointMock = getMockEndpoint(
                "mock:" + myRouteBuilder.MY_ROUTE_URI);
    }

    @Test
    public void timerRouteShouldSendMessage() throws Exception {
        // Arrange
        context.start();

        myEndpointMock.expectedMessageCount(1);
        myEndpointMock.assertIsSatisfied();

        context.stop();
    }
}

Well, it turns out to straightforward. ScalaTest offers a trait equivalent of FunSpec called FunSpecLike. Declare the test like so:

class StoreSVSFileRouteSpec extends CamelTestSupport with FunSpecLike  {}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM