Skip to content

Commit

Permalink
Merge pull request #67 from cathrkri/66-fix-unit-tests
Browse files Browse the repository at this point in the history
Merge #66 with develop.
  • Loading branch information
roaraf authored Apr 14, 2026
2 parents 695043f + 3d84006 commit f3ab384
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 23 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -37,49 +37,48 @@ void updateDescriptionShouldReturnCorrectDescriptionWithoutReadMore() {
WebElement p1 = mock(WebElement.class);
WebElement p2 = mock(WebElement.class);


when(p1.getText()).thenReturn("Short description");
when(p2.getText()).thenReturn("");

when(scraper.findElements(By.cssSelector(".information div p"))).thenReturn(List.of(p1, p2));

when(scraper.findElements(By.cssSelector("a.read-more"))).thenReturn(List.of());
when(scraper.findElements(By.cssSelector("a.read-more")))
.thenReturn(List.of());

when(scraper.findElements(By.cssSelector(".extra-info p"))).thenReturn(List.of());
when(scraper.findElements(By.cssSelector(".information")))
.thenReturn(List.of(p1, p2));

scraper.updateDescription();

String result = scraper.getDescription();

assertTrue(
result.contains("Short description"), "First paragraph should be 'Short description'");
assertFalse(result.isBlank(), "Second paragraph should be blank.");
assertFalse(result.isBlank(), "Blank paragraph should not be included.");
}

@Test
void updateDescriptionShouldReturnCorrectDescriptionWithReadMore() {
WebElement p1 = mock(WebElement.class);
WebElement extra = mock(WebElement.class);
WebElement readMore = mock(WebElement.class);

when(p1.getText()).thenReturn("Intro");
when(extra.getText()).thenReturn("Extra info");
@Test
void updateDescriptionShouldReturnCorrectDescriptionWithReadMore() {
WebElement p1 = mock(WebElement.class);
WebElement extra = mock(WebElement.class);
WebElement readMore = mock(WebElement.class);

when(scraper.findElements(By.cssSelector(".information div p"))).thenReturn(List.of(p1));
when(p1.getText()).thenReturn("Intro");
when(extra.getText()).thenReturn("Extra info");

when(scraper.findElements(By.cssSelector("a.read-more"))).thenReturn(List.of(readMore));
when(scraper.findElements(By.cssSelector("a.read-more"))).thenReturn(List.of(readMore));

when(scraper.findElement(By.cssSelector("a.read-more"))).thenReturn(readMore);
when(scraper.findElement(By.cssSelector("a.read-more"))).thenReturn(readMore);

when(scraper.findElements(By.cssSelector(".extra-info p"))).thenReturn(List.of(extra));
when(scraper.findElements(By.cssSelector(".information"))).thenReturn(List.of(p1, extra));

scraper.updateDescription();
scraper.updateDescription();

String result = scraper.getDescription();
String result = scraper.getDescription();

verify(readMore).click();
assertTrue(result.contains("Intro"), "First paragraph should be 'Intro'");
assertTrue(result.contains("Extra info"), "Second paragraph should be 'Extra info'");
}
verify(readMore).click();
assertTrue(result.contains("Intro"));
assertTrue(result.contains("Extra info"));
}

@Test
void updateLogoShouldReturnCorrectLogoURL() {
Expand Down Expand Up @@ -156,4 +155,43 @@ void scrapeCharityPageShouldCallAllRelevantMethods() {
// UpdateKeyValues should run
verify(spyScraper).updateKeyValues();
}

@Test
void updateLogoShouldHandleException() {
when(scraper.findElement(By.cssSelector(".logo > img"))).thenThrow(new RuntimeException("Not found"));

scraper.updateLogo();

assertNull(scraper.getLogoURL(), "Program should handle the exception and the logo should be null.");
}

@Test
void updateCategoriesShouldHandleException() {
when(scraper.findElements(By.cssSelector(".tag-label"))).thenThrow(new RuntimeException("Fail"));

scraper.updateCategories();

assertEquals("", scraper.getCategories(), "Program should handle the exception " +
"and the categories should be null.");
}

@Test
void updateKeyValuesShouldHandleException() {
when(scraper.findElement(any(By.class))).thenThrow(new RuntimeException("Fail"));

scraper.updateKeyValues();

assertEquals("", scraper.getKeyValues(), "Program should handle the exception and the " +
"exception and the key values should be empty.");
}

@Test
void updateDescriptionShouldHandleException() {
when(scraper.findElements(any(By.class))).thenThrow(new RuntimeException("Fail"));

scraper.updateDescription();

assertNull(scraper.getDescription(), "Program should handle the exception and the " +
"description should be null.");
}
}

0 comments on commit f3ab384

Please sign in to comment.