Home > Blockchain >  How to assert FlashAttributes in Spring WebTestClient?
How to assert FlashAttributes in Spring WebTestClient?

Time:08-16

I want to test FlashMap assignment of a spring @Controller:

@Controller
public class MyServlet {
    @GetMapping("/test")
    public String get() {
            ServletRequestAttributes reqAttr = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            FlashMap outAttr = RequestContextUtils.getOutputFlashMap(reqAttr.getRequest());
            outAttr.put("success", "test");
        }
    }
}

What how can I actually access the flash attributes from a WebTestClient class?

@SpringBootTest
public class ServletTest {
    @Autowired
    private WebTestClient;

    @Test
    public void test() {
        client.get().uri("/test")
            .expectStatus().isOk()
            .expectBody().isEmpty(); 
            //TODO how to validate flash attributes?
    }
}

CodePudding user response:

EntityExchangeResult<byte[]> result = client...
                                            .expectBody().returnResult();

MockMvcWebTestClient.resultActionsFor(result)
                    .andExpect(flash()
                    .attribute("success", "test");
  • Related