Added/updated tests
This commit is contained in:
@@ -109,4 +109,11 @@ public final class Config {
|
|||||||
public void setProperty(final String key, final String value) {
|
public void setProperty(final String key, final String value) {
|
||||||
properties.setProperty(key, value);
|
properties.setProperty(key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resets the config's properties.
|
||||||
|
*/
|
||||||
|
public void reset() {
|
||||||
|
properties = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,33 +1,56 @@
|
|||||||
package de.rwu.easydrop.api.dto;
|
package de.rwu.easydrop.api.dto;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
class ProductDTOTest {
|
class ProductDTOTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testToString1() {
|
void constructor_SetsProductIdAndDataOrigin() {
|
||||||
ProductDTO product1 = new ProductDTO("12345", "Amazon");
|
// Arrange
|
||||||
product1.setMerchant("Merchant A");
|
String productId = "12345";
|
||||||
product1.setCurrentPrice(19.99);
|
String dataOrigin = "Amazon";
|
||||||
product1.setAvailable(true);
|
|
||||||
|
|
||||||
String expectedString1 = "ProductDTO{12345 from Merchant A (Amazon) at 19,99 € (available: yes)}";
|
// Act
|
||||||
String result1 = product1.toString();
|
ProductDTO productDTO = new ProductDTO(productId, dataOrigin);
|
||||||
|
|
||||||
assertEquals(expectedString1, result1);
|
// Assert
|
||||||
|
assertEquals(productId, productDTO.getProductId());
|
||||||
|
assertEquals(dataOrigin, productDTO.getDataOrigin());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testToString2() {
|
void gettersAndSetters_WorkAsExpected() {
|
||||||
ProductDTO product2 = new ProductDTO("67890", "eBay");
|
// Arrange
|
||||||
product2.setMerchant("Merchant B");
|
ProductDTO productDTO = new ProductDTO("12345", "Amazon");
|
||||||
product2.setCurrentPrice(9.99);
|
|
||||||
product2.setAvailable(false);
|
|
||||||
|
|
||||||
String expectedString2 = "ProductDTO{67890 from Merchant B (eBay) at 9,99 € (available: no)}";
|
// Act and Assert
|
||||||
String result2 = product2.toString();
|
assertEquals("12345", productDTO.getProductId());
|
||||||
|
assertEquals("Amazon", productDTO.getDataOrigin());
|
||||||
|
|
||||||
assertEquals(expectedString2, result2);
|
// Modify fields
|
||||||
|
productDTO.setProductId("54321");
|
||||||
|
productDTO.setDataOrigin("eBay");
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assertEquals("54321", productDTO.getProductId());
|
||||||
|
assertEquals("eBay", productDTO.getDataOrigin());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void defaultConstructor_SetsDefaultValues() {
|
||||||
|
// Act
|
||||||
|
ProductDTO productDTO = new ProductDTO(null, null);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assertNull(productDTO.getProductId());
|
||||||
|
assertNull(productDTO.getDataOrigin());
|
||||||
|
assertEquals(0.0, productDTO.getCurrentPrice());
|
||||||
|
assertNull(productDTO.getMerchant());
|
||||||
|
assertEquals(0.0, productDTO.getDeliveryPrice());
|
||||||
|
assertFalse(productDTO.isAvailable());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
75
src/test/java/de/rwu/easydrop/model/ProductTest.java
Normal file
75
src/test/java/de/rwu/easydrop/model/ProductTest.java
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
package de.rwu.easydrop.model;
|
||||||
|
|
||||||
|
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 org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class ProductTest {
|
||||||
|
@Test
|
||||||
|
void testToString1() {
|
||||||
|
Product product1 = new Product();
|
||||||
|
product1.setDataOrigin("Amazon");
|
||||||
|
product1.setProductId("12345");
|
||||||
|
product1.setMerchant("Merchant A");
|
||||||
|
product1.setCurrentPrice(19.99);
|
||||||
|
product1.setAvailable(true);
|
||||||
|
|
||||||
|
String expectedString1 = "Product: [12345 from Merchant A (Amazon) at 19,99 € (available: yes)]";
|
||||||
|
String result1 = product1.toString();
|
||||||
|
|
||||||
|
assertEquals(expectedString1, result1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testToString2() {
|
||||||
|
Product product2 = new Product();
|
||||||
|
product2.setDataOrigin("eBay");
|
||||||
|
product2.setProductId("67890");
|
||||||
|
product2.setMerchant("Merchant B");
|
||||||
|
product2.setCurrentPrice(9.99);
|
||||||
|
product2.setAvailable(false);
|
||||||
|
|
||||||
|
String expectedString2 = "Product: [67890 from Merchant B (eBay) at 9,99 € (available: no)]";
|
||||||
|
String result2 = product2.toString();
|
||||||
|
|
||||||
|
assertEquals(expectedString2, result2);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void gettersAndSetters_WorkAsExpected() {
|
||||||
|
// Arrange
|
||||||
|
Product product = new Product();
|
||||||
|
product.setDataOrigin("Amazon");
|
||||||
|
product.setProductId("12345");
|
||||||
|
product.setCurrentPrice(9.99);
|
||||||
|
product.setMerchant("Example Merchant");
|
||||||
|
product.setDeliveryPrice(2.50);
|
||||||
|
product.setAvailable(true);
|
||||||
|
|
||||||
|
// Act and Assert
|
||||||
|
assertEquals("Amazon", product.getDataOrigin());
|
||||||
|
assertEquals("12345", product.getProductId());
|
||||||
|
assertEquals(9.99, product.getCurrentPrice());
|
||||||
|
assertEquals("Example Merchant", product.getMerchant());
|
||||||
|
assertEquals(2.50, product.getDeliveryPrice());
|
||||||
|
assertTrue(product.isAvailable());
|
||||||
|
|
||||||
|
// Modify fields
|
||||||
|
product.setDataOrigin("eBay");
|
||||||
|
product.setProductId("54321");
|
||||||
|
product.setCurrentPrice(19.99);
|
||||||
|
product.setMerchant("New Merchant");
|
||||||
|
product.setDeliveryPrice(3.50);
|
||||||
|
product.setAvailable(false);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assertEquals("eBay", product.getDataOrigin());
|
||||||
|
assertEquals("54321", product.getProductId());
|
||||||
|
assertEquals(19.99, product.getCurrentPrice());
|
||||||
|
assertEquals("New Merchant", product.getMerchant());
|
||||||
|
assertEquals(3.50, product.getDeliveryPrice());
|
||||||
|
assertFalse(product.isAvailable());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package de.rwu.easydrop.util;
|
package de.rwu.easydrop.util;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
import static org.junit.jupiter.api.Assertions.fail;
|
import static org.junit.jupiter.api.Assertions.fail;
|
||||||
|
|
||||||
@@ -34,7 +35,9 @@ class ConfigImplTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testGetProperty_ConfigNotLoaded() {
|
void testGetProperty_ConfigNotLoaded() throws Exception {
|
||||||
|
config.reset();
|
||||||
|
|
||||||
NoSuchElementException exception = assertThrows(NoSuchElementException.class, () -> {
|
NoSuchElementException exception = assertThrows(NoSuchElementException.class, () -> {
|
||||||
config.getProperty(TESTDATA_KEY);
|
config.getProperty(TESTDATA_KEY);
|
||||||
});
|
});
|
||||||
@@ -92,4 +95,19 @@ class ConfigImplTest {
|
|||||||
|
|
||||||
assertEquals("Couldn't load required config file", exception.getMessage());
|
assertEquals("Couldn't load required config file", exception.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testReset() throws ConfigurationException {
|
||||||
|
config.setConfigLocation("src/test/resources/testdata.properties");
|
||||||
|
config.loadConfig();
|
||||||
|
|
||||||
|
assertNotNull(config.getProperty(TESTDATA_KEY));
|
||||||
|
config.reset();
|
||||||
|
|
||||||
|
NoSuchElementException exception = assertThrows(NoSuchElementException.class, () -> {
|
||||||
|
config.getProperty(TESTDATA_KEY);
|
||||||
|
});
|
||||||
|
|
||||||
|
assertEquals("Config has not been loaded", exception.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user