• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Java Spring Boot Microservices - Spring Security Issue in one service throwing throwing java.lang.StackOverflowError in JUnit


Active Coder
I have a problem in running any test method in service test and controller test in one of the spring boot microservices (order service).

After I completed service and controller , I tried to write their test methods but I have a problem in there.

How can I fix it?

Here is the security config of order service?

    @EnableWebSecurity(debug = true)
    @EnableGlobalMethodSecurity(prePostEnabled = true)
    public class SecurityConfig {
        private final JwtAuthenticationEntryPoint authenticationEntryPoint;
        private final JWTAccessDeniedHandler accessDeniedHandler;
        private final JwtAuthenticationFilter jwtAuthenticationFilter;
        public AuthenticationManager authenticationManager(final AuthenticationConfiguration authenticationConfiguration) throws Exception {
            return authenticationConfiguration.getAuthenticationManager();
        public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
            return http
                    .antMatchers(HttpMethod.POST, "/order/**").hasRole("USER")
                    .antMatchers(HttpMethod.GET, "/order/**").hasRole("USER")
                    .addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class)
        public WebSecurityCustomizer webSecurityCustomizer() {
            return (web)
                    -> web.ignoring().antMatchers("/authenticate/signup", "/authenticate/login", "/authenticate/refreshtoken");

Here is one of the test method of OrderServiceTest shown below

    @DisplayName("Get Order - Success Scenario")
    void test_When_Order_Success() {
        Order order = getMockOrder();
                    "http://PRODUCT-SERVICE/product/" + order.getProductId(),
                    "http://PAYMENT-SERVICE/payment/order/" + order.getId(),
        OrderResponse orderResponse = orderService.getOrderDetails(1);
        verify(orderRepository, times(1)).findById(anyLong());
        verify(restTemplate, times(1)).getForObject(
                    "http://PRODUCT-SERVICE/product/" + order.getProductId(),
        verify(restTemplate, times(1)).getForObject(
                    "http://PAYMENT-SERVICE/payment/order/" + order.getId(),
        assertEquals(order.getId(), orderResponse.getOrderId());

Here is one of the test method of OrderControllerTest shown below.

    @DisplayName("Place Order -- Success Scenario")
    @WithMockUser(username = "User", authorities = { "ROLE_USER" })
    void test_When_placeOrder_DoPayment_Success() throws Exception {

        OrderRequest orderRequest = getMockOrderRequest();
        String jwt = getJWTTokenForRoleUser();

        MvcResult mvcResult
                = mockMvc.perform(MockMvcRequestBuilders.post("/order/placeorder")
                        .header("Authorization", "Bearer " + jwt)

        String orderId = mvcResult.getResponse().getContentAsString();

        Optional<Order> order = orderRepository.findById(Long.valueOf(orderId));

        Order o = order.get();
        assertEquals(Long.parseLong(orderId), o.getId());
        assertEquals("PLACED", o.getOrderStatus());
        assertEquals(orderRequest.getTotalAmount(), o.getAmount());
        assertEquals(orderRequest.getQuantity(), o.getQuantity());

Here is the error when I run any method of service test and controller shown below.

        at java.base/java.lang.Throwable.getOurStackTrace(Throwable.java:861)
        at java.base/java.lang.Throwable.getStackTrace(Throwable.java:853)
        at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:79)

I found this link but it didn't help me fix the issue.
I hope you can help me.

**To run the app,**

1 ) Run Service Registery (Eureka Server)

2 ) Run config server

3 ) Run zipkin and redis through these commands shown below on docker

docker run -d -p 9411:9411 openzipkin/zipkin
docker run -d --name redis -p 6379:6379 redis

4 ) Run api gateway

5 ) Run other services

Here is the repo : Link

Here is the **result of the service test** screenshot : Link

Here is the **result of the controller test** screenshot : Link
Top Bottom