Skip to content

Commit

Permalink
Add unit test for demo
Browse files Browse the repository at this point in the history
Signed-off-by: Viet Nguyen Duc <[email protected]>
  • Loading branch information
VietND96 committed Dec 4, 2023
1 parent 42b444b commit a70c83d
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 3 deletions.
20 changes: 20 additions & 0 deletions test-libraries/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,26 @@
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public static String numberDecimalFormat(String input, String decimal, String ro

public static boolean isCreatable(String text) {
try {
createNumber(text);
return true;
Double number = createNumber(text);
return number != null;
} catch (Exception e) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.ndviet.library.math;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.ndviet.library.configuration.ConfigurationHelpers;

import java.util.Locale;
import java.util.stream.Stream;

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

public class MathHelpersTest {

@ParameterizedTest
@MethodSource("provideParametersForNumberDecimalFormat")
public void numberDecimalFormat(String input, String decimal, String roundingMode, String expected) {
String result = MathHelpers.numberDecimalFormat(input, decimal, roundingMode);
assertEquals(expected, result);
}

private static Stream<Arguments> provideParametersForNumberDecimalFormat() {
return Stream.of(
Arguments.of("1234.5678", "#.##", "HALF_UP", "1234.57"),
Arguments.of("invalid", "#.##", "HALF_UP", "invalid"),
Arguments.of("1234.5678", "#.##", null, "1234.57"),
Arguments.of("1234.5678", null, "HALF_UP", "1234.5678")
);
}

@ParameterizedTest
@CsvSource({
"1234.5678, true",
"0, true",
"-1234, true",
"1.23E3, true",
"invalid, false",
", false"
})
public void isCreatable(String input, boolean expected) {
assertEquals(expected, MathHelpers.isCreatable(input));
}

@ParameterizedTest
@CsvSource({
"'1234,5678', true",
"'-0,75', true",
})
public void isCreatable_different_locate(String input, boolean expected) {
try (MockedStatic<ConfigurationHelpers> mockHelpers = Mockito.mockStatic(ConfigurationHelpers.class)) {
mockHelpers.when(ConfigurationHelpers::getSystemLocale).thenReturn(Locale.FRANCE);
assertEquals(expected, MathHelpers.isCreatable(input));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public WebDriver createLocalDriver() {
public ChromeOptions getOptions() {
ChromeOptions options = new ChromeOptions();
List<String> listArgs = ConfigurationManager.getInstance().getListValues(SELENIUM_CHROME_ARGS);
options.addArguments(listArgs.toArray(new String[0]));
if (listArgs != null) {
listArgs.forEach(arg -> options.addArguments(arg));
}
LinkedHashMap listPrefs = ConfigurationManager.getInstance().getMapValues(SELENIUM_CHROME_PREFS);
if (listPrefs != null) {
listPrefs.forEach((key, value) -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.ndviet.library.webui.driver;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.ndviet.library.WebUI;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import java.time.Duration;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.ndviet.library.configuration.Constants.SELENIUM_BROWSER_TYPE;
import static org.ndviet.library.configuration.Constants.SELENIUM_WEB_DRIVER_TARGET;

public class WebUITest {

@Mock
private WebDriver mockDriver;

@BeforeAll
public static void setUpClass() {
System.setProperty(SELENIUM_WEB_DRIVER_TARGET, "local");
System.setProperty(SELENIUM_BROWSER_TYPE, "chrome");
}

@BeforeEach
public void setUp() {
try (MockedStatic<TargetFactory> mockFactory = Mockito.mockStatic(TargetFactory.class)) {
mockFactory.when(TargetFactory::createInstance).thenReturn(mockDriver);
}
}

@AfterEach
public void tearDown() {
mockDriver.close();
}

@Test
public void openBrowser_returnsDriver() {
mockDriver = WebUI.openBrowser();
assertNotNull(mockDriver);
assertTrue(mockDriver instanceof ChromeDriver);
}

@Test
public void openBrowser_withUrl_returnsDriver() {
String url = "https://demoqa.com/";
mockDriver = WebUI.openBrowser(url);
mockDriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
assertNotNull(mockDriver);
assertTrue(mockDriver.getTitle().equals("DEMOQA"));
}
}

0 comments on commit a70c83d

Please sign in to comment.