import com.github.tomakehurst.wiremock.WireMockServer; import com.github.tomakehurst.wiremock.client.WireMock; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.client.methods.CloseableHttpResponse; import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; import static com.github.tomakehurst.wiremock.client.WireMock.*; import static org.junit.Assert.assertEquals; public class AuthenticationServiceTest { private WireMockServer wireMockServer; private AuthenticationService authService; @Before public void setup() { wireMockServer = new WireMockServer(wireMockConfig().port(8080)); wireMockServer.start(); WireMock.configureFor("localhost", 8080); authService = new AuthenticationService(); } @After public void teardown() { wireMockServer.stop(); } @Test public void testLogin() throws Exception { // Stubbing the login endpoint stubFor(post(urlEqualTo("/login")) .withHeader("Content-Type", equalTo("application/json")) .willReturn(aResponse() .withStatus(200) .withHeader("Content-Type", "application/json") .withBody("{\"message\": \"Login successful\"}"))); // Testing the login functionality String result = authService.login("username", "password"); assertEquals("{\"message\": \"Login successful\"}", result); } }