Provides integration testing capabilities for magnolia projects.
- Java 17
- Magnolia >= 6.3
- Add Maven dependency:
<dependency> <groupId>com.merkle.oss.magnolia</groupId> <artifactId>magnolia-testing</artifactId> <version>0.0.8</version> </dependency>
- Add a magnolia module descriptor in your
src/test/resources/META-INF/magnolia
directory (Bindings can differ from non-test setup).
Creates guice context and starts all magnolia modules. Can import jcr exports (xml files) into repository using @Repository annotation.
There's also a MagnoliaIntegrationBeforeAllTestExtension
which runs all tests in the same context (for better performance).
import static io.smallrye.common.constraint.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import info.magnolia.context.MgnlContext;
import info.magnolia.objectfactory.Components;
import info.magnolia.repository.RepositoryConstants;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import com.merkle.oss.magnolia.testing.MagnoliaIntegrationTestExtension;
import com.merkle.oss.magnolia.testing.repository.Repository;
@ExtendWith(MagnoliaIntegrationTestExtension.class)
class SampleIntegrationTest {
@Repository(workspaces = {@Repository.Workspace(name = RepositoryConstants.WEBSITE, xml = "jcr.xml")})
@Test
void someMethod() throws RepositoryException {
final Session session = MgnlContext.getJCRSession(RepositoryConstants.WEBSITE);
final Node node = session.getRootNode().getNode("0");
final SomeInterface someInterface = Components.getComponent(SomeInterface.class);
assertEquals("some value 42!", someInterface.someMethod(node));
session.logout();
}
}
Only creates guice context, but doesn't start magnolia modules. Can import jcr exports (xml files) into repository, create workspaces and import nodeTypes using @Repository annotation.
There's also a MagnoliaGuiceContextBeforeAllTestExtension
which runs all tests in the same context (for better performance).
import static org.junit.jupiter.api.Assertions.assertEquals;
import info.magnolia.context.MgnlContext;
import info.magnolia.objectfactory.Components;
import info.magnolia.repository.RepositoryConstants;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import com.merkle.oss.magnolia.testing.MagnoliaGuiceContextTestExtension;
import com.merkle.oss.magnolia.testing.repository.Repository;
@Repository(
nodeTypes = { @Repository.NodeTypesDefinition(cnd = "/mgnl-nodetypes/testing-nodetypes.cnd") },
workspaces = { @Repository.Workspace(name = "testing", xml = "jcr-custom-nodetype.xml", create = true) }
)
@ExtendWith(MagnoliaGuiceContextTestExtension.class)
class SampleGuiceContextTest {
@Test
void someMethod() throws RepositoryException {
final Session session = MgnlContext.getJCRSession("testing");
final Node node = session.getRootNode().getNode("0");
final SomeInterface someInterface = Components.getComponent(SomeInterface.class);
assertEquals("some value 42!", someInterface.someMethod(node));
}
}
Custom property files can be specified using the @TestConfiguration
annotation.
import com.merkle.oss.magnolia.testing.MagnoliaGuiceContextTestExtension;
import com.merkle.oss.magnolia.testing.configuration.TestConfiguration;
@TestConfiguration(magnoliaProperties = "/magnolia-testing.properties")
@ExtendWith(MagnoliaGuiceContextTestExtension.class)
class SampleGuiceContextTest {}
${resource.home}
is replaced by your projectssrc/test/resources
directory.- e.g.
magnolia.repositories.config=${resource.home}/repositories.xml
- e.g.
classpath:
is replaced by any classpath resource- e.g.
classpath:/repository/InMemoryJcrRepositoryConfiguration.xml
- e.g.
magnolia.app.rootdir
is replaced by a random generated temporary folder (for each test)- e.g.
magnolia.home=${magnolia.app.rootdir}
- e.g.
Test specific component bindings can be configured using the @TestConfiguration
annotation.
import org.junit.jupiter.api.extension.ExtendWith;
import com.merkle.oss.magnolia.testing.MagnoliaIntegrationTestExtension;
import com.merkle.oss.magnolia.testing.configuration.TestConfiguration;
@ExtendWith(MagnoliaIntegrationTestExtension.class)
class SampleGuiceContextTest {
@Test
@TestConfiguration(components = {
@TestConfiguration.Component(type = SomeInterface.class, implementation = SomeInterface.SomeOtherImplementation.class)
})
void someOtherMethod() {
...
}
}