Browse Source

stock service unit test

Daniel Bohry 1 year ago
parent
commit
5f99849dd6

+ 9 - 9
src/test/java/service/PortfolioServiceTest.java

@@ -51,10 +51,10 @@ public class PortfolioServiceTest {
     @Test
     public void shouldCreateNewPortfolio() {
         //when
-        Portfolio response = portfolioService.create();
+        Portfolio result = portfolioService.create();
 
         //then
-        assertNotNull(response.getId());
+        assertNotNull(result.getId());
     }
 
     @Test
@@ -63,10 +63,10 @@ public class PortfolioServiceTest {
         Portfolio portfolio = portfolioService.create();
 
         //when
-        Portfolio response = portfolioService.get(portfolio.getId());
+        Portfolio result = portfolioService.get(portfolio.getId());
 
         //then
-        assertNotNull(response.getId());
+        assertNotNull(result.getId());
     }
 
     @Test
@@ -76,10 +76,10 @@ public class PortfolioServiceTest {
         Portfolio portfolio2 = portfolioService.create();
 
         //when
-        List<Portfolio> response = portfolioService.getAll();
+        List<Portfolio> result = portfolioService.getAll();
 
         //then
-        assertEquals(2, response.size());
+        assertEquals(2, result.size());
     }
 
     @Test
@@ -94,11 +94,11 @@ public class PortfolioServiceTest {
                 .build();
 
         //when
-        Portfolio response = portfolioService.update(portfolio.getId(), List.of(newStock));
+        Portfolio result = portfolioService.update(portfolio.getId(), List.of(newStock));
 
         //then
-        assertNotNull(response.getId());
-        assertFalse(response.getStocks().isEmpty());
+        assertNotNull(result.getId());
+        assertFalse(result.getStocks().isEmpty());
     }
 
     @Test

+ 30 - 3
src/test/java/service/StockServiceTest.java

@@ -1,14 +1,20 @@
 package service;
 
 import com.danielbohry.stocks.App;
+import com.danielbohry.stocks.domain.Quote;
 import com.danielbohry.stocks.repository.StockRepository;
 import com.danielbohry.stocks.service.StockService;
 import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.test.context.ContextConfiguration;
 
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
 @SpringBootTest
 @ContextConfiguration(classes = {App.class})
 public class StockServiceTest {
@@ -23,9 +29,30 @@ public class StockServiceTest {
         service = new StockService(repository);
     }
 
-    @AfterEach
-    public void teardown() {
-        repository.deleteAll();
+    @Test
+    public void shouldGetStockByCode() {
+        //given
+        String code = "AAPL";
+        Quote expected = new Quote(code, "Apple Inc.", null, null);
+
+        //when
+        Quote result = service.getByCode(code);
+
+        //then
+        assertEquals(expected.getCode(), result.getCode());
+        assertEquals(expected.getName(), result.getName());
+    }
+
+    @Test
+    public void shouldGetStockByName() {
+        //given
+        String name = "West";
+
+        //when
+        List<Quote> result = service.get(name);
+
+        //then
+        assertEquals(19, result.size());
     }
 
 }