• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why does'nt my mockMvc submit any content?

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to optimize step by step an example that I've got from the internet . So I started with the Unit tests.
What I want is to write a Unit test with SpringBoot and Mockito like the last example as here https://spring.io/guides/gs/testing-web/.
In my code I have a controller, a DAO and a service. I would like to Unit test 2 methods of my controller e.g. the one with POST and the one with PUT.
But somehow the content is'nt submitted. The Database in the Background is MySQL5.7.25

following the controller's methods I want to Unit-Test:

I did'nt submit all the methods in the controller but these are the ones that I don't succeed to Unit-Test
then my service:


I did'nt submit the code of the Service Interface
my DAO  (no code of the DAO Interface:


My Repo:

my Entity:

And finally my Tests with some out commented code due to some of my trials

Now my Problem:
the Tests are failing because somehow no content-body is submitted. On the console I have the following Testresults:
MockHttpServletRequest:
 HTTP Method = POST
 Request URI = /articles
  Parameters = {}
     Headers = []
        Body = <no character encoding set>
Session Attrs = {}


MockHttpServletRequest:
 HTTP Method = PUT
 Request URI = /article/2
  Parameters = {}
     Headers = [Accept:"application/json;charset=UTF-8"]
        Body = <no character encoding set>
Session Attrs = {}
My question: What I'm doing wrong?I know that I'm mocking wrong but what?
Many Thanks for helping!
PS:the application is working fine. I can add and update the articles in the database. Only the tests don't work.
Many thanks in advance.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try to use .characterEncoding("utf-8")) :
mockMvc.perform(MockMvcRequestBuilders.post("/articles").content(asJSONString(article)).characterEncoding("utf-8"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk());
reply
    Bookmark Topic Watch Topic
  • New Topic