Skip to content

Instantly share code, notes, and snippets.

@pavankjadda
Last active December 22, 2021 23:28
Show Gist options
  • Save pavankjadda/41e05529f76a07cf8c7f749e2c8335ff to your computer and use it in GitHub Desktop.
Save pavankjadda/41e05529f76a07cf8c7f749e2c8335ff to your computer and use it in GitHub Desktop.
Spring Boot Controller layer unit Test
@SpringBootTest(webEnvironment = RANDOM_PORT)
@ContextConfiguration(classes = {PresApplication.class})
@WithUserDetails(value = "demo_user", userDetailsServiceBeanName = "customDbUserDetailsService")
@AutoConfigureMockMvc
@ActiveProfiles(value = "local")
class PersonControllerTest
{
	@Autowired
	private MockMvc mockMvc;

	@MockBean
	private PersonService personService;

	@Test
	void getPersonById() throws Exception
	{
		var personId = 1L;
		when(personService.findPersonById(personId)).thenReturn(new Person());
		this.mockMvc.perform(get("/api/v1/person/find/" + personId)).andDo(print()).andExpect(status().isOk()).andExpect(content().contentType(MediaType.APPLICATION_JSON));
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment