<dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-webapp</artifactId> <version>9.4.43.v20210629</version> <scope>test</scope> </dependency> public class StringUtils { public static int countOccurrences(String str, char ch) { int count = 0; for (char c : str.toCharArray()) { if (c == ch) { count++; } } return count; } } import org.eclipse.jetty.server.Server; import org.eclipse.jetty.webapp.WebAppContext; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import static org.junit.Assert.assertEquals; public class StringUtilsTest { private static Server server; @BeforeClass public static void setUp() throws Exception { server = new Server(8080); WebAppContext webAppContext = new WebAppContext(); webAppContext.setDescriptor("src/main/webapp/WEB-INF/web.xml"); webAppContext.setResourceBase("src/main/webapp"); webAppContext.setContextPath("/"); webAppContext.addServlet(StringUtilsServlet.class, "/StringUtilsServlet"); server.setHandler(webAppContext); server.start(); } @AfterClass public static void tearDown() throws Exception { server.stop(); } @Test public void testCountOccurrences() throws Exception { String response = HttpClientUtil.getRequest("http://localhost:8080/StringUtilsServlet?str=hello&ch=l"); assertEquals("2", response); } }


上一篇:
下一篇:
切换中文