mardi 1 août 2017

Spring boot , testing crud repository

I never write tests for my rest controllers and services. I have read offical docs , how to write integration tests in spring boot. So for example I have a Rest Controller class

@RestController
@RequestMapping(value = "users")
public class SvcUser {

    @RequestMapping(value = "user", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public ResponseEntity<String> registrateUser(@RequestBody Registration registrate) throws TimeoutException, SocketTimeoutException  {

        final String result = userService.registrateUser(registrate.getPhoneCode(), registrate.getPhoneNumber(), registrate.getOsType());
        return ResponseEntity.ok(result);
    }

And inside userServiceClass i did smth like this

  registrateUser(String args ...){
         Users user = new User();
         user.setSmth(smth);
         userRepository.save(user)

   }

My integration test

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT,classes = GmoikaApiApplication.class)

    public class GmoikaApiApplicationTests {

        private MockMvc mockMvc;
        @Autowired
        private WebApplicationContext wac;
        @Before
        public void setUp(){
            this.mockMvc = webAppContextSetup(wac).build();
        }

        @Test
        public void firstTest() throws Exception {
            mockMvc.perform(post("/users/user")
                    .content("my new user"))
                    .andExpect(status().isOk())
                    .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
                    .andExpect(jsonPath("$.error",is(0)))
                    .andDo(print())
                    .andReturn();        
        }

And this is work fine, but i have new user in my db. I don't wanna create fake users in production only due to tests.My question is how to avoid creation of new users in db inside integration tests?

Aucun commentaire:

Enregistrer un commentaire