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

  • Home
  • SEARCH
  • 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 6356315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:56:21+00:00 2026-05-24T22:56:21+00:00

I am using @Transactional for my JUnit tests (main advantage is rollback of changes

  • 0

I am using @Transactional for my JUnit tests (main advantage is rollback of changes within one test) but I have small problem that this influence my service transactions. So for example this is

my service :

@Service
@Transactional(propagation = Propagation.REQUIRED)
public class ServiceImpl

my unit test :

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/test-context.xml" })
@Transactional
public class TestService 

@Test
public void testNumberTransaction() {
Entity a = new Entity();
Entity b = new Entity();
service.add(a);
service.add(b);
}

So naively I expected to have two separate transactions for service.add() but unless I use @nontransactional on the test method it runs inside one transaction (but then it does not roll back after test).

Is this expected?Can I alter it with some configuration?

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-24T22:56:22+00:00Added an answer on May 24, 2026 at 10:56 pm

    Yes, it’s expected. Propagation.REQUIRED (which is the default) means: execute in the existing transaction if it exists. Else, create a transaction and commit it at the end of the method.

    So yes, if the whole test method is transactional, both service calls will execute in the context of the test transaction.

    Note that since the service is annotated with REQUIRED, it’s supposed to work if a transaction already exists. This makes the test valid: it tests your service in the context of an existing transaction. If you want a service to execute in its own dedicated transaction, it should be annotated with REQUIRES_NEW. But of course, if it’s the case, you won’t be able to rollback the service transaction by executing the test in a transaction.

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

Sidebar

Related Questions

I have no problem testing my DAO and services, but when I test INSERT
im using Junit with spring-test and i would like to have a classic transactionnal
Suppose I'm using transactional replication to replicate articles from one server to another (
I have a need to create a transactional process using an external API that
I am trying to execute a SQL script in my JUnit test using Spring.
The following (simplified version of our) code passes our JUnit tests under Hibernate, but
I'm using Spring junit runner, and its transaction capabilities to start and rollback transactions
I am wondering how I can speedup my unit tests. I am using JUnit
I have a test harness using Spring's SpringJUnit4ClassRunner (which automatically rolls back DB updates
I'm using Spring 3.0.4 and JUnit 4.5. My test classes currently uses Spring's annotation

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.