The springboot comes with a test module.
Annotations require:
@SpringApplicationConfiguration (classes = computeserviceapplication. Class)
This allows the environment context to be introduced.
The complete notes are as follows:
@RunWith (Springjunit4classrunner. Class= computeserviceapplication. class ) @WebAppConfiguration
@Before
Initializing an MOCKMVC instance
Public void throws Exception { = mockmvcbuilders.webappcontextsetup (WAC). build (); }
@Test
Start writing Use cases
An instance of request needs to be initialized, as in the following example.
@Test Public voidTestcomputecontroller ()throwsException {Requestbuilder request=NULL; Request= Get ("/userinfo/209799"); Mvc.perform (Request) Andexpect (status (). IsOk ()). Andexpect (Content (). String (Equalto ("{\" name\ ": \" fx\ ", \" description\ ": \" Old man\ ", \" age\ ": \" 50\ "}"))); Request= Get ("/add?a=5&b=7"); Mvc.perform (Request) Andexpect (status (). IsOk ()). Andexpect (Content (). String (Equalto ("12"))); Request= Get ("/test"); Mvc.perform (Request) Andexpect (status (). IsOk ()); }
Test cases for writing Spring boot controller using MOCKMVC