18
How to automatically unit and integration test your Digital Factory modules #jahiaone Benjamin Papež, QA Architect © 2002 - 2015 Jahia Solutions Group SA

JahiaOne 2015 - How to automatically unit and integration test your Digital Factory modules

Embed Size (px)

Citation preview

How to

automatically unit and integration test

your Digital Factory modules#jahiaone

Benjamin Papež, QA Architect

© 2002 - 2015 Jahia Solutions Group SA

Motivation to automate

Test automation saves time and money

Fast feedback on continuous integration

Repetitive manual regression tests are boring and make testers lose focus

Easier to reproduce certain bugs

Allows for faster upgrades or refactoring

© 2002 - 2015 Jahia Solutions Group SA

Unit tests

Narrow in scope

No dependencies on

code outside the tested

unit (use mocks/stubs)

Runs very fast

Easy to start in

development environment

Integration tests

Test how parts of the system work together

Easier to write (dependent services are available)

Run slower

Usually need some setup, deployment, configuration

© 2002 - 2015 Jahia Solutions Group SA

Unit versus integration tests

Digital factory automated

core tests overview78 unit tests (2 minutes)

1712 integration tests (50 minutes)

209 functional tests - Selenium (11 hours, however together with setup/deployment its 23 hours)

Automated performance test (3 hours)

Sonar static source code analysis (25 minutes)

© 2002 - 2015 Jahia Solutions Group SA

Unit tests

Developers traditionally

develop integration tests

Tested classes very often

have dependencies (JCR,

other services, Spring)

Mocking/stubbing takes

time

Integration tests

Not part of the build

They run for a long time

and it takes time to setup

the environment, so

developers are not

running them locally

© 2002 - 2015 Jahia Solutions Group SA

Where are the problems

Integration tests with Springspring-test module allows for integration testing

without deployment to application server

Spring application context is available

Database and JCR access is available

Its slower than unit tests, but much faster than with starting up a remote application server and deploying to it

With automated configuration tests are run directly on compiling modules or within IDE like unit tests

© 2002 - 2015 Jahia Solutions Group SA

module-test-framework

(from 7.1 onwards) Copies temporary files needed to startup the repository

Overrides and starts the Jahia Spring application context with main core services started and unimportant ones mocked

Runs all Quartz schedulers as RAM schedulers

Registers nodetypes, rules and import XML files of the current module (you can have additional such files for test)

Ability to deploy a site (however without templates)

No OSGI, no JSP / UI testing, no access to other modules orits content (e.g. systemsite)

© 2002 - 2015 Jahia Solutions Group SA

module-test-framework

(from 7.1 onwards)

To use that framework define a test

dependency in your module‘s pom.xml

© 2002 - 2015 Jahia Solutions Group SA

<dependencies>

<dependency>

<groupId>org.jahia.test</groupId>

<artifactId>module-test-framework</artifactId>

<version>7.1.0.0</version>

<scope>test</scope>

</dependency>

</dependencies>

How to write tests (1)

TestNG:

© 2002 - 2015 Jahia Solutions Group SA

import org.jahia.test.framework.AbstractTestNGTest;import org.jahia.test.utils.TestHelper;…public class FacetedQueryTest extends AbstractTestNGTest {@BeforeClasspublic void oneTimeSetUp() throws Exception {

JahiaSite site = TestHelper.createSite(TESTSITE_NAME);…

}

@AfterClasspublic void oneTimeTearDown() throws Exception {

TestHelper.deleteSite(TESTSITE_NAME);}

@Testpublic void testSimpleFacets() throws Exception {

JCRSessionWrapper session = JCRSessionFactory.getInstance().getCurrentUserSession(Constants.EDIT_WORKSPACE,Locale.ENGLISH);

How to write tests (2)

JUnit: instead of static @BeforeClass, @AfterClass use:

© 2002 - 2015 Jahia Solutions Group SA

import org.jahia.test.framework.AbstractJUnitTest;import org.jahia.test.utils.TestHelper;…public class FacetedQueryJUnitTest extends AbstractJUnitTest {

@Overridepublic void beforeClassSetup() throws Exception {

super.beforeClassSetup();JahiaSite site = TestHelper.createSite(TESTSITE_NAME);…

}

@Overridepublic void afterClassSetup() throws Exception {

super.afterClassSetup();TestHelper.deleteSite(TESTSITE_NAME);

}

@Testpublic void testSimpleFacets() throws Exception {

JCRSessionWrapper session = JCRSessionFactory.getInstance().getCurrentUserSession(Constants.EDIT_WORKSPACE, Locale.ENGLISH);

How to execute tests (1)

Unit tests or integration tests with Spring:

Maven:

unit-tests profile is defined in jahia-parent pom.xml

Eclipse/IDEA - directly run or debug test class

with Junit/TestNG plugin

© 2002 - 2015 Jahia Solutions Group SA

your-module-path> mvn install -P unit-tests

How to execute tests (2)

Integration-tests running on remote

application server

Deploy JAR with test module (jahia-test-module)

Test classes need to be configured in Spring

© 2002 - 2015 Jahia Solutions Group SA

<bean class="org.jahia.test.bin.TestBean"><property name="priority" value="56"/><property name="testCases">

<list><value>org.jahia.modules.external.test.db.ExternalDatabaseProviderTest</value>

</list></property>

</bean>

How to execute tests (3)

Maven (see http://subversion.jahia.org/svn/jahia/trunk/README)All tests:

Single tests:

plugin uses url set in <jahia.test.url> parameter in your settings.xml file

or test servlet URL

© 2002 - 2015 Jahia Solutions Group SA

mvn jahia:test surefire-report:report-only

mvn -Dtest=org.jahia.services.stuff.MyTest jahia:test surefire-report:report-only

http://<server:port>/<context>/cms/test/

http://<server:port>/<context>/cms/test/org.jahia.services.stuff.MyTest

Examples Unit-tests with Mockito:

checkout jcrestapi modulehttps://github.com/Jahia/jcrestapi.git

src/test/org/jahia/modules/jcrestapi/accessors/

checkout taglib modulehttp://subversion.jahia.org/svn/jahia/trunk/taglib/

src/test/org/jahia/taglibs/jcr/node/NodeComparatorTest.java

Integration-test with Spring: checkout facets module

https://github.com/Jahia/facets.gitsrc/test/org/jahia/test/services/query/FacetedQueryTest.java

checkout core modulehttp://subversion.jahia.org/svn/jahia/trunk/core/src/test/org/jahia/services/tags/TaggingTest.java

© 2002 - 2015 Jahia Solutions Group SA

What lies ahead?

Creating/converting more tests to be run during compilation

Enhancing the test framework

Provide different Spring context profiles

Evaluating Spring boot (Spring v4)

Providing reusable mocks and stubs if requested

© 2002 - 2015 Jahia Solutions Group SA

Conclusion Start automating tests, either as

Unit tests (with mocking/stubbing all dependencies)

Integration tests with Spring

Integration tests running on remote server

Functional tests with Selenium(attend JahiaOne presentation tomorrow)

If you detect a Digital factory bug through your automatedtest, send it to us It will help us recreate and fix the bug

Test will be included in our build process to ensure it will not occur again

© 2002 - 2015 Jahia Solutions Group SA

Questions

???© 2002 - 2015 Jahia Solutions Group SA

Testing will eradicate bugs!

Thank you for your attention

#jahiaone

© 2002 - 2014 Jahia Solutions Group SA