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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:46:35+00:00 2026-06-08T05:46:35+00:00

I need your help in solving one issue. I have such method in my

  • 0

I need your help in solving one issue. I have such method in my DAO class that saves Role:

@Repository(value="jdbcRoleDAO")
@Transactional(propagation=Propagation.SUPPORTS, readOnly=true, rollbackFor=Exception.class)
public class JdbcRoleDAO implements RoleDAO {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private DBLogger<Role> dbLogger;

    /**
     * @throws DublicateEntryException if object with specified unique for application field already 
     *              exists in DB.
     */
    @CacheEvict(value = "roles", allEntries = true)
    @Transactional(propagation=Propagation.REQUIRED, readOnly=false, rollbackFor=Exception.class)
    @Override
    public Role save(final Role role) {

        if (this.getRoleByName(role.getName()) != null) {
            throw new DublicateEntryException("Record with specified role name already exists in application.");
        }

        KeyHolder keyHolder = new GeneratedKeyHolder();
        jdbcTemplate.update(new PreparedStatementCreator() {

            @Override
            public PreparedStatement createPreparedStatement(Connection connection)
                    throws SQLException {

                PreparedStatement ps = connection.prepareStatement(SQL_INSERT_ROLE, new String[]{"ID"});

                ps.setString(1, role.getName());
                ps.setString(2, role.getDesription());
                ps.setObject(3, (role.getParentRole()!=null)?role.getParentRole().getId():null);
                ps.setString(4, role.getPrivilege().toString());

                return ps;
            }
        }, keyHolder);

        Long key = (Long) keyHolder.getKey();
        if (key != null) {
            role.setId(key);
        } else {
            role.setId(-1);
        }

        // Add information about operation to log table
        dbLogger.logBasicOperation(role, OperationName.CREATE);

        return role;
    }
...
}

And I want to write unit test for this method using Mockito. For now it looks like:

@RunWith(MockitoJUnitRunner.class)
public class JdbcRoleDAOTest {

    private static final long TEST_ROLE_ID = 1L;
    private static final int TEST_ROLE_VERSION = 7;

    @Mock
    private DBLogger<Role> dbLogger;

    @Mock
    private JdbcTemplate jdbcTemplate;

    @InjectMocks
    private JdbcRoleDAO roleDAO;

    /**
     * test role that is used in all methods.
     */
    private Role role;

    @Before
    public void setUp(){
        // Create a test role object
        role = new Role();
        role.setId(TEST_ROLE_ID);
        role.setName("TEST");
        role.setDesription("Test role");
        role.setPrivilege(Privilege.DEFAULT);
        role.setVersion(TEST_ROLE_VERSION);

        // Return correct version of the object
        Mockito.when(jdbcTemplate.queryForInt(JdbcRoleDAO.SQL_GET_VERSION, TEST_ROLE_ID))
            .thenReturn(TEST_ROLE_VERSION);
    }

    @Test
    public void testSave() {
        Assert.assertNotNull(role);

        roleDAO.save(role);

        InOrder inOrder = Mockito.inOrder(jdbcTemplate, dbLogger);

        // Verify execution of the conditions.
        inOrder.verify(jdbcTemplate, Mockito.times(1)).update(Mockito.any(PreparedStatementCreator.class), 
                Mockito.any(KeyHolder.class));

        inOrder.verify(dbLogger, Mockito.times(1)).logBasicOperation(role, OperationName.CREATE);

        /* 
         * Expected -1 because I can't mock KeyHolder and should be returned -1 value (because 
         * KeyHolder will return null instead of key)
         */ 
        Assert.assertEquals("Generated id is wrong!", -1, role.getId());
    }
…
}

The problem is in that I want to mock somehow KeyHolder but I don’t know how better to do this. As you see, now KeyHolder instance is created in the save method and that’s is a main difficulty. I thought about injecting KeyHolder using Spring with scope “prototype” but as I understand this can cause problems when few users will concurrently try to save new Roles. Also I tried to do something like this:

Mockito.when(jdbcTemplate.update(Mockito.any(PreparedStatementCreator.class), 
                Mockito.any(KeyHolder.class)))
                .thenAnswer(new Answer<Void>() {

                    @Override
                    public Void answer(InvocationOnMock invocation)
                            throws Throwable {

                        Object[] arguments = invocation.getArguments();
                        for (int i=0; i<arguments.length; i++) {
                            if (arguments[i] instanceof KeyHolder) {
                                KeyHolder keyHolder = (KeyHolder) arguments[i];

                                KeyHolder spyKeyHolder = Mockito.spy(keyHolder);
                                Mockito.when(spyKeyHolder.getKey()).thenReturn(TEST_GENERATED_ID);
                                Mockito.doReturn(TEST_GENERATED_ID).when(spyKeyHolder).getKey();
                            }
                        }

                        return null;
                    }
                });

But this doesn’t work.
May be someone can give me an advice how it is possible to mock (or spy) KeyHolder not moving it outside from save method? Is there some features in Mockito that can make this possible? Or it is impossible?

Thank you in advance

  • 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-06-08T05:46:37+00:00Added an answer on June 8, 2026 at 5:46 am

    You could create the following interface:

    public interface KeyHolderFactory {
         KeyHolder newKeyHolder();
    }
    

    Then add a KeyHolderFactory field inside your JdbcRoleDAO, to be filled through dependency injection in production, and manually with a mock while testing. The production implementation is straightforward, and you can use a single instance of it (singleton):

    public class GeneratedKeyHolderFactory implements KeyHolderFactory {
         public KeyHolder newKeyHolder() {
             return new GeneratedKeyHolder();
         }
    }
    

    Inside the method you will have

    ...
    KeyHolder keyHolder = factory.newKeyHolder();
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need your help :/ I have a register that points to one memory
Need your help with my PHP/MYSQL array. I have a php script that selects
I need your help. I have a C executable called generator.out that is a
I need your help in solving the following 2 functions/problems: 1) I have to
I need your help. Say I have a code inside a for statement similar
need your help. I tried myself but cant solve it. I have a table
I need your help. I have been switching from various libraries trying to find
I need your help :) I have a EditText and below a ExpandableListView. I
I need your help :) I have a table in a database (SQL Server
Need your help on the below. I have a table where I have functions

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.