Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Testing chapter regarding avoiding false positives with JPA [SPR-9032] #13671

Closed
spring-issuemaster opened this issue Jan 17, 2012 · 3 comments
Assignees
Milestone

Comments

@spring-issuemaster
Copy link
Collaborator

@spring-issuemaster spring-issuemaster commented Jan 17, 2012

Mattias Severson opened SPR-9032 and commented

Step by step:

  1. Go to http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#testcontext-tx
  2. Scroll down to "Avoid false positives when testing ORM code"
  3. Find how to include SessionFactory and flush() is used to avoid false positives if you are using Hibernate.

The information is good, but it is not applicable if you use JPA. To avoid false positives when using JPA (backed by Hibernate) I propose that you add a new snippet that the describes a different approach. Suggested implementation:

@PersistenceContext
EntityManager entityManager;

@Test(expected = GenericJDBCException.class)
public void updateWithSessionFlush() {
    updateJpaEntity();
    // Manual flush is required to avoid false positive in test
    entityManager.unwrap(Session.class).flush();
}

Affects: 3.1 GA

Issue Links:

  • #18296 Doc: Flush in test with SessionFactory vs EntityManager ("is duplicated by")

Referenced from: commits 64c388d

@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Jan 19, 2012

Mattias Severson commented

Note, I have also read that you could get a SessionFactory using the following configuration:

<bean id="sessionFactory" class="org.springframework.orm.jpa.vendor.HibernateJpaSessionFactoryBean">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
    <!-- more properties needed ? -->
</bean>

In that case, the text in the test documentation can remain unchanged.

Dependencies used:
Spring version 3.1

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.0.1.Final</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.0.1.Final</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.0-api</artifactId>
        <version>1.0.1.Final</version>
    </dependency>
@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Jan 20, 2012

Mattias Severson commented

It seems that there is a more simple solution to this problem that I previously had overlooked:

@PersistenceContext
EntityManager entityManager;

@Test(expected = GenericJDBCException.class)
public void updateWithSessionFlush() {
updateJpaEntity();
// Manual flush is required to avoid false positive in test
entityManager.flush();
}

That said, I still think that the information in the reference manual about how to avoid false positives during integration testing when using ORM with aid of JPA should be updated.

@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented May 3, 2016

Sam Brannen commented

Since the time that this issue was originally opened, the requested information had already been added to the reference manual in conjunction with #18296; however, it contained a bug and misleading text.

This has now been addressed in the following GitHub commit:

64c388d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.