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 7001327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:47:21+00:00 2026-05-27T20:47:21+00:00

I am trying to do a unit test on the list action of a

  • 0

I am trying to do a unit test on the list action of a controller. Here is the code for testing it :

  void testListAction()
   {
       ac = new AddressesController();

       def org = new Organizations(viewAllPost: true);
       mockForConstraintsTests(Addresses);
       def a = new Addresses(firstLine:'A', secondLine:'B', thirdLine:'C', luCountry:UnitedStates, zipCode:'12345', luState:Florida, city:'jag');
       assertTrue(a.validate());
       mockSession['currentUserOrganizationId'] = org;  

       mockDomain(Addresses, [
           new Addresses(firstLine:'A1', secondLine:'B', thirdLine:'C', luCountry:UnitedStates, zipCode:'12345', luState:Florida, city:'jag'),
           new Addresses(firstLine:'A2', secondLine:'B2', thirdLine:'C2', luCountry:UnitedStates, zipCode:'12344', luState:Florida, city:'jag2')
           ]);

        def model = ac.list();
       assertEquals(2, model.postInstanceList.size());
   }

But No matter how I tried I am always getting back the same result that the model.postInstanceList is null and I can not invoke the size method on it. What am I doing wrong here ?

  • 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-27T20:47:21+00:00Added an answer on May 27, 2026 at 8:47 pm

    You are not saving the instances. You should save:

    mockDomain(Addresses)
    new Addresses(firstLine:'A1', secondLine:'B', thirdLine:'C', luCountry:UnitedStates, zipCode:'12345', luState:Florida, city:'jag').save()
    
    new Addresses(firstLine:'A2', secondLine:'B2', thirdLine:'C2', luCountry:UnitedStates, zipCode:'12344', luState:Florida, city:'jag2').save()
    

    I would do it like this:

    mockDomain(Addresses)
    mockForContraintsTests(Addresses)
    def address1 = new Addresses(firstLine:'A1', secondLine:'B', thirdLine:'C', luCountry:UnitedStates, zipCode:'12345', luState:Florida, city:'jag')
    if(address1.validate()) address1.save()
    def address2 = new Addresses(firstLine:'A2', secondLine:'B2', thirdLine:'C2', luCountry:UnitedStates, zipCode:'12344', luState:Florida, city:'jag2')
    if(address2.validate()) address2.save()
    
    
    assertEquals 2, Addresses.list().size()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use embedded ActiveMQ broker for unit testing, as explained here: http://activemq.apache.org/how-to-unit-test-jms-code.html
I am trying to unit test a controller action that uses UpdateModel but I
I am trying to unit test some code, and I need to to replace
I am trying to learn unit testing. I am trying to unit test some
I am currently trying to write some unit test against my zend framework controller.
I'm trying to mock up DomainContext calls for my unit test. My real code
I'm trying to unit test struts2 actions (hitting a real database), but I haven't
I'm trying to unit test my routes using the MvcContrib.TestHelper ShouldMapTo <TController> () extension
I'm trying to unit test a private method that I have attached to my
I was trying to unit test the apple push notification library when I got

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.