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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:50:21+00:00 2026-06-18T08:50:21+00:00

I’m trying to properly set up junit test for a persistance class and even

  • 0

I’m trying to properly set up junit test for a persistance class and even though I’m getting ‘green bar’, I’m worried about couple of things.
When i run the test file in debugger, I can see for the testList() that all object are loaded as expected and if any one has children, they are loaded as well.

For testList(), after I do...
instance.update(sg);
List <SettingsGroup> sl = instance.list();

… I see in debugger (screenshot at the end) that children are not loaded.

What’s the proper way to update an element and retrieve it back to verify update did happen?
Right now I’m doing something like

instance.update(sg);
List <SettingsGroup> sl = instance.list();

… but I’m not sure about that.

When I’m trying to testDelete(), size of list of elments is the same as before delete and it makes me wonder.

When tried to research hibernate testing, I’m running into a lot of simple examples, but I’m just not getting yet how to do proper set up, tear down and any session/transaction management.

I table corresponding to this class has 5 ‘hardcoded’ rows, so I pretty sure about ‘read’ calls.

TEST CLASS

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration( locations = {"applicationContext.xml"})
@Transactional
public class SettingGroupDaoTest extends AbstractJUnit4SpringContextTests {

    public SettingGroupDaoTest(){}
    SettingGroupDao instance;

    @BeforeClass
    public static void setUpClass() throws Exception {}

    @AfterClass
    public static void tearDownClass() throws Exception {}

    @Before
    public void setUp()   {
        Configuration c  = HibernateConfiguration.getHibernateConfiguration()
                .configuration(SettingsGroup.class);

        SessionFactory sessionFactory = c.buildSessionFactory();
        instance = new SettingGroupDao();
        instance.setSessionFactory( sessionFactory );
    }

    @Test
    public void testList() throws Exception {
        SettingsGroup sq = instance.get(1);
        SettingsGroup s1 = sq.getChildren().get(0);
        System.out.println( "children index 2 name: " + s1.getName() );
        System.out.println( "2nd element of children: " + sq.getChildren().get(1).getName() );
    }

    @Test
    public void testUpdate() throws Exception{
        SettingsGroup sg = instance.get(1);
        sg.setName("Updated Name of Setting Group");
        instance.update(sg);
        List <SettingsGroup> sl = instance.list();
        Hibernate.initialize( instance.list() );
        System.out.println( "UPDATED Settings Group Name: " + sg.getName() );
    }

    @Test
    public void testDelete() throws Exception{
        instance.delete( 0 );
        int s = instance.list().size();
        System.out.println( "Size after delete: " + s );
    }
}

CONFIG FOR TEST ABOVE:

public class HibernateConfiguration {
    private HibernateConfiguration(){}
    public Configuration configuration( java.lang.Class clazz ) {
        Configuration c = new Configuration();
        c.setProperty(Environment.DRIVER, "org.postgresql.Driver");
        c.setProperty(Environment.URL, "jdbc:postgresql://localhost/###");
        c.setProperty(Environment.USER, "postgres");
        c.setProperty(Environment.PASS, "####");
        c.setProperty(Environment.DIALECT, "org.hibernate.dialect.PostgreSQLDialect");
        c.setProperty(Environment.AUTOCOMMIT, "true"); //"true");
        c.addClass( clazz );
        return c;
    }
    public static HibernateConfiguration getHibernateConfiguration(){
        return new HibernateConfiguration();
    }
}

DOMAIN CLASS:

@Entity
@SequenceGenerator(name = "settings_group_id_seq", sequenceName = "settings_group_id_seq")
@Cache(usage= CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Table(name="settings_group")
public class SettingsGroup implements Serializable, DomainObject {

    private int id;
    private String name;
    private String code;

    private List<SettingsGroup> children = new LinkedList<SettingsGroup>();

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "settings_group_id_seq")
    @Column(name = "id", insertable = false)
    public int getId() {return id;}

    public void setId(int id) {this.id = id;}

    @Column(name = "name")
    public String getName() {return name;}

    public void setName(String name) {this.name = name;}

    @Column( name = "code")
    public String getCode() {return code; }

    public void setCode(String code) {this.code = code;}


    @OneToMany( fetch = FetchType.EAGER)
    @JoinColumn( name = "parent_id")
    @OrderColumn
    public List<SettingsGroup> getChildren() {return children;}

    public void setChildren(List<SettingsGroup> children) {this.children = children;}
}

enter image description 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-06-18T08:50:22+00:00Added an answer on June 18, 2026 at 8:50 am

    A couple of thoughts … what you’re doing is really an integration test since it requires a database. Secondly, not positive, but don’t you have to commit the update before you can successfully query the updated rows?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I don't have much knowledge about the IPv6 protocol, so sorry if the question

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.