Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6886563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:46:30+00:00 2026-05-27T05:46:30+00:00

EDIT: Cleaned up the question for readability. Please ignore comments up to October 31st.

  • 0

EDIT: Cleaned up the question for readability. Please ignore comments up to October 31st.

In our application stack we work with many smaller jar modules that get combined into a final web application. One module defines JSF features, like implementing this ViewScope.

Now appart from integration testing we want to be able to unit test every part and thus need a way to mock a complete Faces Context (to be accessed via a wrapper) to test classes that use it.

The important part here is complete meaning it has to have an initialized ViewMap as this is where our ViewScope puts its objects.


I’ve tried different approaches:

1) shale-test: I’ve come the furthest with this but unfortunately the project is retired.

So far I’ve wrapped the FacesContext in a Provider which allows me to replace it with a Mocked FacesContext for testing. I’ve also modified the shale implementation of AbstractViewControllerTestCase to include an application context.

However when calling MockedFacesContext.getViewRoot().getViewMap() as this will throw an UnsupportedOperationException. The reasons seems to be that the MockApplication does not instantiate Application.defaultApplication (it’s null) which is required for this method call. This seems to be a shale-test limitation.


2) JMock or mockito These seem to me not to not really mock anything at all as most members will just remain null. Don’t know if JMock or mockito can actually call the propper initialization methods.


3) Custom Faces Mocker: To me this seems the only remaining option but we don’t really have the time to analyse how Faces is initialized and recreate the behaviour for mocking purposes. Maybe someone has dont this before and can share major waypoints and gotchas?


Or is there any alternative way to mock a FacesContext outside a web application?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:46:30+00:00Added an answer on May 27, 2026 at 5:46 am

    I would go with PowerMock+Mockito:

    From your link:

    private Map<String,Object> getViewMap() {
        return FacesContext.getCurrentInstance().getViewRoot().getViewMap();
    }
    

    In the test:

    @RunWith(PowerMockRunner.class)
    @PrepareForTest({ FacesContext.class });
    public class TheTest {
        /*
         * fake viewMap.
         */
        private Map<String,Object> viewMap = Maps.newHashMap() // guava
    
        /**
         * mock for FaceContext
         */
        @Mock
        private FacesContext faceContext;
    
        /**
         * mock for UIViewRoot
         */
        @Mock
        private UIViewRoot uiViewRoot;
    
        @Before
        public void setUp() {
            Mockito.doReturn(this.uiViewRoot).when(this.faceContext).getViewRoot();
            Mockito.doReturn(this.viewMap).when(this.uiViewRoot).getViewMap();
    
            PowerMock.mockStatic(FacesContext.class);
            PowerMock.doReturn(this.faceContext).when(FacesContext.class, "getCurrentInstance");
        }
    
        @Test
        public void someTest() {
            /*
             * do your thing and when  
             * FacesContext.getCurrentInstance().getViewRoot().getViewMap(); 
             * is called, this.viewMap is returned.
             */
        }
    }
    

    Some reading:

    • http://code.google.com/p/powermock/wiki/MockitoUsage
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Edit: This question was written in 2008, which was like 3 internet ages ago.
Edit: From another question I provided an answer that has links to a lot
EDIT: Learned that Webmethods actually uses NLST, not LIST, if that matters Our business
EDIT: This question is more about language engineering than C++ itself. I used C++
edit #2: Question solved halfways. Look below As a follow-up question, does anyone know
My question is pretty simple: If you have two web-application components: Server-side (secret-capable) code
In Java, when having some non-empty ArrayList, does list.sublist(from,to).clear() edit (refactored question): reduce the
EDIT: This was formerly more explicitly titled: - Best solution to stop Kontiki's KHOST.EXE
EDIT What small things which are too easy to overlook do I need to
Edit : Solved, there was a trigger with a loop on the table (read

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.