Skip to content

Integrated Testing

ganmath edited this page Apr 21, 2024 · 1 revision
  1. Spring Boot project for Business Logic (Project 1)
  2. Spring Boot project for Selenium and Cucumber (Project 2)
  3. Angular project for UI (Project 3)

Project 1: Spring Boot for Business Logic

This project will focus on handling the business operations and data interactions.

Setup and Dependencies

  • Initialize the project using Spring Initializr or your preferred method, adding dependencies such as Spring Web, Spring Data JPA for database operations, and any other necessary business logic dependencies.

Directory Structure

  • src/main/java/ for your business logic
  • src/main/resources/ for configuration files
  • src/test/java/ for your testing code

Unit Tests and Mock Tests

  1. Add JUnit and Mockito Dependencies:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
  2. Write Unit Tests:

    • Create test cases in src/test/java/ to validate each component of your business logic using JUnit.
    • Example for a service class:
      @ExtendWith(SpringExtension.class)
      public class UserServiceTest {
          @Mock
          private UserRepository userRepository;
          
          @InjectMocks
          private UserService userService;
      
          @Test
          public void testFindUserById() {
              User user = new User(1L, "John Doe");
              Mockito.when(userRepository.findById(1L)).thenReturn(Optional.of(user));
              User found = userService.findUserById(1L);
              assertEquals("John Doe", found.getName());
          }
      }
  3. Mock Testing:

    • Use Mockito to mock dependencies in the tests to ensure each unit test is isolated from others.

Project 2: Spring Boot for Selenium and Cucumber

This project will handle all automated BDD tests using Selenium WebDriver and Cucumber.

Setup and Dependencies

  • Initialize a separate Spring Boot project.
  • Add Cucumber and Selenium Dependencies:
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-spring</artifactId>
        <version>YourCucumberVersion</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>YourSeleniumVersion</version>
        <scope>test</scope>
    </dependency>

Cucumber Setup

  1. Feature Files:

    • Place your feature files under src/test/resources/features.
  2. Step Definitions and Test Runner:

    • Organize step definitions under src/test/java/your/package/stepdefs.
    • Use the Spring context for dependency injection and setup web driver management.
  3. Configure Cucumber-Spring Integration:

    @RunWith(Cucumber.class)
    @CucumberOptions(
        features = "src/test/resources/features",
        glue = "your.package.stepdefs"
    )
    public class TestRunner {}

Project 3: Angular Project for UI

This project will be a standard Angular application handling the user interface.

Setup

  • Generate the project using Angular CLI: ng new your-project-name
  • Develop your components, services, and modules as required for the UI.

Integration and Testing

Ensure that each project is capable of running independently and has its own testing setup. For integration:

  • Backend (Project 1) should expose an API consumed by the Angular frontend (Project 3).
  • Test Automation (Project 2) should be configured to interact with either a staging environment or directly with the development build, ideally in a CI/CD pipeline to ensure tests are run against the most recent version of the backend and frontend.

This structure promotes separation of concerns, making it easier to manage and scale your projects independently while maintaining a robust testing environment across the development lifecycle.

Clone this wiki locally