-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Mocking pojo objects during DAO testing
PostPosted: Sun Feb 28, 2016 3:18 pm 
Newbie

Joined: Sun Feb 28, 2016 3:16 pm
Posts: 17
Hello all!

How you mocking pojo objects during DAO testing?
I am getting org.springframework.orm.hibernate5.HibernateSystemException: Unknown entity: com.domain.models.Category$$EnhancerByCGLIB$$c5e1a209;

But without mocks everything works fine.

Million times thank you!!!!!


Top
 Profile  
 
 Post subject: Re: Mocking pojo objects during DAO testing
PostPosted: Sun Feb 28, 2016 5:19 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Can you post the code?

You can either mock the DAO layer, and test the Service layer in isolation, or you can use an in-memory database or a real database to test the DAO layer too.


Top
 Profile  
 
 Post subject: Re: Mocking pojo objects during DAO testing
PostPosted: Mon Feb 29, 2016 7:17 am 
Newbie

Joined: Sun Feb 28, 2016 3:16 pm
Posts: 17
I do not want to mock session factory, I want to mock only models.
Here is my code:
Code:
    @Mock
    private Category category;
    //...testTarget and other mocks
    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        //get connection, doReturn, etc.
    }

    @Test
    public void save() throws Exception {
        testTarget.save(category);

        assertThat(category.getId()).isNotEqualTo(0);
    }

Mapping:
Code:
<hibernate-mapping default-lazy="true">
    <class name="com.domain.models.Category" table="Category">
        <id name="id" type="long" access="property">
            <column name="categoryId" length="20"/>
            <generator class="identity"/>
        </id>
        <property name="name" column="categoryName" type="string"/>
        <property name="image" column="categoryImage" type="string"/>
        <property name="parentId" column="categoryParentId" type="long"/>
    </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject: Re: Mocking pojo objects during DAO testing
PostPosted: Mon Feb 29, 2016 8:36 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
You don;t need to mock entities because those are just POJO.
Typically, you are interested in mocking services like Data Access Objects or Service classes.

But an entity is just a regular object that shouldn;t have any dependency, so why would you want to mock it?


Top
 Profile  
 
 Post subject: Re: Mocking pojo objects during DAO testing
PostPosted: Mon Feb 29, 2016 12:51 pm 
Newbie

Joined: Sun Feb 28, 2016 3:16 pm
Posts: 17
Because any POJO object in Java might contain logic in getters, setters or constructors.
This code influences on DAO testing.


Top
 Profile  
 
 Post subject: Re: Mocking pojo objects during DAO testing
PostPosted: Mon Feb 29, 2016 4:25 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
I wouldn't store business logic in entities. The service layer is a much better place for business logic.
Anyway, you could just use interfaces and have the POJOs implement those. In the unit tests, you can mock the interfaces and the problem is solved.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.