Skip to content

Commit

Permalink
updated TDD HeadlessExample
Browse files Browse the repository at this point in the history
  • Loading branch information
danrodgar committed May 11, 2024
1 parent cfe92f6 commit 658dbf5
Show file tree
Hide file tree
Showing 6 changed files with 331 additions and 165 deletions.
2 changes: 2 additions & 0 deletions code/MockitoATM/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
<goal>report</goal>
</goals>
</execution>
<!--
<execution>
<id>generate-badge</id>
<phase>post-site</phase>
Expand All @@ -91,6 +92,7 @@
<goal>badge</goal>
</goals>
</execution>
-->
</executions>
</plugin>
</plugins>
Expand Down
20 changes: 20 additions & 0 deletions code/SeleniumTestExample/src/test/java/uah/HeadlessExample.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package uah;

import org.junit.Ignore;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
Expand All @@ -12,7 +13,24 @@

public class HeadlessExample {


@Test
void headlessChromeTest1() {
System.setProperty("webdriver.chrome.driver", "./src/test/resources/chromedriver");

ChromeOptions options = new ChromeOptions();
options.addArguments("headless");

WebDriver driver = new ChromeDriver(options);
driver.get("https://escuelapolitecnica.uah.es/");

System.out.println("Title of the page:" + driver.getTitle());
System.out.println("URL of the page:" + driver.getCurrentUrl());
}

/*
@Test
@Ignore
public void headlessChromeTest() {
System.setProperty("webdriver.chrome.driver", "./src/test/resources/chromedriver");
Expand All @@ -38,6 +56,8 @@ public void headlessChromeTest() {
driver.quit();
}
*/

@Test
void headlessFirefoxTest() {
System.setProperty("webdriver.gecko.driver", "./src/test/resources/geckodriver");
Expand Down
Binary file modified code/SeleniumTestExample/src/test/resources/chromedriver
Binary file not shown.
84 changes: 84 additions & 0 deletions code/TDDExampleFactorial/src/main/java/uah/Factorial.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package uah;

import java.util.Scanner;

public class Factorial {
public static void main(String[] args) {
System.out.print("Enter an integer: ");
Scanner in = new Scanner(System.in);
int num = in.nextInt();
Factorial fact = new Factorial();
try {
System.out.println(fact.compute(num));
} catch (Exception e) {
System.out.print(e.toString());
}
}

public int compute(int i) {
int result = 0;
if (i==0)
result = 1;
else
result = i*compute(i-1);
return result;
}

/* Initial version after the first JUnit */
/* public int compute(int i) {
return 1;
}*/

/* After adding second, third, etc. JUnit */
/* public int compute(int i) {
int result = 0;
if (i==0 || i ==1)
result = 1;
else
if (i==2)
result = 2;
else
if (i==3)
result = 6;
else
result = i*compute(i-1);
return result;
}*/

/*After refactoring */
/* public int compute(int i) {
int result = 0;
if (i == 0 || i == 1) result = 1;
else result = i * compute(i - 1);
return result;
}*/

/* public int compute(int i) {
if (i<0)
throw new IllegalArgumentException("No negatives: " + i);
int result = 0;
if (i==0 || i ==1)
result = 1;
else
if (i==2)
result = 2;
else
if (i==3)
result = 6;
else
result = i*compute(i-1);
return result;
}*/

/*
public int compute(int i) {
if (i<0)
throw new IllegalArgumentException("No negatives: " + i);
int result = 0;
if (i == 0 || i == 1) result = 1;
else result = i * compute(i - 1);
return result;
}
*/
}
67 changes: 58 additions & 9 deletions code/TDDExampleFactorial/src/test/java/uah/FactorialTest.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,69 @@
package uah;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

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

class FactorialTest {

private Factorial factorial;

/* We can refactor the Junits with new Factorial as setUp()*/
/* We can refactor the Junits with new Factorial as setUp()
@BeforeEach
public void setUp() {
factorial = new Factorial();
}
*/

@Test
public void shouldFactorialOf0Return1() {
// Factorial factorial = new Factorial() ;
int expectedValue = 1;
int obtainedValue = factorial.compute(0);
assertEquals(expectedValue, obtainedValue);
Factorial factorial = new Factorial();
int expected = 1;
int actual = factorial.compute(0);
assertEquals(expected, actual);
}

@Test
public void shouldFactorialOf1Return1() {
Factorial factorial = new Factorial();
int expected = 1;
int actual = factorial.compute(1);
assertEquals(expected, actual);
}

@Test
public void shouldFactorialOf2Return2() {
Factorial factorial = new Factorial();
int expected = 2;
int actual = factorial.compute(2);
assertEquals(expected, actual);
}

@Test
public void shouldFactorialOf3Return6() {
Factorial factorial = new Factorial();
int expected = 6;
int actual = factorial.compute(3);
assertEquals(expected, actual);
}

@Test
public void shouldFactorialOf5Return120() {
Factorial factorial = new Factorial();
int expected = 120;
int actual = factorial.compute(5);
assertEquals(expected, actual);
}



// Factorial factorial = new Factorial() ;
//int expectedValue = 1;
//int obtainedValue = factorial.compute(0);
//assertEquals(expectedValue, obtainedValue);
//}

/*
@Test
public void shouldFactorialOf1Return1() {
// Factorial factorial = new Factorial() ;
Expand All @@ -47,11 +87,20 @@ void testExpectedExceptionWithSuperType() {
;
});
}

*/
// There is also an overflow Exception
// The following can be done as exercises:
// 1. Overflow, check the maximum number before the input is too large
// 2. Implement the iterative version and check it
// Another similar example that can be done is with Fibonacci


/*
1. fact(0) = 0;
2. fact(1) = 1;
3. fact(2) = 2;
4. fact(3) = 6;
5. fact(5) = 120;
*/

}
Loading

0 comments on commit 658dbf5

Please sign in to comment.