You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lu...@apache.org on 2014/03/04 06:29:42 UTC

svn commit: r1573858 - in /myfaces/site/cms-site/trunk/content/wiki/core/user-guide/jsf-and-myfaces-howtos: ./ unit-testing/

Author: lu4242
Date: Tue Mar  4 05:29:41 2014
New Revision: 1573858

URL: http://svn.apache.org/r1573858
Log:
add myfaces-impl-test documentation

Added:
    myfaces/site/cms-site/trunk/content/wiki/core/user-guide/jsf-and-myfaces-howtos/unit-testing/   (with props)
    myfaces/site/cms-site/trunk/content/wiki/core/user-guide/jsf-and-myfaces-howtos/unit-testing.mdtext   (with props)
    myfaces/site/cms-site/trunk/content/wiki/core/user-guide/jsf-and-myfaces-howtos/unit-testing/create-test-from-base-class.mdtext   (with props)
    myfaces/site/cms-site/trunk/content/wiki/core/user-guide/jsf-and-myfaces-howtos/unit-testing/create-test-junit-runner.mdtext   (with props)
    myfaces/site/cms-site/trunk/content/wiki/core/user-guide/jsf-and-myfaces-howtos/unit-testing/setup-with-maven.mdtext   (with props)

Propchange: myfaces/site/cms-site/trunk/content/wiki/core/user-guide/jsf-and-myfaces-howtos/unit-testing/
------------------------------------------------------------------------------
    bugtraq:number = true

