I never did a test and decided I would do my first. I am using Spring Boot and by default the layout is as follows: https://zapodaj.net/c8b65f1644e95.png.html The file that is created on startup looks like this
@RunWith(SpringRunner.class)
@SpringBootTest
public class RestWebServicesApplicationTests {
@Test
public void contextLoads() {
}
}
I do not change anything here, because I do not need it. By contrast, I created a new class
public class CheckUserDataRestControllerTest {
@Mock
private UserService userService;
@InjectMocks
private CheckUserDataRestController checkUserDataRestController;
private MockMvc mockMvc;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders.standaloneSetup(checkUserDataRestController).build();
}
@Test
public void textExistsUsername() throws Exception {
mockMvc
.perform(get("/checkUsernameAtRegistering")
.param("username", "jonki97"))
.andExpect(status().isOk());
}
}
To test the controller method, which should return the status OK.
@GetMapping("/checkUsernameAtRegistering")
public HttpEntity<Boolean> checkUsernameAtRegistering(@RequestParam String username) {
return ResponseEntity.ok().body(!userService.existsByUsername(username));
}
However, during the test, he throws me out
java.lang.AssertionError: Status
Expected :200
Actual :404
<Click to see difference>
at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:54)
...
This is the tested controller: https://pastebin.com/MWW9YwiH
I did not make any special configurations because I did not find out that I needed to do something.