Skip to content

Improve test cases with better assertions and add tests for non-exist… #132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
Expand Up @@ -15,8 +15,7 @@

import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;

@DataMongoTest
@Testcontainers
Expand Down Expand Up @@ -45,24 +44,31 @@ public void testAddAccount() {
a.setBalance(1232);
a.setCustomerId("234353464576586464");
a = repository.save(a);
assertNotNull(a);
assertNotNull(a.getId());
assertNotNull(a, "Account should not be null after saving");
assertNotNull(a.getId(), "Account ID should not be null after saving");
id = a.getId();
}

@Test
@Order(2)
public void testFindAccount() {
Optional<Account> optAcc = repository.findById(id);
assertTrue(optAcc.isPresent());
assertTrue(optAcc.isPresent(), "Account should be found by ID");
assertEquals("12345678909", optAcc.get().getNumber(), "Account number should match");
}

@Test
@Order(2)
@Order(3)
public void testFindAccountByNumber() {
Account a = repository.findByNumber("12345678909");
assertNotNull(a);
assertNotNull(a.getId());
assertNotNull(a, "Account should not be null when found by number");
assertEquals("12345678909", a.getNumber(), "Account number should match");
}

@Test
@Order(4)
public void testFindNonExistentAccount() {
Optional<Account> optAcc = repository.findById("non-existent-id");
assertFalse(optAcc.isPresent(), "Account should not be found for a non-existent ID");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
import static io.specto.hoverfly.junit.dsl.HoverflyDsl.service;
import static io.specto.hoverfly.junit.dsl.ResponseCreators.success;
import static io.specto.hoverfly.junit.dsl.matchers.HoverflyMatchers.startsWith;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
properties = {
Expand Down Expand Up @@ -59,19 +58,25 @@ public void addCustomerTest() {
c.setPesel("1234567890");
c.setName("Jan Testowy");
c = template.postForObject("/customers", c, Customer.class);
assertNotNull(c, "Customer should not be null after being added");
assertNotNull(c.getId(), "Customer ID should not be null after being added");
id = c.getId();
}

// @Test
// @Order(2)
@Test
@Order(2)
public void findCustomerWithAccounts(Hoverfly hoverfly) {
hoverfly.simulate(
dsl(service("http://account-service")
.get(startsWith("/accounts/customer"))
.willReturn(success("[{\"id\":\"1\",\"number\":\"1234567890\"}]", "application/json"))));

Customer c = template.getForObject("/customers/pesel/{pesel}", Customer.class, "1234567890");
assertNotNull(c, "Customer should not be null when fetched by PESEL");
assertEquals("1234567890", c.getPesel(), "Customer PESEL should match");

Customer cc = template.getForObject("/customers/{id}", Customer.class, c.getId());
assertNotNull(cc);
assertTrue(cc.getAccounts().size() > 0);
assertNotNull(cc, "Customer should not be null when fetched by ID");
assertTrue(cc.getAccounts().size() > 0, "Customer should have associated accounts");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@

import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;

@DataMongoTest
@Testcontainers
Expand Down Expand Up @@ -47,24 +46,31 @@ public void testAddCustomer() {
c.setPesel("1234567890");
c.setType(CustomerType.INDIVIDUAL);
c = repository.save(c);
assertNotNull(c);
assertNotNull(c.getId());
assertNotNull(c, "Customer should not be null after saving");
assertNotNull(c.getId(), "Customer ID should not be null after saving");
id = c.getId();
}

@Test
@Order(2)
public void testFindCustomer() {
Optional<Customer> optCus = repository.findById(id);
assertTrue(optCus.isPresent());
assertTrue(optCus.isPresent(), "Customer should be found by ID");
assertEquals("Test1", optCus.get().getName(), "Customer name should match");
}

@Test
@Order(2)
@Order(3)
public void testFindCustomerByPesel() {
Customer c = repository.findByPesel("1234567890");
assertNotNull(c);
assertNotNull(c.getId());
assertNotNull(c, "Customer should not be null when found by PESEL");
assertEquals("1234567890", c.getPesel(), "Customer PESEL should match");
}

@Test
@Order(4)
public void testFindNonExistentCustomer() {
Optional<Customer> optCus = repository.findById("non-existent-id");
assertFalse(optCus.isPresent(), "Customer should not be found for a non-existent ID");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
import java.time.LocalDate;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;

@DataMongoTest
@Testcontainers
Expand Down Expand Up @@ -50,23 +49,31 @@ public void testAddProduct() {
p.setBalance(10000);
p.setDateOfStart(LocalDate.now());
p = repository.save(p);
assertNotNull(p);
assertNotNull(p.getId());
assertNotNull(p, "Product should not be null after saving");
assertNotNull(p.getId(), "Product ID should not be null after saving");
id = p.getId();
}

@Test
@Order(2)
public void testFindProduct() {
Optional<Product> optCus = repository.findById(id);
assertTrue(optCus.isPresent());
Optional<Product> optProd = repository.findById(id);
assertTrue(optProd.isPresent(), "Product should be found by ID");
assertEquals("123", optProd.get().getAccountId(), "Product account ID should match");
}

@Test
@Order(2)
@Order(3)
public void testFindProductByAccountId() {
Product p = repository.findByAccountId("123");
assertNotNull(p);
assertNotNull(p.getId());
assertNotNull(p, "Product should not be null when found by account ID");
assertEquals("123", p.getAccountId(), "Product account ID should match");
}

@Test
@Order(4)
public void testFindNonExistentProduct() {
Optional<Product> optProd = repository.findById("non-existent-id");
assertFalse(optProd.isPresent(), "Product should not be found for a non-existent ID");
}
}