Added: myfaces/site/cms-site/trunk/content/wiki/core/user-guide/jsf-and-myfaces-howtos/unit-testing.mdtext
URL: http://svn.apache.org/viewvc/myfaces/site/cms-site/trunk/content/wiki/core/user-guide/jsf-and-myfaces-howtos/unit-testing.mdtext?rev=1573858&view=auto
==============================================================================
--- myfaces/site/cms-site/trunk/content/wiki/core/user-guide/jsf-and-myfaces-howtos/unit-testing.mdtext (added)
+++ myfaces/site/cms-site/trunk/content/wiki/core/user-guide/jsf-and-myfaces-howtos/unit-testing.mdtext Tue Mar  4 05:29:41 2014
@@ -0,0 +1,79 @@
+Title: Unit Testing
+Notice:    Licensed to the Apache Software Foundation (ASF) under one
+           or more contributor license agreements.  See the NOTICE file
+           distributed with this work for additional information
+           regarding copyright ownership.  The ASF licenses this file
+           to you under the Apache License, Version 2.0 (the
+           "License"); you may not use this file except in compliance
+           with the License.  You may obtain a copy of the License at
+           .
+             http://www.apache.org/licenses/LICENSE-2.0
+           .
+           Unless required by applicable law or agreed to in writing,
+           software distributed under the License is distributed on an
+           "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+           KIND, either express or implied.  See the License for the
+           specific language governing permissions and limitations
+           under the License.
+           
+There are multiple strategies to deal with Unit Testing when you are using
+MyFaces, but over the time we have identified two possible alternatives.
+
+ 1. MyFaces Test: unit testing using predefined mock objects for JSF 
+    artifacts. See [MyFaces Test Homepage](http://myfaces.apache.org/test/index.html)
+ 2. MyFaces Core Test Impl Module (new feature since 2.2.1): run a full 
+    MyFaces Core instance inside a JUnit 4 Test using a runner 
+    or an already initialized base class. In this mode it is also
+    possible to run JSF + CDI.
+
+The recommended strategy when you are creating your own unit tests is first
+define the scenario to test and then identify the right tool for the job.
+Let's see when and how each option works.
+
+## Using MyFaces Test ##
+
+If you are testing some methods in a JSF managed bean or a CDI bean, or
+if you need to check if a Validator or Converter works, possibly the 
+only thing you need is a dummy FacesContext instance and an environment
+where the beans lives. In that case, MyFaces Test provide some base 
+classes for your JUnit tests or if you need more flexibility
+it also has a class that encapsulates a mocked container. See
+org.apache.myfaces.test.mock.MockedJsfTestContainer for details. 
+If you are using CDI, there are different options out there 
+to run a CDI container inside a JUnit test that can be mixed with
+MyFaces Test. 
+
+See [MyFaces Test Homepage](http://myfaces.apache.org/test/index.html) for
+more information about how to use this library.
+
+## MyFaces Core Test Impl Module ##
+
+If you need to validate some behavior that is bound to the JSF lifecycle,
+for example create a view like in a real request, input some values and 
+click on a submit button, or check the view structure, or check
+the navigation algorithm, or check the interaction between your JSF views
+and your beans, then you need a JSF test environment that can be as 
+close as possible to the real one in your web application running 
+in a web server.
+
+In this case, the interest resides on test a complex server side logic and
+forget about the client side, which is usually encapsulated by the
+JSF component. If you need a full simulation you are probably looking 
+for functional testing, and in that case, tools like Arquillian,
+Selenium or Cargo + HtmlUnit can help you better.
+
+MyFaces Core Test Impl Module helps you to run MyFaces Core inside a JUnit 
+Test using a mocked servlet environment. The idea is "simulate" the 
+effect of the rendered html markup and javascript using a MockMyFacesClient
+which manipulates a ServletMockContainer and add the necessary request
+parameters in the same way as a web browser sends an HTTP request and
+then the web container decodes. If you are using a third party JSF 
+component library, it is easy to ceate a custom client class taking 
+MockMyFacesClient as base model and checking how HTTP requests are filled
+by a real web browser.
+
+Below you can find more information about how to use this library.
+
+{% for label, page in children %}* [{{ page.headers.title }}]({{ page.path }})
+{% endfor %}
+

Propchange: myfaces/site/cms-site/trunk/content/wiki/core/user-guide/jsf-and-myfaces-howtos/unit-testing.mdtext
------------------------------------------------------------------------------
    svn:eol-style = native

Added: myfaces/site/cms-site/trunk/content/wiki/core/user-guide/jsf-and-myfaces-howtos/unit-testing/create-test-from-base-class.mdtext
URL: http://svn.apache.org/viewvc/myfaces/site/cms-site/trunk/content/wiki/core/user-guide/jsf-and-myfaces-howtos/unit-testing/create-test-from-base-class.mdtext?rev=1573858&view=auto
==============================================================================
--- myfaces/site/cms-site/trunk/content/wiki/core/user-guide/jsf-and-myfaces-howtos/unit-testing/create-test-from-base-class.mdtext (added)
+++ myfaces/site/cms-site/trunk/content/wiki/core/user-guide/jsf-and-myfaces-howtos/unit-testing/create-test-from-base-class.mdtext Tue Mar  4 05:29:41 2014
@@ -0,0 +1,68 @@
+Title: Create Test from Base Class
+Notice:    Licensed to the Apache Software Foundation (ASF) under one
+           or more contributor license agreements.  See the NOTICE file
+           distributed with this work for additional information
+           regarding copyright ownership.  The ASF licenses this file
+           to you under the Apache License, Version 2.0 (the
+           "License"); you may not use this file except in compliance
+           with the License.  You may obtain a copy of the License at
+           .
+             http://www.apache.org/licenses/LICENSE-2.0
+           .
+           Unless required by applicable law or agreed to in writing,
+           software distributed under the License is distributed on an
+           "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+           KIND, either express or implied.  See the License for the
+           specific language governing permissions and limitations
+           under the License.
+
+First create a test class that extends from:
+
+ * org.apache.myfaces.mc.test.core.AbstractMyFacesRequestTestCase
+ * org.apache.myfaces.mc.test.core.AbstractMyFacesCDIRequestTestCase (Use Apache OpenWebbeans)
+
+For example
+ 
+    :::java
+    public class SimpleTestCase extends AbstractMyFacesCDIRequestTestCase
+    {
+        @Inject
+        @Named("helloWorld")
+        private HelloWorldController helloWorldBean;
+        
+        @Test
+        public void testHelloWorld() throws Exception
+        {
+            startViewRequest("/helloWorld.xhtml");
+            processLifecycleExecute();
+            Assert.assertEquals("page2.xhtml", helloWorldBean.send());
+            renderResponse();
+            
+            client.inputText("mainForm:name", "John Smith");
+            // The button end current request and start a new request 
+            // with a simulated submit 
+            client.submit("mainForm:submit");
+            
+            processLifecycleExecute();
+            Assert.assertEquals("John Smith", helloWorldBean.getName());
+            Assert.assertEquals("/page2.xhtml", facesContext.getViewRoot().getViewId());
+            endRequest();
+        }
+        
+        @Override
+        protected ExpressionFactory createExpressionFactory()
+        {
+            return new com.sun.el.ExpressionFactoryImpl();
+        }
+
+        @Override
+        protected String getWebappResourcePath()
+        {
+            return "webapp";
+        }
+    }
+    
+The method getWebappResourcePath() defines where in the test classpath are the pages
+to load. createExpressionFactory() defines the EL Expression factory used. There are
+many examples available in MyFaces Core 2.2.x impl module, so just take a look and
+the code.

Propchange: myfaces/site/cms-site/trunk/content/wiki/core/user-guide/jsf-and-myfaces-howtos/unit-testing/create-test-from-base-class.mdtext
------------------------------------------------------------------------------
    svn:eol-style = native

Added: myfaces/site/cms-site/trunk/content/wiki/core/user-guide/jsf-and-myfaces-howtos/unit-testing/create-test-junit-runner.mdtext
URL: http://svn.apache.org/viewvc/myfaces/site/cms-site/trunk/content/wiki/core/user-guide/jsf-and-myfaces-howtos/unit-testing/create-test-junit-runner.mdtext?rev=1573858&view=auto
==============================================================================
--- myfaces/site/cms-site/trunk/content/wiki/core/user-guide/jsf-and-myfaces-howtos/unit-testing/create-test-junit-runner.mdtext (added)
+++ myfaces/site/cms-site/trunk/content/wiki/core/user-guide/jsf-and-myfaces-howtos/unit-testing/create-test-junit-runner.mdtext Tue Mar  4 05:29:41 2014
@@ -0,0 +1,159 @@
+Title: Create Test using JUnit Runner
+Notice:    Licensed to the Apache Software Foundation (ASF) under one
+           or more contributor license agreements.  See the NOTICE file
+           distributed with this work for additional information
+           regarding copyright ownership.  The ASF licenses this file
+           to you under the Apache License, Version 2.0 (the
+           "License"); you may not use this file except in compliance
+           with the License.  You may obtain a copy of the License at
+           .
+             http://www.apache.org/licenses/LICENSE-2.0
+           .
+           Unless required by applicable law or agreed to in writing,
+           software distributed under the License is distributed on an
+           "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+           KIND, either express or implied.  See the License for the
+           specific language governing permissions and limitations
+           under the License.
+
+Create Test using JUnit Runner is no different than write other JUnit tests.
+Just use MyFacesTestRunner to run the tests. 
+
+    :::java
+    @RunWith(MyFacesTestRunner.class)
+    @TestConfig(webappResourcePath = "webapp")
+    public class DemoTestCase
+    {
+        @TestContainer
+        private MyFacesContainer container;
+        
+        @Test
+        public void testHelloWorld() throws Exception
+        {
+            container.startViewRequest("/helloWorld.xhtml");
+            container.processLifecycleExecute();
+            container.renderResponse();
+            
+            container.getClient().inputText("mainForm:name", "John Smith");
+            // The button end current request and start a new request 
+            // with a simulated submit 
+            container.getClient().submit("mainForm:submit");
+            
+            container.processLifecycleExecute();
+            
+            Assert.assertEquals("/page2.xhtml", 
+                container.getFacesContext().getViewRoot().getViewId());
+            container.renderResponse();
+            container.endRequest();
+        }
+    }
+
+The key object to control the lifecycle and the context in general is 
+MyFacesContainer. With this object, it is possible to send a request, 
+execute the lifecycle, make assertions and so on. Remember you are always 
+on the server side, and the "client" is just something in the middle that 
+mimic the steps done by the browser, so it is quite simple to write 
+a custom client that manipulates MyFacesContainer for a third party library.
+            
+There are some annotations that allow to define methods that are called at some
+specified points:
+
+ * @BeforeRequest and @AfterRequest
+ * @SetupWebConfigParams : makes the method called before init but after 
+   servletContext is created
+ * @BeforeJSFInit : called after servlet listeners are initialized but before
+   JSF init.
+        
+Here are some examples about how to make it run with different environments:
+
+### Apache MyFaces Core + Apache OpenWebbeans ###
+
+    :::java
+    @RunWith(MyFacesTestRunner.class)
+    @TestConfig(
+        scanAnnotations = false,
+        webappResourcePath = "webapp",
+        expressionFactory = "com.sun.el.ExpressionFactoryImpl")
+    @TestServletListeners({
+        "org.apache.webbeans.servlet.WebBeansConfigurationListener"
+    })
+    public class MyFacesRunnerSimpleTestCase
+    {
+        @TestContainer
+        private MyFacesContainer container;
+        
+        @Inject
+        @Named("helloWorld")
+        private HelloWorldController helloWorldBean;
+        
+        @Test
+        public void testHelloWorld() throws Exception
+        {
+            // ....
+
+
+### Apache MyFaces Core + Spring 3 ###
+
+Please note bean injection is only supported for CDI, but it is possible
+to enable it if you provide an implementation of InjectionProvider spi 
+interface:
+
+    :::java
+    @RunWith(MyFacesTestRunner.class)
+    @TestConfig(
+        scanAnnotations = true,
+        oamAnnotationScanPackages = "jsf2jpa",
+        webappResourcePath = "webapp",
+        expressionFactory = "com.sun.el.ExpressionFactoryImpl")
+    @TestServletListeners({
+        "org.springframework.web.context.ContextLoaderListener",
+        "org.springframework.web.context.request.RequestContextListener"
+    })
+    public class MyFacesRunnerSimpleTestCase
+    {
+        @TestContainer
+        private MyFacesContainer container;
+        
+        @Test
+        public void testHelloWorld() throws Exception
+        {
+            // ....
+
+### Apache MyFaces Core + Weld 1.1.x ###
+
+    :::java
+    @RunWith(MyFacesTestRunner.class)
+    @TestConfig(
+        scanAnnotations = false,
+        webappResourcePath = "webapp",
+        expressionFactory = "com.sun.el.ExpressionFactoryImpl")
+    @TestServletListeners({
+        "org.jboss.weld.environment.servlet.Listener"
+    })
+    public class MyFacesRunnerSimpleTestCase
+    {
+        @TestContainer
+        private MyFacesContainer container;
+        
+        @Inject
+        @Named("helloWorld")
+        private HelloWorldController helloWorldBean;
+        
+        @BeforeJSFInit
+        public void init() throws NamingException
+        {
+            InitialContext ic = new InitialContext();
+            ic.bind("java:comp/env/BeanManager", 
+                container.getServletContext().getAttribute(
+                    "org.jboss.weld.environment.servlet.javax.enterprise.inject.spi.BeanManager"));
+            container.getServletContext().setAttribute("javax.enterprise.inject.spi.BeanManager",
+                container.getServletContext().getAttribute(
+                    "org.jboss.weld.environment.servlet.javax.enterprise.inject.spi.BeanManager"));
+        }
+        
+        @Test
+        public void testHelloWorld() throws Exception
+        {
+            // ....
+            
+

Propchange: myfaces/site/cms-site/trunk/content/wiki/core/user-guide/jsf-and-myfaces-howtos/unit-testing/create-test-junit-runner.mdtext
------------------------------------------------------------------------------
    svn:eol-style = native

Added: myfaces/site/cms-site/trunk/content/wiki/core/user-guide/jsf-and-myfaces-howtos/unit-testing/setup-with-maven.mdtext
URL: http://svn.apache.org/viewvc/myfaces/site/cms-site/trunk/content/wiki/core/user-guide/jsf-and-myfaces-howtos/unit-testing/setup-with-maven.mdtext?rev=1573858&view=auto
==============================================================================
--- myfaces/site/cms-site/trunk/content/wiki/core/user-guide/jsf-and-myfaces-howtos/unit-testing/setup-with-maven.mdtext (added)
+++ myfaces/site/cms-site/trunk/content/wiki/core/user-guide/jsf-and-myfaces-howtos/unit-testing/setup-with-maven.mdtext Tue Mar  4 05:29:41 2014
@@ -0,0 +1,98 @@
+Title: Setup Test Environment with Maven
+Notice:    Licensed to the Apache Software Foundation (ASF) under one
+           or more contributor license agreements.  See the NOTICE file
+           distributed with this work for additional information
+           regarding copyright ownership.  The ASF licenses this file
+           to you under the Apache License, Version 2.0 (the
+           "License"); you may not use this file except in compliance
+           with the License.  You may obtain a copy of the License at
+           .
+             http://www.apache.org/licenses/LICENSE-2.0
+           .
+           Unless required by applicable law or agreed to in writing,
+           software distributed under the License is distributed on an
+           "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+           KIND, either express or implied.  See the License for the
+           specific language governing permissions and limitations
+           under the License.
+
+First of all, you need to include the required test dependencies into your
+project:
+
+    :::xml
+    <!-- MyFaces 2.2.x should be available as a compile dependency or
+         as a test dependency -->
+    <!-- Base library to run MyFaces in a JUnit test. If you are
+         using MyFaces Core 2.2.1 use the same version number -->
+    <dependency>
+        <groupId>org.apache.myfaces.core</groupId>
+        <artifactId>myfaces-impl-test</artifactId>
+        <version>2.2.1</version>
+        <scope>test</scope>
+    </dependency>    
+    
+    <!-- The library uses Myfaces-Test mock objects -->
+    <dependency>
+        <groupId>org.apache.myfaces.test</groupId>
+        <artifactId>myfaces-test22</artifactId>
+        <version>1.0.6</version>
+        <scope>test</scope>
+    </dependency>
+
+    <!-- Don't forget junit -->
+    <dependency>
+        <groupId>junit</groupId>
+        <artifactId>junit</artifactId>
+        <version>4.11</version>
+    </dependency>
+    
+    <!-- Don't forget to include an EL implementation, otherwise
+         you will use the mock EL implementation bundled with
+         MyFaces Test -->
+    <dependency>
+        <groupId>org.eclipse.jetty.orbit</groupId>
+        <artifactId>javax.el</artifactId>
+        <version>2.2.0.v201108011116</version>
+        <scope>test</scope>
+    </dependency>
+    <dependency>
+        <groupId>org.eclipse.jetty.orbit</groupId>
+        <artifactId>com.sun.el</artifactId>
+        <version>2.2.0.v201108011116</version>
+        <scope>test</scope>
+    </dependency>
+    
+Suppose a typical web application using maven. It usually has the following
+structure:
+
+    :::text
+    src
+     |- main
+          |- java
+          |- resources
+          |- webapp
+     |- test
+          |- java
+          |- resources
+
+In the test phase, by default the environment only has access to the resources
+in src/main/java, src/main/resources, src/test/java and src/test/resources
+through the classpath. So one thing you need is make the webapp folder accesible
+as test resources through the classpath with something like this.
+
+    :::xml
+    <build>
+        <testResources>
+            <testResource>
+                <directory>${project.basedir}/src/test/resources</directory>
+            </testResource>
+            <testResource>
+                <directory>${project.basedir}/src/main/webapp</directory>
+                <targetPath>webapp</targetPath>
+            </testResource>
+        </testResources>  
+        
+        <!-- ... -->
+    </build>
+
+

Propchange: myfaces/site/cms-site/trunk/content/wiki/core/user-guide/jsf-and-myfaces-howtos/unit-testing/setup-with-maven.mdtext
------------------------------------------------------------------------------
    svn:eol-style = native