aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/dev/submelon/pantry/ItemRepositoryTests.java
blob: 1f890d8ab24bd4837f371e94574becfea073029f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package dev.submelon.pantry;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@TestInstance(Lifecycle.PER_CLASS)
@SpringBootTest
public class ItemRepositoryTests {
    @Autowired
    private PantryItemRepository itemRepository;

    @BeforeAll
    public void before() throws Exception {
        PantryItem pb = new PantryItem("Peanut Butter", "Crunchy", 14.0, "oz");
        PantryItem jelly = new PantryItem("Strawberry Preserves", "The best", 12.8, "oz");
        PantryItem bread = new PantryItem("Oatnut Bread", "Relatively healthy, right?", 10, "slices");
        assertNull(pb.getId());
        assertNull(jelly.getId());
        assertNull(bread.getId());
        this.itemRepository.save(pb);
        this.itemRepository.save(jelly);
        this.itemRepository.save(bread);
        assertNotNull(pb.getId());
        assertNotNull(jelly.getId());
        assertNotNull(bread.getId());
    }

    @Test
    public void testFetchData() {
        PantryItem pb = itemRepository.findByName("Peanut Butter").get();
        assertNotNull(pb);
        assertEquals(14.0, pb.getQuantity());
        Iterable<PantryItem> items = itemRepository.findAll();

        int count = 0;
        for (PantryItem item : items) {
            assertNotNull(item);
            count++;
        }

        assertEquals(3, count);
    }
}