|
//DEPS org.jsoup:jsoup:1.15.3 |
|
//DEPS org.junit.jupiter:junit-jupiter-api:5.8.2 |
|
//DEPS org.junit.jupiter:junit-jupiter-engine:5.8.2 |
|
//DEPS org.mockito:mockito-core:4.5.1 |
|
|
|
import org.jsoup.Jsoup; |
|
import org.jsoup.nodes.Document; |
|
import org.junit.jupiter.api.BeforeEach; |
|
import org.junit.jupiter.api.Test; |
|
import org.mockito.MockedStatic; |
|
|
|
import java.io.IOException; |
|
|
|
import static org.junit.jupiter.api.Assertions.*; |
|
import static org.mockito.Mockito.*; |
|
|
|
class WebScraperTest { |
|
|
|
private WebScraper webScraper; |
|
|
|
@BeforeEach |
|
void setUp() { |
|
webScraper = new WebScraper(); |
|
} |
|
|
|
@Test |
|
void testValidUrlWithContent() throws IOException { |
|
String html = "<html><body><div class='content'>Test Content</div></body></html>"; |
|
Document mockDocument = Jsoup.parse(html); |
|
|
|
try (MockedStatic<Jsoup> mockedJsoup = mockStatic(Jsoup.class)) { |
|
mockedJsoup.when(() -> Jsoup.connect(anyString()).get()).thenReturn(mockDocument); |
|
|
|
String result = webScraper.scrape("https://example.com"); |
|
assertEquals("Test Content", result); |
|
} |
|
} |
|
|
|
@Test |
|
void testValidUrlWithoutContentDiv() throws IOException { |
|
String html = "<html><body><div>No Content Div</div></body></html>"; |
|
Document mockDocument = Jsoup.parse(html); |
|
|
|
try (MockedStatic<Jsoup> mockedJsoup = mockStatic(Jsoup.class)) { |
|
mockedJsoup.when(() -> Jsoup.connect(anyString()).get()).thenReturn(mockDocument); |
|
|
|
String result = webScraper.scrape("https://example.com"); |
|
assertEquals("No content found with the specified class.", result); |
|
} |
|
} |
|
|
|
@Test |
|
void testEmptyUrl() { |
|
assertThrows(IllegalArgumentException.class, () -> webScraper.scrape("")); |
|
} |
|
|
|
@Test |
|
void testNullUrl() { |
|
assertThrows(IllegalArgumentException.class, () -> webScraper.scrape(null)); |
|
} |
|
|
|
@Test |
|
void testInvalidUrl() { |
|
assertThrows(IllegalArgumentException.class, () -> webScraper.scrape("not a url")); |
|
} |
|
|
|
@Test |
|
void testConnectionTimeout() throws IOException { |
|
try (MockedStatic<Jsoup> mockedJsoup = mockStatic(Jsoup.class)) { |
|
mockedJsoup.when(() -> Jsoup.connect(anyString()).get()).thenThrow(new IOException("Connection timeout")); |
|
|
|
String result = webScraper.scrape("https://example.com"); |
|
assertEquals("Error: Connection timeout", result); |
|
} |
|
} |
|
|
|
@Test |
|
void testHttpError() throws IOException { |
|
try (MockedStatic<Jsoup> mockedJsoup = mockStatic(Jsoup.class)) { |
|
mockedJsoup.when(() -> Jsoup.connect(anyString()).get()).thenThrow(new IOException("HTTP error 404")); |
|
|
|
String result = webScraper.scrape("https://example.com"); |
|
assertEquals("Error: HTTP error 404", result); |
|
} |
|
} |
|
|
|
@Test |
|
void testEmptyDocument() throws IOException { |
|
Document mockDocument = Jsoup.parse(""); |
|
|
|
try (MockedStatic<Jsoup> mockedJsoup = mockStatic(Jsoup.class)) { |
|
mockedJsoup.when(() -> Jsoup.connect(anyString()).get()).thenReturn(mockDocument); |
|
|
|
String result = webScraper.scrape("https://example.com"); |
|
assertEquals("No content found with the specified class.", result); |
|
} |
|
} |
|
} |