Why this mocked StringBuilder always return the string "stringBuilder" ? Posibly confiusion with Junit mocks

35 Views Asked by At

Hello and sorry for my english. Im new in Junit and im trying to learning mocks and how it works, well, I have this test class with the next mocks:

    @DataJpaTest
    @AutoConfigureTestDatabase(connection = EmbeddedDatabaseConnection.H2)
    @ExtendWith(MockitoExtension.class)
    public class UserRepositoryTest {
        @Autowired
        private UserRepository userRepository;
        @Mock
        private SecureRandom secureRandom;
        @Mock
        private StringBuilder stringBuilder;
        @InjectMocks
        private StringUtilsApp stringUtilsApp;

As you can see im using StringUtilsApp, is a dependency and I inyect 2 mocks in it, This is the method im using in my test:

    @Component
    @RequiredArgsConstructor
    public class StringUtilsApp {
        private final SecureRandom secureRandom;
        private final StringBuilder stringBuilder;
    
        /**
         * Generates a random String
         *
         * @param length     the length of the String
         * @param characters the posibles characters of the String
         * @return a random String
         */
        public String randomStringGenerator(int length, String characters) {
            String charactersToUse = (characters != null && !characters.isEmpty()) ? characters : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
            for (int i = 0; i < length; i++) {
                int randomIndex = secureRandom.nextInt(charactersToUse.length());
                char randomChar = charactersToUse.charAt(randomIndex);
                stringBuilder.append(randomChar);
            }
            return stringBuilder.toString();
        }
    }

This is the method where im using the randomStringGenerator in my JUNIT test:

    private User getARandomUser() {
        return User.builder()
                .username(stringUtilsApp.randomStringGenerator(80,null))
               .password(stringUtilsApp.randomStringGenerator(200,null))
                .email(stringUtilsApp.randomStringGenerator(200, null))
                .role(Role.ADMIN)
                .build();
    }

The bean of StringBuilder that I inyect in the class StringUtils :

    @Configuration
    @RequiredArgsConstructor
    public class ApplicationConfig {
        private final UserRepository userRepository;
    
    
        @Bean
        @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
        public StringBuilder stringBuilder() {
            return new StringBuilder();
        }
    }

Well with all this context when I use my method randomStringGenerator in any method of my test the StringBuilder always return the string "stringBuilder" and only happend in my tests, if I use in other class of the app inyecting the dependency, the returning string is the random String that i wait:

Result using the method in any Junit test

Why is happend this? Is there something Im not taking into account? Thanks. PD: Any corrections or other good practices about my code are wellcoming :)

Im wating to know what is happening with this bean and whats the problem with the mock.

0

There are 0 best solutions below