It is now time to write our first unit test.
We will focus on writing tests at the controller level because we have little to no business code or service. The key to writing tests for Spring MVC is the org.springframework.boot:spring-boot-starter-test
dependency in our classpath. It will add a few very useful libraries, such as these:
hamcrest
: This is JUnit's assertion librarymockito
: This is a mocking libraryspring-test
: This is the Spring testing library
We will test the redirection to the profile page that is created when the user hasn't created their profile yet.
We already have an autogenerated test called MasterSpringMvc4ApplicationTests
. It is the most basic kind of test one can write with the Spring test framework: it does nothing but blow up if the context cannot be loaded:
@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = MasterSpringMvc4Application.class) @WebAppConfiguration public class MasterSpringMvc4ApplicationTests { ...