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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:44:15+00:00 2026-06-06T19:44:15+00:00

Suppose I have the following service object public class UserService { @Autowired private UserDao

  • 0

Suppose I have the following service object

public class UserService {

    @Autowired
    private UserDao dao;

    public void addUser(String username, String password) {
        if (username.length() < 8 ) {
            username = username  + "random" ; // add some random string
        }
        User user = new User(username, password);

        dao.save(user);
    }
}

I want to test the behaviour of the method “addUser” when username length is less 8 and when the username is more than 8 char. How do approach in unit test UserService.addUser(…), and verify it? I am aware using assert(), but the value “password” is not available outside the addUser(…) method.

I use JUnit and Mockito.

  • 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-06T19:44:16+00:00Added an answer on June 6, 2026 at 7:44 pm

    I came up a solution, after some re-visit the problem again after some months.

    The idea is to observed the object user that is being passed to UserDao. We can inspect the value of the username by doing this, hence the unit test code:

    @RunWith(MockitoJUnitRunner.class)
    public class UserServiceTest {
        @Mock
        private UserDao dao;
    
        @InjectMock
        private UserService service;
    
        @Test
        public void testAddingUserWithLessThan8CharUsername () {
            final String username = "some";
            final String password = "user";
            doAnswer(new Answer<Object>() {
                @Override
                public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
                    Object[] args = invocationOnMock.getArguments();
                    User toBeSaved = (User) args[0];
                    Assert.assertEquals(username + "random", toBeSaved.getPassword());
                    return null;
                }
            }).when(userDao).save(Matchers.any(User.class));
            service.addUser(username, password);
        }
    }
    

    Guillaume actually had the closest answer, but he answered using jMock. However, he gave me the idea on how to accomplish this, so I think he deserves some credit too.

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

Sidebar

Related Questions

Suppose I have following code package memoryleak; public class MemoryLeak { public static int
Suppose we have following packages in our project org.xxx.dao, org.xxx.service, org.xxx.service.impl all the package
I have the following JAX-RS service. @Path(config) public class ConfigurationResponder { @GET @Produces({application/json}) @Consumes({application/json})
Suppose I have the following methods declared in my web service: @WebMethod() public Long
Suppose I have these two objects: public class Object1 { string prop1; string prop2;
Suppose I have the following 2 web service code, I think I can do
Suppose i have following WCF Services. UtilityService (Service to provide utility functions) SomeOtherService 1
I have WCF service with IParameterInspector class which suppose to store the message for
Suppose we have a service with the following methods. [ServiceContract(SessionMode = SessionMode.Required, CallbackContract =
Suppose I have an asmx web service at the following address: http://localhost/BudgetWeb/Service.asmx This web

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.