Unit Testing
Unit Tests are used to test the smallest units of code. They use only a subset of IM features and are focused on testing the code of classes, beans, routes, mappers, etc. The test is executed fast and provides immediate feedback. The core class of a unit test is UnitTestSpecification
.
How to Test a Bean
In this particular scenario, our objective is to validate the correct initialization of a bean of the type list containing two values. We utilize seedBean
to specify the required bean and seedRoute
to define a route that incorporates this bean. During the test execution, we aim to invoke the route named test-bean
and verify that the output body contains an array with ['foo', 'bar'].
To achieve this, we employ the Camel feature AdviceWith.adviceWith
, which replaces <from uri="timer://foo?repeatCount=1"/>
with direct:start
. This replacement enables us to call the route using sendBody("direct:start")
. Through this test, we make sure that the bean is not only defined but also used correctly in the specified route.
class BeansTest extends UnitTestSpecification {
@Autowired
private CamelContext context
def "should test bean"() {
given:
seedBean('''<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<util:list id="myList" value-type="java.lang.String">
<value>foo</value>
<value>bar</value>
</util:list>
</beans>''')
seedRoute('''
<routes xmlns="http://camel.apache.org/schema/spring">
<route xmlns="http://camel.apache.org/schema/spring" id="test-bean">
<from uri="timer://foo?repeatCount=1"/>
<setBody>
<method ref="myList" method="toString"/>
</setBody>
</route>
</routes>
''')
AdviceWith.adviceWith(context, "test-bean", a ->
a.replaceFromWith("direct:start")
)
when:
Exchange exchange = sendBody("direct:start", [])
then:
exchange.getIn().getBody() == "[foo, bar]"
}
}
How to Test a Class
In this specific use case, we utilize seedClass
to define an ExampleProcess
class, which is employed by the route named test-class
and sets the body to "Changed body". Through this test, our aim is to validate that the ExampleProcess
class is accurately defined, added to the context as an exampleProcess
bean, and that the process
method functions as anticipated.
class ClassesTest extends UnitTestSpecification {
@Autowired
private CamelContext context
def "should test processor"() {
given:
seedClass("package repo.classes\n" +
"\n" +
"import org.apache.camel.Exchange\n" +
"import org.apache.camel.Processor\n" +
"\n" +
"class ExampleProcessor implements Processor {\n" +
"\n" +
" @Override\n" +
" void process(Exchange exchange) throws Exception {\n" +
" exchange.getIn().setBody(\"Changed body\")\n" +
" }\n" +
"}")
seedRoute('''
<routes xmlns="http://camel.apache.org/schema/spring">
<route xmlns="http://camel.apache.org/schema/spring" id="test-class">
<from uri="timer://foo?repeatCount=1"/>
<to uri="bean:exampleProcessor"/>
</route>
</routes>
''')
AdviceWith.adviceWith(context, "test-class", a ->
a.replaceFromWith("direct:start")
)
when:
Exchange exchange = sendBody("direct:start", [])
then:
exchange.getIn().getBody() == "Changed body"
}
}
How to Test a Mapper
In this particular use case, we employ seedMapper
to define a test mapper utilized by the test-mapper
route. This mapper converts [["test":"Jerry", "age": "42"]] to [["convertedAge": 42, "attribute1": "Jerry"]]. The objective of this test is to ensure the correct definition of the mapper, validate the application of stringToInteger
, and confirm the renaming of the attribute "test" to "attribute1".
class MapperTest extends UnitTestSpecification {
@Autowired
private CamelContext context
def "should test mapper"() {
given:
seedMapper('''<mappers>
<loadMapper id="test">
<body in="age" out="convertedAge" converter="stringToInteger"/>
<body in="test" out="attribute1"/>
</loadMapper>
</mappers>''')
seedRoute('''
<routes xmlns="http://camel.apache.org/schema/spring">
<route xmlns="http://camel.apache.org/schema/spring" id="test-mapper">
<from uri="timer://foo?repeatCount=1"/>
<to uri="pfx-model:transform?mapper=test"/>
</route>
</routes>
''')
AdviceWith.adviceWith(context, "test-mapper", a ->
a.replaceFromWith("direct:start")
)
when:
Exchange exchange = sendBody("direct:start", [["test":"Jerry", "age": "42"]])
then:
exchange.getIn().getBody() == [["convertedAge": 42, "attribute1": "Jerry"]]
}
}
Â
IntegrationManager version 5.8.0