Search code examples
apache-camelcucumber-jvm

Camel blueprint testing and cucumber


Is it possible to combine cucumber with CamelBlueprintTestSupport? I have my runner class:

@RunWith(Cucumber.class)
@CucumberOptions(monochrome=true,
        format={ "pretty", "html:target/cucumber"}, 
        features = "C:/Users/Developer/workspace_camel/SRV002_PatronInformation/src/test/resources/cucumber/asynchronousErrorHandling.feature")


    public class RunFeature_SRV002_PatronInformationTest  {

    }

and my blueprint test class with the scenarios:

public class SRV002_PatronInformationScenarioTest extends CamelBlueprintTestSupport {

        @Override
        protected String getBlueprintDescriptor() {
            return "/OSGI-INF/blueprint/blueprint.xml";
        }



        @Given("^client communicates asynchronous via socket$")
        public void client_communicates_asynchronous_via_socket() throws Throwable {
        System.out.println("test");

        }

        @When("^client posts message$")
        public void an_error_occurs_inside_the_integration() throws Throwable {
            String endpoint = "netty4:tcp://localhost:5000?sync=false&textline=true";
            template.sendBody(endpoint, "test");


        }

        @Then("^the integration should not return response to the client$")
        public void the_integration_should_not_return_the_error_to_the_client() throws Throwable {
            System.out.println("test");
        }

    }

The problem now is that, when I run this I run into nullpointerexception at template.sendbody because the context, bundle and routes haven't started. For some reason it seems adding @RunWith(Cucumber) prevents the camel routes from starting.

Anyone knows how this can be solved? Thanks Souciance


Solution

  • Ok so I managed to solve this. For reference look here: http://camel.465427.n5.nabble.com/How-to-test-routes-when-using-another-TestRunner-td5772687.html

    Thanks to Gregor Lenz for the help.

    Essentially the key here is that in your Camel BlueprintTestSupport class, inside the test method, that starts the given scenario you need to add this.setUp(). See the code below:

    In Cucumber

    SRVXXX_FileTransferCamelRunner filetransfer = new SRVXXX_FileTransferCamelRunner(); 
    
    @Given("^an input file$")
        public void an_input_file() throws Throwable {
            endpoint.append("file:C:/Camel/input?fileName=input.txt");          
        }
        @When("^client puts the file in the input directory$")
        public void client_puts_the_file_in_the_input_directory() throws Throwable {
            filetransfer.testPutFile(fileData.toString(), endpoint.toString());     
        }
    
    @Then("^the integration should move the file to the output directory$")
    public void the_integration_should_move_the_file_to_the_output_directory() throws Throwable {
        String outputPath = "C:/Camel/output/input.txt";
        filetransfer.testFileHasMoved(outputPath);
    }
    

    In Camel

    @Test
        public void testPutFile(String body, String endpoint) throws Exception {
            this.setUp();
            template.sendBody(endpoint,body);   
            Thread.sleep(2000);
            assertFileNotExists(endpoint);
        }