Skip to main content

Posts

Showing posts from June, 2021

RADUS#5 - Unit testing controller

  As we have written few methods in our controller, let's write quick unit tests for couple of methods: @RunWith (SpringRunner . class) @WebMvcTest (TodoController . class) public class TodoControllerTest { @Autowired private MockMvc mvc; @MockBean private TodoServiceImpl todoService; private final String BASE_PATH_V1 = "/api/1/0" ; @Test public void testHello() throws Exception { mvc . perform( get(BASE_PATH_V1 + "/hello" )) . andExpect(status() . isOk()) . andExpect(content() . string( "Hello Rest demo" )); } @Test public void testGetUsers() throws Exception { // Mock setup Map < Integer, String > expected = new HashMap <> (); expected . put( 1 , "Todo - 1" ); expected . put( 2 , "Todo - 2" ); Mockito . when(todoService . getTodos()) . thenReturn(expected); //...