2
respostas

JavaSpringBoot

Meu projeto ta retornando esse erro e nao consigo arrumar. Alguem pode me ajudar ?

[org.springframework.security.web.session.DisableEncodeUrlFilter@1e3f86d5, org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@560271a1, org.springframework.security.web.context.SecurityContextPersistenceFilter@12fcb2c3, org.springframework.security.web.header.HeaderWriterFilter@49671897, org.springframework.security.web.authentication.logout.LogoutFilter@5db948c9, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@7ddcb0dc, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@517704, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@31a136a6, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@4b9c411, org.springframework.security.web.session.SessionManagementFilter@4026461d, org.springframework.security.web.access.ExceptionTranslationFilter@2d313c8c, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@4d518c66] 2023-07-31 15:30:50.265 INFO 12844 --- [ main] o.s.b.t.m.w.SpringBootMockServletContext : Initializing Spring TestDispatcherServlet '' 2023-07-31 15:30:50.266 INFO 12844 --- [ main] o.s.t.web.servlet.TestDispatcherServlet : Initializing Servlet '' 2023-07-31 15:30:50.266 INFO 12844 --- [ main] o.s.t.web.servlet.TestDispatcherServlet : Completed initialization in 0 ms 2023-07-31 15:30:50.298 INFO 12844 --- [ main] C.apigateway.ApiGatewayApplicationTests : Started ApiGatewayApplicationTests in 1.874 seconds (JVM running for 3.241)

MockHttpServletRequest: HTTP Method = GET Request URI = /products Parameters = {} Headers = [] Body = null Session Attrs = {SPRING_SECURITY_SAVED_REQUEST=DefaultSavedRequest [http://localhost/products]}

Handler: Type = null

Async: Async started = false Async result = null

Resolved Exception: Type = null

ModelAndView: View name = null View = null Model = null

FlashMap: Attributes = null

MockHttpServletResponse: Status = 401 Error message = Unauthorized Headers = [WWW-Authenticate:"Basic realm="Realm"", X-Content-Type-Options:"nosniff", X-XSS-Protection:"1; mode=block", Cache-Control:"no-cache, no-store, max-age=0, must-revalidate", Pragma:"no-cache", Expires:"0", X-Frame-Options:"DENY"] Content type = null Body = Forwarded URL = null Redirected URL = null Cookies = []

java.lang.AssertionError: Status expected:<200> but was:<401> Expected :200 Actual :401

Process finished with exit code -1

2 respostas

César, boa tarde.

Nos disponibilize o codigo por gentileza.

Esse é o teste que esta dando o erro

@RunWith(MockitoJUnitRunner.class) @WebMvcTest(ApiGatewayApplication.class) public class ApiGatewayApplicationTests {

@Autowired
private MockMvc mockMvc;

@MockBean
private ProductService productService;

@Test
public void testGetAllProducts() throws Exception {
    // Mock o comportamento do productService.getAllProducts()
    List<Product> products = new ArrayList<>();
    products.add(new Product(1L, "Product 1", "Description 1", 100.0, "In Stock"));
    products.add(new Product(2L, "Product 2", "Description 2", 200.0, "In Stock"));
    products.add(new Product(3L, "Product 3", "Description 3", 300.0, "Out of Stock"));
    when(productService.getAllProducts()).thenReturn(products);

    // Executa a requisição GET para "/products" com autenticação básica e verifica o resultado
    mockMvc.perform(get("/products").with(httpBasic("username", "password")))
            .andExpect(status().isOk())
            .andExpect(jsonPath("$.length()").value(3))
            .andExpect(jsonPath("$[0].id").value(1L))
            .andExpect(jsonPath("$[0].name").value("Product 1"))
            .andExpect(jsonPath("$[0].price").value(100.0))
            .andExpect(jsonPath("$[1].id").value(2L))
            .andExpect(jsonPath("$[1].name").value("Product 2"))
            .andExpect(jsonPath("$[1].price").value(200.0))
            .andExpect(jsonPath("$[2].id").value(3L))
            .andExpect(jsonPath("$[2].name").value("Product 3"))
            .andExpect(jsonPath("$[2].price").value(300.0));
}

}