Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit test generated by RoostGPT #105

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 113 additions & 50 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,50 +1,113 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.bootexample4</groupId>
<artifactId>products</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>products</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.5</version>
<relativePath/>
<!-- lookup parent from repository -->
</parent>
<groupId>com.bootexample4</groupId>
<artifactId>products</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>products</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.spring.javaformat</groupId>
<artifactId>spring-javaformat-formatter</artifactId>
<version>0.0.40</version>
<!--Dependency added by RoostGPT-->
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.10</version>
<scope>compile</scope>
<!--Dependency added by RoostGPT-->
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.7</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<outputDirectory>coverageReport</outputDirectory>
</configuration>
</execution>
</executions>
<!--Plugin added by RoostGPT-->
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
<!--Plugin added by RoostGPT-->
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>3.2.5</version>
<configuration>
<outputDirectory>testReport</outputDirectory>
</configuration>
<!--Plugin added by RoostGPT-->
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>2.1</version>
<configuration>
<outputDirectory>testReport</outputDirectory>
</configuration>
<!--Plugin added by RoostGPT-->
</plugin>
<plugin>
<groupId>io.spring.javaformat</groupId>
<artifactId>spring-javaformat-maven-plugin</artifactId>
<version>0.0.40</version>
<!--Plugin added by RoostGPT-->
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@

// ********RoostGPT********
/*
Test generated by RoostGPT for test java-myproducts using AI Type Azure Open AI and AI Model roostgpt-4-32k

ROOST_METHOD_HASH=createProduct_60409495d0
ROOST_METHOD_SIG_HASH=createProduct_5b0158b3eb

Scenario 1: Testing creation of a product with valid data

Details:
TestName: createProductWithValidData()
Description: The test aims to check if a product is successfully created when supplied with valid data, conforming to the expected field requirements.

Execution:
Arrange: Create a mock product with all necessary details (name, description, price, etc.). Mock the product repository to return the product when save() is called.
Act: Call createProduct method with the mock product.
Assert: Using JUnit assertions to check if the returned product is not null and matches the mock product.

Validation:
Asserting here aims to verify that the method can successfully create a product with valid data as per the defined business logic. This scenario is significant as it validates the basic creation functionality of the product.

Scenario 2: Testing creation of a product with null data

Details:
TestName: createProductWithNullData()
Description: The test aims to check the scenario when a product creation is attempted with null data.

Execution:
Arrange: Mock the product repository to return null when save() is called with null.
Act: Call createProduct method with null.
Assert: Using JUnit assertions to check if the returned product is null.

Validation:
This assertion verifies that the method appropriately handles null data and does not create a product, meeting necessary validity checks. This scenario is significant as it validates the application's robustness in handling invalid input.

Scenario 3: Error Exception when Repository is unavailable

Details:
TestName: createProductWhenRepositoryUnavailable()
Description: The test aims to check the scenario when an error is expected due to unavailable product repository.

Execution:
Arrange: Mock the product repository to throw a RuntimeException when save() is called.
Act: Call createProduct method with a valid product.
Assert: Using JUnit assertions to check if a RuntimeException is thrown.

Validation:
Asserting here aims to verify that in case of underlying system unavailability (like Database), the method throws an exception as per the application's exception handling and recovery mechanism. This scenario is significant as it validates system stability under crucial circumstances.

Scenario 4: Creating a product that already exists in the repository

Details:
TestName: createProductThatAlreadyExists()
Description: The test aims to check the scenario when a product already present in the repository is being created again.

Execution:
Arrange: Mock the product and save it to the repository.
Act: Call createProduct method with the same product.
Assert: Using JUnit assertions to check if a DuplicateResourceException or an equivalent is thrown or handled.

Validation:
This assertion verifies that the code prevents duplication of products in the repository as per the business rules. This scenario is significant as it ensures the application's integrity of data.
*/

// ********RoostGPT********

package com.bootexample4.products.controller;

import com.bootexample4.products.model.Product;
import com.bootexample4.products.repository.ProductRepository;
import static org.mockito.Mockito.when;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.junit.jupiter.api.*;
import java.util.List;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

class ProductControllerCreateProductTest {

@Autowired
@InjectMocks
private ProductController productController;

@Mock
private ProductRepository productRepository;

private Product product;

@BeforeEach
void setUp() {
MockitoAnnotations.initMocks(this);
product = new Product();
product.setName("Product1");
product.setDescription("Product 1 description");
product.setPrice(99.0);
}

@Test
@Tag("valid")
void createProductWithValidData() {
when(productRepository.save(product)).thenReturn(product);
Product savedProduct = productController.createProduct(product);
assertEquals(product, savedProduct);
}

@Test
@Tag("invalid")
void createProductWithNullData() {
when(productRepository.save(null)).thenReturn(null);
Product savedProduct = productController.createProduct(null);
assertNull(savedProduct);
}

@Test
@Tag("integration")
void createProductWhenRepositoryUnavailable() {
when(productRepository.save(product)).thenThrow(new RuntimeException("Repository Unavailable"));
Exception exception = assertThrows(RuntimeException.class, () -> productController.createProduct(product));
assertEquals("Repository Unavailable", exception.getMessage());
}

@Test
@Tag("boundary")
void createProductThatAlreadyExists() {
when(productRepository.save(product)).thenReturn(product);
Product savedProduct = productController.createProduct(product);
when(productRepository.save(savedProduct)).thenThrow(new RuntimeException("Duplicate Resource"));
Exception exception = assertThrows(RuntimeException.class, () -> productController.createProduct(savedProduct));
assertEquals("Duplicate Resource", exception.getMessage());
}

}
Loading