Skip to content

Commit a70c83d

Browse files
committed
Add unit test for demo
Signed-off-by: Viet Nguyen Duc <[email protected]>
1 parent 42b444b commit a70c83d

File tree

5 files changed

+142
-3
lines changed

5 files changed

+142
-3
lines changed

test-libraries/pom.xml

+20
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,26 @@
3131
<groupId>org.apache.logging.log4j</groupId>
3232
<artifactId>log4j-api</artifactId>
3333
</dependency>
34+
<dependency>
35+
<groupId>org.junit.jupiter</groupId>
36+
<artifactId>junit-jupiter-api</artifactId>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.junit.jupiter</groupId>
40+
<artifactId>junit-jupiter-engine</artifactId>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.junit.jupiter</groupId>
44+
<artifactId>junit-jupiter-params</artifactId>
45+
</dependency>
46+
<dependency>
47+
<groupId>org.mockito</groupId>
48+
<artifactId>mockito-core</artifactId>
49+
</dependency>
50+
<dependency>
51+
<groupId>org.mockito</groupId>
52+
<artifactId>mockito-inline</artifactId>
53+
</dependency>
3454
</dependencies>
3555

3656
</project>

test-libraries/test-libraries-utilities/src/main/java/org/ndviet/library/math/MathHelpers.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ public static String numberDecimalFormat(String input, String decimal, String ro
4848

4949
public static boolean isCreatable(String text) {
5050
try {
51-
createNumber(text);
52-
return true;
51+
Double number = createNumber(text);
52+
return number != null;
5353
} catch (Exception e) {
5454
return false;
5555
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package org.ndviet.library.math;
2+
3+
import org.junit.jupiter.params.ParameterizedTest;
4+
import org.junit.jupiter.params.provider.Arguments;
5+
import org.junit.jupiter.params.provider.CsvSource;
6+
import org.junit.jupiter.params.provider.MethodSource;
7+
import org.mockito.MockedStatic;
8+
import org.mockito.Mockito;
9+
import org.ndviet.library.configuration.ConfigurationHelpers;
10+
11+
import java.util.Locale;
12+
import java.util.stream.Stream;
13+
14+
import static org.junit.jupiter.api.Assertions.assertEquals;
15+
16+
public class MathHelpersTest {
17+
18+
@ParameterizedTest
19+
@MethodSource("provideParametersForNumberDecimalFormat")
20+
public void numberDecimalFormat(String input, String decimal, String roundingMode, String expected) {
21+
String result = MathHelpers.numberDecimalFormat(input, decimal, roundingMode);
22+
assertEquals(expected, result);
23+
}
24+
25+
private static Stream<Arguments> provideParametersForNumberDecimalFormat() {
26+
return Stream.of(
27+
Arguments.of("1234.5678", "#.##", "HALF_UP", "1234.57"),
28+
Arguments.of("invalid", "#.##", "HALF_UP", "invalid"),
29+
Arguments.of("1234.5678", "#.##", null, "1234.57"),
30+
Arguments.of("1234.5678", null, "HALF_UP", "1234.5678")
31+
);
32+
}
33+
34+
@ParameterizedTest
35+
@CsvSource({
36+
"1234.5678, true",
37+
"0, true",
38+
"-1234, true",
39+
"1.23E3, true",
40+
"invalid, false",
41+
", false"
42+
})
43+
public void isCreatable(String input, boolean expected) {
44+
assertEquals(expected, MathHelpers.isCreatable(input));
45+
}
46+
47+
@ParameterizedTest
48+
@CsvSource({
49+
"'1234,5678', true",
50+
"'-0,75', true",
51+
})
52+
public void isCreatable_different_locate(String input, boolean expected) {
53+
try (MockedStatic<ConfigurationHelpers> mockHelpers = Mockito.mockStatic(ConfigurationHelpers.class)) {
54+
mockHelpers.when(ConfigurationHelpers::getSystemLocale).thenReturn(Locale.FRANCE);
55+
assertEquals(expected, MathHelpers.isCreatable(input));
56+
}
57+
}
58+
}

test-libraries/test-libraries-webui/src/main/java/org/ndviet/library/webui/driver/BrowserFactory.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ public WebDriver createLocalDriver() {
3131
public ChromeOptions getOptions() {
3232
ChromeOptions options = new ChromeOptions();
3333
List<String> listArgs = ConfigurationManager.getInstance().getListValues(SELENIUM_CHROME_ARGS);
34-
options.addArguments(listArgs.toArray(new String[0]));
34+
if (listArgs != null) {
35+
listArgs.forEach(arg -> options.addArguments(arg));
36+
}
3537
LinkedHashMap listPrefs = ConfigurationManager.getInstance().getMapValues(SELENIUM_CHROME_PREFS);
3638
if (listPrefs != null) {
3739
listPrefs.forEach((key, value) -> {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package org.ndviet.library.webui.driver;
2+
3+
import org.junit.jupiter.api.AfterEach;
4+
import org.junit.jupiter.api.BeforeAll;
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
7+
import org.mockito.Mock;
8+
import org.mockito.MockedStatic;
9+
import org.mockito.Mockito;
10+
import org.ndviet.library.WebUI;
11+
import org.openqa.selenium.WebDriver;
12+
import org.openqa.selenium.chrome.ChromeDriver;
13+
14+
import java.time.Duration;
15+
16+
import static org.junit.jupiter.api.Assertions.assertNotNull;
17+
import static org.junit.jupiter.api.Assertions.assertTrue;
18+
import static org.ndviet.library.configuration.Constants.SELENIUM_BROWSER_TYPE;
19+
import static org.ndviet.library.configuration.Constants.SELENIUM_WEB_DRIVER_TARGET;
20+
21+
public class WebUITest {
22+
23+
@Mock
24+
private WebDriver mockDriver;
25+
26+
@BeforeAll
27+
public static void setUpClass() {
28+
System.setProperty(SELENIUM_WEB_DRIVER_TARGET, "local");
29+
System.setProperty(SELENIUM_BROWSER_TYPE, "chrome");
30+
}
31+
32+
@BeforeEach
33+
public void setUp() {
34+
try (MockedStatic<TargetFactory> mockFactory = Mockito.mockStatic(TargetFactory.class)) {
35+
mockFactory.when(TargetFactory::createInstance).thenReturn(mockDriver);
36+
}
37+
}
38+
39+
@AfterEach
40+
public void tearDown() {
41+
mockDriver.close();
42+
}
43+
44+
@Test
45+
public void openBrowser_returnsDriver() {
46+
mockDriver = WebUI.openBrowser();
47+
assertNotNull(mockDriver);
48+
assertTrue(mockDriver instanceof ChromeDriver);
49+
}
50+
51+
@Test
52+
public void openBrowser_withUrl_returnsDriver() {
53+
String url = "https://demoqa.com/";
54+
mockDriver = WebUI.openBrowser(url);
55+
mockDriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
56+
assertNotNull(mockDriver);
57+
assertTrue(mockDriver.getTitle().equals("DEMOQA"));
58+
}
59+
}

0 commit comments

Comments
 (0)