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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:43:57+00:00 2026-05-28T20:43:57+00:00

Suppose I have this: @Transactional(rollbackFor = NotificationException.class) public interface PersonManagerService { public void addPerson(Person

  • 0

Suppose I have this:

@Transactional(rollbackFor = NotificationException.class)
public interface PersonManagerService {
   public void addPerson(Person person);
}

and an implementation:

public class PersonManagerServiceImpl implements PersonManagerService {

public OtherService otherService;

public void addPerson(Person person) {
// stuff
}

// getter and setter for otherService
}

How would I go about mocking the otherService dependency while still having the addPerson method hit the database?

My scenario is that I want to test that a particular exception causes a rollback of the saving of the person that is being added. This exception would come from the OtherService class, which I don’t want to call the real version of. I am currently using Spring Transaction annotations so I have a test like this:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {various locations})
public class Test() {

@Autowired
PersonManagerService service;

@Test
public void test() {
// how to call setOtherService so it can be a mock?
}

If I try to cast the auto wired bean, I get an IllegalArgumentException because it is a proxy. If I made the Impl not use an interface, I could use CGLIB but I don’t want to do that. If I create the impl programmatically, then it’s not tied into the transaction flow. What other options do I have?

  • 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-28T20:43:58+00:00Added an answer on May 28, 2026 at 8:43 pm

    There are various ways you can tackle this program (from best to worst):

    • take advantage of @Profiles – in Spring 3.1 you can associate profile name with every bean. When you are starting an application context you provide active profiles and only beans without any profile associated or with profile matching provided will be instantiated. This is a very powerful mechanism.

      @Profile("prd")
      public class PersonManagerServiceImpl implements PersonManagerService
      
      //...
      
      @Profile("test")
      public class PersonManagerServiceMock implements PersonManagerService
      
      //...
      
      @ContextConfiguration
      @ActiveProfiles(value = "test")
      public class Test {
      
    • use primary or @Primary – if you are autowiring otherService in PersonManagerServiceImpl you can define a second mock bean with primary="true" attribute or @Primary annotation. Spring will prefer primary beans when autowiring.

    • unwrap transactional proxy (see: Is it possible to unproxy a Spring bean? and Mocking a property of a CGLIB proxied service not working) to access setter. A bit hacky, but works for others

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

Sidebar

Related Questions

Suppose I have this class: class Int{ private: int x; public: Int(int i) :
Suppose I have this interface public interface IFoo { ///<summary> /// Foo method ///</summary>
Suppose I have this function: void my_test() { A a1 = A_factory_func(); A a2(A_factory_func());
Suppose I have a class like the following: public class Stage { public int
suppose we have these classes: public class Record { int key; int value; Record(){
Supposing I mark a method @Transactional(rollbackFor = PersistenceException.class) in my Service layer and have
So suppose I have the Person and Child models: class Person < ActiveRecord::Base has_many
Suppose I have this function, in pseudo-code, that reverse a list: def function reverse(
Suppose I have this Windows wchar_t string: L\x4f60\x597d and L\x00e4\x00a0\x597d and would like to
Suppose I have this code: var myArray = new Object(); myArray["firstname"] = "Bob"; myArray["lastname"]

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.