#39 Added some unit tests
This commit is contained in:
86
src/test/java/de/rwu/easydrop/util/ConfigTest.java
Normal file
86
src/test/java/de/rwu/easydrop/util/ConfigTest.java
Normal file
@@ -0,0 +1,86 @@
|
||||
package de.rwu.easydrop.util;
|
||||
|
||||
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.fail;
|
||||
import static org.mockito.Mockito.spy;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import javax.naming.ConfigurationException;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
class ConfigTest {
|
||||
@Mock
|
||||
private FileInputStream mockFileInputStream;
|
||||
|
||||
private Config config;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws ConfigurationException {
|
||||
MockitoAnnotations.openMocks(this);
|
||||
config = spy(Config.getInstance());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetProperty_ExistingKey() throws NoSuchElementException {
|
||||
config.setProperty("API_KEY", "12345");
|
||||
|
||||
String value = config.getProperty("API_KEY");
|
||||
|
||||
assertEquals("12345", value);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetProperty_NonExistingKey() {
|
||||
assertThrows(NoSuchElementException.class, () -> config.getProperty("NON_EXISTING_KEY"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSetProperty() {
|
||||
config.setProperty("API_KEY", "12345");
|
||||
|
||||
assertEquals("12345", config.getProperty("API_KEY"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetInstanceNull() {
|
||||
config = null;
|
||||
|
||||
try {
|
||||
Config newConfig = Config.getInstance();
|
||||
// Check if the returned instance is not null
|
||||
assertNotNull(newConfig);
|
||||
} catch (ConfigurationException e) {
|
||||
fail("ConfigurationException should not be thrown.");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetInstanceNotNull() {
|
||||
try {
|
||||
Config newConfig = Config.getInstance();
|
||||
// Check if the returned instance is not null
|
||||
assertNotNull(newConfig);
|
||||
} catch (ConfigurationException e) {
|
||||
fail("ConfigurationException should not be thrown.");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testLoadConfigSuccessfully() {
|
||||
try {
|
||||
config.loadConfig();
|
||||
config.setProperty("WHATEVER", "SUCCESS");
|
||||
assertNotNull(config.getProperty("WHATEVER"));
|
||||
} catch (ConfigurationException e) {
|
||||
fail("ConfigurationException should not be thrown");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user