Skip to content

rnhc1000/springExpert

Repository files navigation

Roadmap to Spring Certification

I started working on Spring Professional Developer Certification and decide to dive into it through @devsuperior training to be better prepared to tackle this beast. There are 3 courses available, Spring Professional, Spring Expert and Spring Reference. This is how I played with the challenges of the Spring Expert with focus on Unit Tests using JUnit, Integration Tests using MockMVC and RestAssured and of course lots of code based on TDD.

Table of contents

Overview

Here you will find the code challenges proposed over the training using Java 21 and Spring Boot 3.X where the focus is how to implement automated tests, starting with unit tests and walking thru the application layers up to end end to end tests. Visit https://devsuperior.com.br/curso-java-spring-expert to know details of the training.


Requirements

  • Spring Boot project setup
  • Fundamentals of automated testing
  • TDD - Test Driven Development
  • JUnit
  • Java Vanilla tests
  • Factory pattern
  • Spring annotation for testing
  • JUnit fixtures
  • Unit tests
  • Mockito
  • MockBean
  • Repository tests
  • Service layer tests
  • Web layer tests
  • MockMvc
  • Integration tests
  • Data validation
  • Custom validation
  • Security, login, access control
  • OAuth2, JWT, Cors
  • Database queries
  • Spy Mock
  • Test coverage
  • Jacoco
  • API tests
  • RestAssured

Project Structure

  • docs
    • javadocs
  • src
    • main
    • java
      • br.dev.ferreiras.springExpert/challengeOne
        • config
        • controller
          • handlers
        • dto
        • entity
        • enums
        • mapper
        • repository
        • services
          • exceptions
    • resources
      • db.migration
      • certs
    • test

Howto Build and Run

- H2 Database : http://127.0.0.1:8080/h2-console
- profile active: dev
- service socket: 127.0.0.1:8080
- tweak a few knobs to get it up and running

Screenshot

Links

Built with

My Skills

Code Snippet

import java.util.List;

/**
 *
 * @author [email protected]
 * @version 1.1.12.01
 * @since 1.0
 *
 */

ExtendWith(SpringExtension.class)
@ContextConfiguration
public class UserServiceTests {

  @InjectMocks
  private UserService userService;

  @Mock
  private UserRepository userRepository;

  @Mock
  private CustomUserUtil userUtil;

  private String existingUsername, nonExistingUsername;
  private UserEntity userEntity;
  private List<UserDetailsProjection> userDetails;

  @BeforeEach
  void setUp() throws Exception {

    existingUsername = "[email protected]";
    nonExistingUsername = "[email protected]";
    userEntity = UserFactory.createUserEntity();
    userDetails = UserDetailsFactory.createCustomAdminClientUser(existingUsername);

    Mockito.when(userRepository.findByUsername(existingUsername)).thenReturn(Optional.of(userEntity));
    Mockito.when(userRepository.findByUsername(nonExistingUsername)).thenThrow(UsernameNotFoundException.class);

    Mockito.when(userRepository.searchUserAndRolesByUsername(existingUsername)).thenReturn(userDetails);
    Mockito.when(userRepository.searchUserAndRolesByUsername(nonExistingUsername)).thenReturn(new ArrayList<>());

  }

  @Test
  public void authenticatedShouldReturnUserEntityWhenUserExists() {

    Mockito.when(userUtil.getLoggedUsername()).thenReturn(existingUsername);
    UserEntity user = userService.authenticated();

    Assertions.assertNotNull(user);
    Assertions.assertEquals(user.getUsername(), existingUsername);

  }

  @Test
  public void authenticatedShouldThrowUsernameNotFoundExceptionWhenUserDoesNotExists() {

    Mockito.doThrow(ClassCastException.class).when(userUtil).getLoggedUsername();

    Assertions.assertThrows(UsernameNotFoundException.class, () -> {
      userService.authenticated();
    });

  }

  @Test
  public void loadUserByUsernameShouldReturnUserDetailsWhenUserExists() {

    UserDetails user = userService.loadUserByUsername(existingUsername);

    Assertions.assertNotNull(user);
    Assertions.assertEquals(user.getUsername(), existingUsername);

  }

  @Test
  public void loadUserByUsernameShouldThrowUsernameNotFoundExceptionWhenUserDoesNotExists() {

    Assertions.assertThrows(UsernameNotFoundException.class, () -> {
      userService.loadUserByUsername(nonExistingUsername);
    });

  }
}

Continued development

  • Unit Tests -OK
  • Provide a Json to FrontEnd including
    • delivery status of each operation to frontend - OK
    • count of operations consumed by subscriber - OK
  • Subscriber Authentication - OK
    • Spring JWT-OAuth2 - OK
  • Records Pagination - OK

Useful resources

Author

Ricardo Ferreira

- Portfolio

My Portfolio...

About

Roadmap to Spring Professional Developer Certification

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages