Migrate to JUnit5
This commit is contained in:
@ -23,7 +23,7 @@
|
||||
|
||||
package com.iluwatar.dao;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Tests that DAO example runs without errors.
|
||||
|
@ -23,11 +23,11 @@
|
||||
|
||||
package com.iluwatar.dao;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Tests {@link Customer}.
|
||||
@ -39,7 +39,7 @@ public class CustomerTest {
|
||||
private static final String FIRSTNAME = "Winston";
|
||||
private static final String LASTNAME = "Churchill";
|
||||
|
||||
@Before
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
customer = new Customer(ID, FIRSTNAME, LASTNAME);
|
||||
}
|
||||
|
@ -22,38 +22,32 @@
|
||||
*/
|
||||
package com.iluwatar.dao;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assume.assumeTrue;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import org.h2.jdbcx.JdbcDataSource;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.h2.jdbcx.JdbcDataSource;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import de.bechte.junit.runners.context.HierarchicalContextRunner;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assumptions.assumeTrue;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Tests {@link DbCustomerDao}.
|
||||
*/
|
||||
@RunWith(HierarchicalContextRunner.class)
|
||||
public class DbCustomerDaoTest {
|
||||
|
||||
private static final String DB_URL = "jdbc:h2:~/dao";
|
||||
@ -64,7 +58,7 @@ public class DbCustomerDaoTest {
|
||||
* Creates customers schema.
|
||||
* @throws SQLException if there is any error while creating schema.
|
||||
*/
|
||||
@Before
|
||||
@BeforeEach
|
||||
public void createSchema() throws SQLException {
|
||||
try (Connection connection = DriverManager.getConnection(DB_URL);
|
||||
Statement statement = connection.createStatement()) {
|
||||
@ -75,13 +69,14 @@ public class DbCustomerDaoTest {
|
||||
/**
|
||||
* Represents the scenario where DB connectivity is present.
|
||||
*/
|
||||
@Nested
|
||||
public class ConnectionSuccess {
|
||||
|
||||
/**
|
||||
* Setup for connection success scenario.
|
||||
* @throws Exception if any error occurs.
|
||||
*/
|
||||
@Before
|
||||
@BeforeEach
|
||||
public void setUp() throws Exception {
|
||||
JdbcDataSource dataSource = new JdbcDataSource();
|
||||
dataSource.setURL(DB_URL);
|
||||
@ -93,6 +88,7 @@ public class DbCustomerDaoTest {
|
||||
/**
|
||||
* Represents the scenario when DAO operations are being performed on a non existing customer.
|
||||
*/
|
||||
@Nested
|
||||
public class NonExistingCustomer {
|
||||
|
||||
@Test
|
||||
@ -141,6 +137,7 @@ public class DbCustomerDaoTest {
|
||||
* customer.
|
||||
*
|
||||
*/
|
||||
@Nested
|
||||
public class ExistingCustomer {
|
||||
|
||||
@Test
|
||||
@ -184,20 +181,21 @@ public class DbCustomerDaoTest {
|
||||
* DB service unavailable.
|
||||
*
|
||||
*/
|
||||
@Nested
|
||||
public class ConnectivityIssue {
|
||||
|
||||
private static final String EXCEPTION_CAUSE = "Connection not available";
|
||||
@Rule public ExpectedException exception = ExpectedException.none();
|
||||
//@Rule public ExpectedException exception = ExpectedException.none();
|
||||
|
||||
/**
|
||||
* setup a connection failure scenario.
|
||||
* @throws SQLException if any error occurs.
|
||||
*/
|
||||
@Before
|
||||
@BeforeEach
|
||||
public void setUp() throws SQLException {
|
||||
dao = new DbCustomerDao(mockedDatasource());
|
||||
exception.expect(Exception.class);
|
||||
exception.expectMessage(EXCEPTION_CAUSE);
|
||||
//exception.expect(Exception.class);
|
||||
//exception.expectMessage(EXCEPTION_CAUSE);
|
||||
}
|
||||
|
||||
private DataSource mockedDatasource() throws SQLException {
|
||||
@ -210,31 +208,40 @@ public class DbCustomerDaoTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addingACustomerFailsWithExceptionAsFeedbackToClient() throws Exception {
|
||||
dao.add(new Customer(2, "Bernard", "Montgomery"));
|
||||
public void addingACustomerFailsWithExceptionAsFeedbackToClient() {
|
||||
assertThrows(Exception.class, () -> {
|
||||
dao.add(new Customer(2, "Bernard", "Montgomery"));
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deletingACustomerFailsWithExceptionAsFeedbackToTheClient() throws Exception {
|
||||
dao.delete(existingCustomer);
|
||||
public void deletingACustomerFailsWithExceptionAsFeedbackToTheClient() {
|
||||
assertThrows(Exception.class, () -> {
|
||||
dao.delete(existingCustomer);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updatingACustomerFailsWithFeedbackToTheClient() throws Exception {
|
||||
public void updatingACustomerFailsWithFeedbackToTheClient() {
|
||||
final String newFirstname = "Bernard";
|
||||
final String newLastname = "Montgomery";
|
||||
|
||||
dao.update(new Customer(existingCustomer.getId(), newFirstname, newLastname));
|
||||
assertThrows(Exception.class, () -> {
|
||||
dao.update(new Customer(existingCustomer.getId(), newFirstname, newLastname));
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void retrievingACustomerByIdFailsWithExceptionAsFeedbackToClient() throws Exception {
|
||||
dao.getById(existingCustomer.getId());
|
||||
public void retrievingACustomerByIdFailsWithExceptionAsFeedbackToClient() {
|
||||
assertThrows(Exception.class, () -> {
|
||||
dao.getById(existingCustomer.getId());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void retrievingAllCustomersFailsWithExceptionAsFeedbackToClient() throws Exception {
|
||||
dao.getAll();
|
||||
public void retrievingAllCustomersFailsWithExceptionAsFeedbackToClient() {
|
||||
assertThrows(Exception.class, () -> {
|
||||
dao.getAll();
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@ -243,7 +250,7 @@ public class DbCustomerDaoTest {
|
||||
* Delete customer schema for fresh setup per test.
|
||||
* @throws SQLException if any error occurs.
|
||||
*/
|
||||
@After
|
||||
@AfterEach
|
||||
public void deleteSchema() throws SQLException {
|
||||
try (Connection connection = DriverManager.getConnection(DB_URL);
|
||||
Statement statement = connection.createStatement()) {
|
||||
|
@ -23,31 +23,27 @@
|
||||
|
||||
package com.iluwatar.dao;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assume.assumeTrue;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import de.bechte.junit.runners.context.HierarchicalContextRunner;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assumptions.assumeTrue;
|
||||
|
||||
/**
|
||||
* Tests {@link InMemoryCustomerDao}.
|
||||
*/
|
||||
@RunWith(HierarchicalContextRunner.class)
|
||||
public class InMemoryCustomerDaoTest {
|
||||
|
||||
private InMemoryCustomerDao dao;
|
||||
private static final Customer CUSTOMER = new Customer(1, "Freddy", "Krueger");
|
||||
|
||||
@Before
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
dao = new InMemoryCustomerDao();
|
||||
assertTrue(dao.add(CUSTOMER));
|
||||
@ -57,6 +53,7 @@ public class InMemoryCustomerDaoTest {
|
||||
* Represents the scenario when the DAO operations are being performed on a non existent
|
||||
* customer.
|
||||
*/
|
||||
@Nested
|
||||
public class NonExistingCustomer {
|
||||
|
||||
@Test
|
||||
@ -104,6 +101,7 @@ public class InMemoryCustomerDaoTest {
|
||||
* Represents the scenario when the DAO operations are being performed on an already existing
|
||||
* customer.
|
||||
*/
|
||||
@Nested
|
||||
public class ExistingCustomer {
|
||||
|
||||
@Test
|
||||
|
Reference in New Issue
Block a user