Skip to content

Commit

Permalink
Updated URLCharityScraperTest
Browse files Browse the repository at this point in the history
Fixed the unit test after description tests broke due to change in selector.
Added negative tests/tests for exception handling.
  • Loading branch information
roaraf committed Apr 14, 2026
1 parent c8a9a92 commit 1c41eef
Showing 1 changed file with 61 additions and 23 deletions.
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 1c41eef

Please sign in to comment.