This commit is contained in:
Вячеслав Иванов 2024-05-03 15:49:36 +04:00
parent 19620712d4
commit a5e11f60ad
2 changed files with 20 additions and 17 deletions

View File

@ -1,12 +1,12 @@
package com.example.demo; package com.example.demo;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.DataIntegrityViolationException; import org.springframework.dao.DataIntegrityViolationException;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import com.example.demo.core.error.NotFoundException; import com.example.demo.core.error.NotFoundException;
import com.example.demo.product.model.ProductEntity; import com.example.demo.product.model.ProductEntity;
@ -17,6 +17,7 @@ import com.example.demo.types.service.TypeService;
import jakarta.transaction.Transactional; import jakarta.transaction.Transactional;
@SpringBootTest @SpringBootTest
@TestMethodOrder(OrderAnnotation.class)
class ProductsServiceTests { class ProductsServiceTests {
@Autowired @Autowired
private TypeService typeService; private TypeService typeService;
@ -28,7 +29,6 @@ class ProductsServiceTests {
private ProductEntity product; private ProductEntity product;
@BeforeEach
void createData() { void createData() {
removeData(); removeData();
@ -42,7 +42,6 @@ class ProductsServiceTests {
productService.create(new ProductEntity("test", type3, 450.50)); productService.create(new ProductEntity("test", type3, 450.50));
} }
@AfterEach
void removeData() { void removeData() {
productService.getAll(0L).forEach(item -> productService.delete(item.getId())); productService.getAll(0L).forEach(item -> productService.delete(item.getId()));
typeService.getAll().forEach(item -> typeService.delete(item.getId())); typeService.getAll().forEach(item -> typeService.delete(item.getId()));
@ -56,7 +55,8 @@ class ProductsServiceTests {
@Transactional @Transactional
@Test @Test
void createTest() { void createTest() {
Assertions.assertEquals(3, productService.getAll(0L).size()); Assertions.assertEquals(6, productService.getAll(0L).size());
product = productService.create(new ProductEntity("test1", type, 399.00));
Assertions.assertEquals(product, productService.get(product.getId())); Assertions.assertEquals(product, productService.get(product.getId()));
} }
@ -71,9 +71,10 @@ class ProductsServiceTests {
void updateTest() { void updateTest() {
final String test = "TEST"; final String test = "TEST";
final TypeEntity newType = typeService.create(new TypeEntity("Фреш")); final TypeEntity newType = typeService.create(new TypeEntity("Фреш"));
product = productService.create(new ProductEntity("te2st", type, 399.00));
final TypeEntity oldType = product.getType(); final TypeEntity oldType = product.getType();
final ProductEntity newEntity = productService.update(product.getId(), new ProductEntity(test, newType, 100.00)); final ProductEntity newEntity = productService.update(product.getId(), new ProductEntity(test, newType, 100.00));
Assertions.assertEquals(3, productService.getAll(0L).size());
Assertions.assertEquals(newEntity, productService.get(product.getId())); Assertions.assertEquals(newEntity, productService.get(product.getId()));
Assertions.assertEquals(test, newEntity.getName()); Assertions.assertEquals(test, newEntity.getName());
Assertions.assertEquals(newType, newEntity.getType()); Assertions.assertEquals(newType, newEntity.getType());
@ -82,11 +83,13 @@ class ProductsServiceTests {
@Test @Test
void deleteTest() { void deleteTest() {
type = typeService.create(new TypeEntity("yynrrnn"));
product = productService.create(new ProductEntity("te2st", type, 399.00));
productService.delete(product.getId()); productService.delete(product.getId());
Assertions.assertEquals(2, productService.getAll(0L).size()); Assertions.assertEquals(6, productService.getAll(0L).size());
final ProductEntity newEntity = productService.create(new ProductEntity(product.getName(), product.getType(), product.getPrice())); final ProductEntity newEntity = productService.create(new ProductEntity(product.getName(), product.getType(), product.getPrice()));
Assertions.assertEquals(3, typeService.getAll().size()); Assertions.assertEquals(7, typeService.getAll().size());
Assertions.assertNotEquals(product.getId(), newEntity.getId()); Assertions.assertNotEquals(product.getId(), newEntity.getId());
} }
} }

View File

@ -1,8 +1,6 @@
package com.example.demo; package com.example.demo;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder; import org.junit.jupiter.api.TestMethodOrder;
@ -22,16 +20,15 @@ class TypeServiceTests {
private TypeEntity type; private TypeEntity type;
@BeforeEach
void createData() { void createData() {
removeData();
type = typeService.create(new TypeEntity("Пепперони")); type = typeService.create(new TypeEntity("Пепперони"));
typeService.create(new TypeEntity("Мясная")); typeService.create(new TypeEntity("Мясная"));
typeService.create(new TypeEntity("4 сыра")); typeService.create(new TypeEntity("4 сыра"));
} }
@AfterEach
void removeData() { void removeData() {
typeService.getAll().forEach(item -> typeService.delete(item.getId())); typeService.getAll().forEach(item -> typeService.delete(item.getId()));
} }
@ -43,7 +40,8 @@ class TypeServiceTests {
@Test @Test
void createTest() { void createTest() {
Assertions.assertEquals(3, typeService.getAll().size()); type = typeService.create(new TypeEntity("thdgndgneth"));
Assertions.assertEquals(5, typeService.getAll().size());
Assertions.assertEquals(type, typeService.get(type.getId())); Assertions.assertEquals(type, typeService.get(type.getId()));
} }
@ -62,9 +60,10 @@ class TypeServiceTests {
@Test @Test
void updateTest() { void updateTest() {
final String test = "TEST"; final String test = "TEST";
type = typeService.create(new TypeEntity("ryjhjhrrnh"));
final String oldName = type.getName(); final String oldName = type.getName();
final TypeEntity newEntity = typeService.update(type.getId(), new TypeEntity(test)); final TypeEntity newEntity = typeService.update(type.getId(), new TypeEntity(test));
Assertions.assertEquals(3, typeService.getAll().size()); Assertions.assertEquals(4, typeService.getAll().size());
Assertions.assertEquals(newEntity, typeService.get(type.getId())); Assertions.assertEquals(newEntity, typeService.get(type.getId()));
Assertions.assertEquals(test, newEntity.getName()); Assertions.assertEquals(test, newEntity.getName());
Assertions.assertNotEquals(oldName, newEntity.getName()); Assertions.assertNotEquals(oldName, newEntity.getName());
@ -72,11 +71,12 @@ class TypeServiceTests {
@Test @Test
void deleteTest() { void deleteTest() {
type = typeService.create(new TypeEntity("gnnetnten"));
typeService.delete(type.getId()); typeService.delete(type.getId());
Assertions.assertEquals(2, typeService.getAll().size()); Assertions.assertEquals(5, typeService.getAll().size());
final TypeEntity newEntity = typeService.create(new TypeEntity(type.getName())); final TypeEntity newEntity = typeService.create(new TypeEntity(type.getName()));
Assertions.assertEquals(3, typeService.getAll().size()); Assertions.assertEquals(6, typeService.getAll().size());
Assertions.assertNotEquals(type.getId(), newEntity.getId()); Assertions.assertNotEquals(type.getId(), newEntity.getId());
} }
} }