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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:58:24+00:00 2026-05-29T05:58:24+00:00

Essentially is a question about a JUnit test where you create an entity in

  • 0

Essentially is a question about a JUnit test where you create an entity in the @Before method but you can’t find it in the actual test method. I’ve been trying to find out what’s going on, and any kind of help would be really appreciated.

Essentially it’s a JUnit class like this:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"myConfiFile.xml"})
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
@Transactional
public class DummyTest {

@Before
public void setUp() {
    // Create an entity here and call .save()
}

@Test
public void testCountMsisdnNumberPlans() {
    int howManyInstances = dao.countInstancesOfEntity();
}
}

The DAO code is quite simple:

@Transactional(readOnly = true)
public Integer countInstancesOfEntity(Integer idhlr) {
    return ((BigDecimal) em.createNativeQuery("SELECT COUNT(*) FROM ENTITY")
        .getSingleResult()).intValue();
}

In the setUp() we create one entity but the DAO code keeps returning 0. Any comment would be really great.

Thanks.

  • 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-29T05:58:24+00:00Added an answer on May 29, 2026 at 5:58 am

    SpringJUnit4ClassRunner executes @Before and @After inside the same transaction as the test method. Thus, changes made in persistence context in @Before are not flushed to the database before running the test method. Implicit flush before executing a query doesn’t happen as well, because transaction is configured as read only.

    So, you have several options:

    • Remove readOnly = true, it should enable automatic flush before executing the query
    • Add explicit flush() to @Before, it should force flush even if transaction is read only
    • Use @BeforeTransaction instead of @Before. Note that this method runs outside of transaction, so that it’s not affected by defaultRollback setting, and requires manual transaction management.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I initially asked a question about how to create linux users via HTML (essentially
I have a question about Java style. I've been programming Java for years, but
My question is essentially the same as the following one but the answer did
This seems like a basic question, but I haven't found any clear answers. Essentially,
This is probably a question that is more about object alloc/retain/release, but I'll use
I posted a question not to long ago about how my program was essentially
Essentially, my question is: can this not be done any easier? ; and what
Essentially, it's a rehash of this question for Entity Framework 4. How do you
Hey, I've got a question about general member function pointers. I'm trying to achieve
N.B This question has been significantly edited before the first answer was given. Hi,

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.