Springboot with Mockito Rest Controller Test example

Springboot with Mockito Rest Controller Test example



public class ControllerTest{
private MockMvc mockMvc;

Service s;


@Before
public void setUp() throws Exception{

s = mock(Service.class);
this.mockMvc = MockMvcBuilders.standaloneSetup(Controller.class).build();


}

@Test
public void Test(){

when(s.get()).thenReturn("");

ResultActions ac = mockMvc.perform(get("url"));

sc.addReturn().getResponse().getStatus();
}


}

  • Mock service example
s = mock(Service.class);
when(s.get()).thenReturn("");

  • Mock controller example
this.mockMvc = MockMvcBuilders.standaloneSetup(Controller.class).build();
ResultActions ac = mockMvc.perform(get("url"));

  • mockito with rest endpoint testing
ResultActions ac = mockMvc.perform(get("url"));
  • mockito with get type testing
ResultActions ac = mockMvc.perform(get("url")).header("key","value");

ResultActions ac = mockMvc.perform(get("url")).param("url param", "value").param("","");
  • mockito with post type endpoint testing
ResultActions ac = mockMvc.perform(post("url"))
.contentType(MediaType.APPLICATION_JSON)
.content("json content")
;

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Hibernate Search - Elasticsearch with JSON manipulation

Spring Elasticsearch Operations