Test JUnit5
Since Camel 3.0
The camel-test-junit5
module is used for unit testing Camel.
The class org.apache.camel.test.junit5.CamelTestSupport
provides a base JUnit class which you would extend
and implement your Camel unit test.
Simple unit test example
As shown below is a basic junit test which uses camel-test-junit5
. The createRouteBuilder
method is used
for build the routes to be tested. Then the methods with @Test
annotations are JUnit test methods which
will be executed. The base class CamelTestSupport
has a number of helper methods to configure testing,
see more at the javadoc of this class.
import org.apache.camel.RoutesBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit5.CamelTestSupport;
import org.junit.jupiter.api.Test;
public class SimpleMockTest extends CamelTestSupport {
@Test
public void testMock() throws Exception {
getMockEndpoint("mock:result").expectedBodiesReceived("Hello World");
template.sendBody("direct:start", "Hello World");
MockEndpoint.assertIsSatisfied(context);
}
@Override
protected RoutesBuilder createRouteBuilder() {
return new RouteBuilder() {
@Override
public void configure() {
from("direct:start").to("mock:result");
}
};
}
}
Migrating Camel Tests from JUnit 4 to JUnit 5
Find below some hints to help in migrating camel tests from JUnit 4 to JUnit 5.
Projects using camel-test
would need to use camel-test-junit5
. For instance, maven users would update their pom.xml
file as below:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test-junit5</artifactId>
<scope>test</scope>
</dependency>
It’s possible to run JUnit4 & JUnit5 based Camel tests side by side including the following dependencies camel-test ,
camel-test-junit5 and junit-vintage-engine . This configuration allows migrating Camel tests one by one.
|
Migration Steps
-
Imports of
org.apache.camel.test.junit4.*
should be replaced withorg.apache.camel.test.junit5.*
-
TestSupport
static methods should be imported where needed, for instanceimport static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf
-
Usage of the field
CamelTestSupport.log
should be replaced by another logger, for instanceorg.slf4j.LoggerFactory.getLogger(MyCamelTest.class);
-
Usage of the method
CamelTestSupport.createRegistry
should be replaced byCamelTestSupport.createCamelRegistry()
-
Overrides of
isCreateCamelContextPerClass()
returningfalse
should be removed -
Overrides of
isCreateCamelContextPerClass()
returningtrue
should be replaced by@TestInstance(Lifecycle.PER_CLASS)
-
Usage of the method
CamelTestSupport.assertMockEndpointsSatisfied
should be replaced byMockEndpoint.assertIsSatisfied(context)
Once Camel related steps have been performed, there are still typical JUnit 5 migration steps to remember:
-
New JUnit 5 assertions should be imported where needed, for instance
import static org.junit.jupiter.api.Assertions.assertEquals
-
Assertion messages should be moved to the last parameter where needed, for instance
assertEquals("message", 2, 1)
becomesassertEquals(2, 1, "message")
-
org.junit.Test
should be changed in favor oforg.junit.jupiter.api.Test
-
org.junit.Ignore
should be changed in favor oforg.junit.jupiter.api.Disabled
-
org.junit.Before
should be changed in favor oforg.junit.jupiter.api.BeforeEach
-
org.junit.After
should be changed in favor oforg.junit.jupiter.api.AfterEach
-
org.junit.BeforeClass
should be changed in favor ofimport org.junit.jupiter.api.BeforeAll
-
org.junit.AfterClass
should be changed in favor ofimport org.junit.jupiter.api.AfterAll
-
Built-in
assertThat
from third-party assertion libraries should be used. For instance, useorg.hamcrest.MatcherAssert.assertThat
fromjava-hamcrest
Depending on the context, more involved tips might be needed, please check the JUnit 5 User Guide.