Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • 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 - JUnit Controller Test JWT Issue

datdatyul

Active Coder
I have a problem about solving JwtGrantedAuthoritiesConverter in order service after defining preauthorize annotation in some methods of Order Controller.
After I wrote the test shown below, I tried to run it but I got this issue shown below as well.
I also shared my repo as a link.
How can I fix it?
Here is the test method shown below

Code:
@Test
    public void test_WhenPlaceOrder_DoPayment_Success() throws Exception {
        //First Place Order
        // Get Order by Order Id from Db and check
        //Check Output

        OrderRequest orderRequest = getMockOrderRequest();
        MvcResult mvcResult
                = mockMvc.perform(MockMvcRequestBuilders.post("/order/placeOrder")
                        .with(jwt().authorities(new SimpleGrantedAuthority("USER")))
                        .contentType(MediaType.APPLICATION_JSON_VALUE)
                        .content(objectMapper.writeValueAsString(orderRequest))
                ).andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();

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

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

        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 application.yaml underneath test folder

Code:
server:
  port: 8082

spring:
  datasource:
    url: jdbc:h2:mem:order
    username: sa
    password: password
    driverClassName: org.h2.Driver
  jpa:
    database-platform: org.hibernate.dialect.H2Dialect
    hibernate:
      ddl-auto: update
  application:
    name: ORDER-SERVICE
  config:
    import: configserver:http://localhost:9296

Here is the error shown below.

Code:
java.lang.NoClassDefFoundError: org/springframework/security/oauth2/server/resource/authentication/JwtGrantedAuthoritiesConverter
   Caused by: java.lang.ClassNotFoundException: org.springframework.security.oauth2.server.resource.authentication.JwtGrantedAuthoritiesConverter

Then I added spring-security-oauth2-resource-server dependency in order service, I got this issue show below.

Code:
Caused by: java.lang.ClassNotFoundException: org.springframework.security.oauth2.jwt.Jwt$Builder

How can I fix it?
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom