简体   繁体   中英

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?

You need to create profiles in spring framework, 1st for your standard application with your database configuration and another for your test application, For testing you should use h2database. H2 database is in-memory databases so you don't need to any h2 database in installation, just add the dependency of h2database in project

com.h2database h2 1.4.194

also in your test profile

jdbc.driverClassName=org.h2.Driver
jdbc.url=jdbc:h2:mem:myDb;DB_CLOSE_DELAY=-1

hibernate.dialect=org.hibernate.dialect.H2Dialect
hibernate.hbm2ddl.auto=create

I assume that your project is a Maven project, then:

  1. Add the following dependency to pom.xml

    <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>test</scope> </dependency>

  2. Add to src/test/resources/config the file application.properties containing something similar to:

    spring.datasource.url=jdbc:h2:mem:mytestdb spring.datasource.driver-class-name=org.h2.Driver spring.datasource.username=sa spring.datasource.password= spring.jpa.database-platform=org.hibernate.dialect.H2Dialect spring.jpa.database=H2 spring.jpa.open-in-view=false spring.jpa.show-sql=false spring.jpa.hibernate.ddl-auto=create-drop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM