Git Product home page Git Product logo

sling-org-apache-sling-testing-rules's Introduction

Apache Sling

Build Status Test Status Coverage Sonarcloud Status JavaDoc Maven Central testing License

Apache Sling Testing Rules

This module is part of the Apache Sling project.

The Sling Http Testing Rules allow writing integration tests easily. They are primarily meant to be used for tests that use http against a Sling instance and make use of the org.apache.sling.testing.clients which offer a simple, immutable and extendable way of working with specialized http clients.

The junit rules incorporate boiler-plate logic that is shared in tests and take the modern approach of using junit rules rather than inheritance. The SlingRule (for methods) or SlingClassRule are base rules, chaining other rules like TestTimeoutRule, TestDescriptionRule, FilterRule. The SlingInstanceRule extends that and starts a Sling instance if needed and also allows instantiating a SlingClient pointing to the instance - base url, credentials, etc.

Starting an Integration Test

Starting an integration is very simple out of the box, but is very extendable, both by combining or configuring the junit rules and by using the versatile SlingClient (which can be extended or adapted by calling adaptTo(MyClient.class) without losing the client configuration)

Simple Example using SlingInstanceRule

    public class MySimpleIT {
    
        @ClassRule
        public static SlingInstanceRule instanceRule = new SlingInstanceRule();
    
        @Rule
        public SlingRule methodRule = new SlingRule(); // will configure test timeout, description, etc.
    
        @Test
        public void testChangeOSGiConfig() {
           SlingClient client = instanceRule.getAdminClient();
           client.createNode("/content/myNode", "nt:unstructured");
           Assert.assertTrue("Node should be there", client.exists("/content/myNode"));
        }
            
    } 

Example using SlingInstanceRule and the clients

    public class MyOSGiIT {
        @ClassRule
        public static SlingInstanceRule instanceRule = new SlingInstanceRule();
    
        @Rule
        public SlingRule methodRule = new SlingRule(); // will configure test timeout, description, etc.
    
        @Test
        public void testChangeOSGiConfig() {
           OsgiConsoleClient osgiClient = instanceRule.getAdminClient(OsgiConsoleClient.class);
           // Save osgi config for pid (to be restored later) 
           InstanceConfig osgiConfig = new OsgiInstanceConfig(osgiClient, "MYPID").save();
           // edit the config for this test
           osgiClient.editConfigurationWithWait(20, "MYPID", null, myMap);
           SlingHttpResponse response = osgiClient.adaptTo(MyClient.class).myClientMethod();
           response.checkContentContains("my expected content");
           osgiConfig.restore();
        }
            
    } 

sling-org-apache-sling-testing-rules's People

Contributors

bdelacretaz avatar chetanmeh avatar dulvac avatar enapps-enorman avatar klcodanr avatar oliverlietz avatar raducotescu avatar rombert avatar volteanu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.