Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Test generated by RoostGPT for test endtoendunit using AI Type Open AI and AI Model gpt-4

package com.bootexample4.products.controller;

import com.bootexample4.products.model.Product;
import com.bootexample4.products.repository.ProductRepository;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;

public class ProductController_createProduct_36b748883e_Test {

@InjectMocks
ProductController productController;

@Mock
ProductRepository productRepository;

@BeforeEach
public void init() {
MockitoAnnotations.initMocks(this);
}

@Test
public void testCreateProduct_Success() {
Product product = new Product();
product.setId(1L); // changed from int to Long
product.setName("Test Product");
product.setDescription("Test Description");
product.setPrice(100.0);

when(productRepository.save(any(Product.class))).thenReturn(product);

Product createdProduct = productController.createProduct(product);

assertEquals(product.getId(), createdProduct.getId());
assertEquals(product.getName(), createdProduct.getName());
assertEquals(product.getDescription(), createdProduct.getDescription());
assertEquals(product.getPrice(), createdProduct.getPrice());

verify(productRepository, times(1)).save(product);
}

@Test
public void testCreateProduct_Failure() {
Product product = new Product();
product.setId(1L); // changed from int to Long
product.setName("Test Product");
product.setDescription("Test Description");
product.setPrice(100.0);

when(productRepository.save(any(Product.class))).thenReturn(null);

Product createdProduct = productController.createProduct(product);

assertEquals(null, createdProduct);

verify(productRepository, times(1)).save(product);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Test generated by RoostGPT for test endtoendunit using AI Type Open AI and AI Model gpt-4

package com.bootexample4.products.controller;

import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.when;
import static org.mockito.ArgumentMatchers.any;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.Optional;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.http.ResponseEntity;

import com.bootexample4.products.model.Product;
import com.bootexample4.products.repository.ProductRepository;

public class ProductController_deleteProduct_dcaff736d4_Test {

@InjectMocks
private ProductController productController;

@Mock
private ProductRepository productRepository;

@BeforeEach
public void init() {
MockitoAnnotations.initMocks(this);
}

@Test
public void testDeleteProduct_Success() {
Product product = new Product();
product.setId(1L);

when(productRepository.findById(any(Long.class))).thenReturn(Optional.of(product));
doNothing().when(productRepository).delete(product);

ResponseEntity<Object> responseEntity = productController.deleteProduct(1L);

assertEquals(200, responseEntity.getStatusCodeValue());
}

@Test
public void testDeleteProduct_NotFound() {
when(productRepository.findById(any(Long.class))).thenReturn(Optional.empty());

ResponseEntity<Object> responseEntity = productController.deleteProduct(1L);

assertEquals(404, responseEntity.getStatusCodeValue());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.bootexample4.products.controller;

import com.bootexample4.products.model.Product;
import com.bootexample4.products.repository.ProductRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.when;

import java.util.Arrays;
import java.util.List;

public class ProductController_getAllProducts_7e38cc05f6_Test {

@InjectMocks
ProductController productController;

@Mock
ProductRepository productRepository;

@BeforeEach
public void init() {
MockitoAnnotations.initMocks(this);
}

@Test
public void testGetAllProducts() {
Product product1 = new Product();
product1.setId(1L);
product1.setName("Product1");

Product product2 = new Product();
product2.setId(2L);
product2.setName("Product2");

List<Product> products = Arrays.asList(product1, product2);

when(productRepository.findAll()).thenReturn(products);

List<Product> result = productController.getAllProducts();
assertEquals(2, result.size());
}

@Test
public void testGetAllProducts_empty() {
when(productRepository.findAll()).thenReturn(Arrays.asList());

List<Product> result = productController.getAllProducts();
assertEquals(0, result.size());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Test generated by RoostGPT for test endtoendunit using AI Type Open AI and AI Model gpt-4

package com.bootexample4.products.controller;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import static org.mockito.Mockito.when;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpStatus;

import com.bootexample4.products.model.Product;
import com.bootexample4.products.repository.ProductRepository;

import java.util.Optional;

@ExtendWith(MockitoExtension.class)
public class ProductController_getProductById_d22f3ea272_Test {

@InjectMocks
private ProductController productController;

@Mock
private ProductRepository productRepository;

@Test
public void testGetProductById_Found() {
Long id = 1L; // TODO: Change the value to the ID of a product that exists in your database.
Product product = new Product();
product.setId(id);
// TODO: Add more properties to the product as needed.

when(productRepository.findById(id)).thenReturn(Optional.of(product));

ResponseEntity<Product> response = productController.getProductById(id);

assertEquals(HttpStatus.OK, response.getStatusCode());
assertEquals(product, response.getBody());
}

@Test
public void testGetProductById_NotFound() {
Long id = 2L; // TODO: Change the value to the ID of a product that does not exist in your database.

when(productRepository.findById(id)).thenReturn(Optional.empty());

ResponseEntity<Product> response = productController.getProductById(id);

assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
assertTrue(response.getBody() == null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.bootexample4.products.controller;

import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyLong;

import com.bootexample4.products.model.Product;
import com.bootexample4.products.repository.ProductRepository;
import org.springframework.http.ResponseEntity;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

@SpringBootTest
public class ProductController_updateProduct_9454a9af90_Test {

@Autowired
private ProductController productController;

@MockBean
private ProductRepository productRepository;

private Product existingProduct;

@BeforeEach
public void setup() {
existingProduct = new Product();
existingProduct.setId(1L);
existingProduct.setName("Test Product");
existingProduct.setDescription("Test Description");
existingProduct.setPrice(100.00);
}

@Test
public void testUpdateProduct_Success() {
Product newProduct = new Product();
newProduct.setName("Updated Product");
newProduct.setDescription("Updated Description");
newProduct.setPrice(200.00);

when(productRepository.findById(anyLong())).thenReturn(Optional.of(existingProduct));
when(productRepository.save(any(Product.class))).thenReturn(newProduct);

ResponseEntity<Product> response = productController.updateProduct(1L, newProduct);

assertEquals(200, response.getStatusCodeValue());
assertEquals(newProduct, response.getBody());
}

@Test
public void testUpdateProduct_NotFound() {
Product newProduct = new Product();
newProduct.setName("Updated Product");
newProduct.setDescription("Updated Description");
newProduct.setPrice(200.00);

when(productRepository.findById(anyLong())).thenReturn(Optional.empty());

ResponseEntity<Product> response = productController.updateProduct(1L, newProduct);

assertEquals(404, response.getStatusCodeValue());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Test generated by RoostGPT for test endtoendunit using AI Type Open AI and AI Model gpt-4

package com.bootexample4.products.model;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.MockitoAnnotations;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class Product_getDescription_b1844ea396_Test {

@InjectMocks
private Product product;

@BeforeEach
public void setup() {
MockitoAnnotations.openMocks(this);
}

@Test
public void testGetDescription_Success() {
String expectedDescription = "Test Product Description";
product.setDescription(expectedDescription);
String actualDescription = product.getDescription();
assertEquals(expectedDescription, actualDescription, "Expected and actual descriptions should match");
}

@Test
public void testGetDescription_Null() {
product.setDescription(null);
String actualDescription = product.getDescription();
assertEquals(null, actualDescription, "Description should be null");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.bootexample4.products.model;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

public class Product_getId_ba349b1eff_Test {

private Product product;

@BeforeEach
public void setUp() {
product = new Product();
}

@Test
public void testGetIdSuccess() {
Long expectedId = 1L;
product.setId(expectedId);
Long actualId = product.getId();
assertEquals(expectedId, actualId, "The expected ID does not match the actual ID");
}

@Test
public void testGetIdFailure() {
Long unexpectedId = 2L;
product.setId(1L);
Long actualId = product.getId();
assertNotEquals(unexpectedId, actualId, "The unexpected ID should not match the actual ID");
}
}
Loading