63 lines
2.1 KiB
Java

package com.example.demo;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.example.demo.core.error.NotFoundException;
import com.example.demo.stocks.model.StockEntity;
import com.example.demo.stocks.service.StockService;
@SpringBootTest
@TestMethodOrder(OrderAnnotation.class)
class StockServiceTests {
@Autowired
private StockService stockService;
@Test
void getTest() {
Assertions.assertThrows(NotFoundException.class, () -> stockService.get(0L));
}
@Test
@Order(1)
void createTest() {
stockService.create(new StockEntity(null, "Stock 1", 50));
stockService.create(new StockEntity(null, "Stock 2", 0));
final StockEntity last = stockService.create(new StockEntity(null, "Stock 3", 25));
Assertions.assertEquals(3, stockService.getAll().size());
Assertions.assertEquals(last, stockService.get(3L));
}
@Test
@Order(2)
void updateTest() {
final String test = "TEST";
final Integer valueTest = 15;
final StockEntity entity = stockService.get(3L);
final String oldName = entity.getName();
final StockEntity newEntity = stockService.update(3L, new StockEntity(1L, test, valueTest));
Assertions.assertEquals(3, stockService.getAll().size());
Assertions.assertEquals(newEntity, stockService.get(3L));
Assertions.assertEquals(test, newEntity.getName());
Assertions.assertNotEquals(oldName, newEntity.getName());
}
@Test
@Order(3)
void deleteTest() {
stockService.delete(3L);
Assertions.assertEquals(2, stockService.getAll().size());
final StockEntity last = stockService.get(2L);
Assertions.assertEquals(2L, last.getId());
final StockEntity newEntity = stockService.create(new StockEntity(null, "TEST", 15));
Assertions.assertEquals(3, stockService.getAll().size());
Assertions.assertEquals(4L, newEntity.getId());
}
}