每个测试用例重置DB序列

时间:2015-10-26 19:42:17

标签: spring-boot flyway spring-orm

我正在使用flyweight db脚本来设置 数据库:

CREATE SEQUENCE user_id_seq START WITH 1;
CREATE TABLE user (
  id            SERIAL          NOT NULL PRIMARY KEY,
  email         VARCHAR(64)     NOT NULL,
  nick          VARCHAR(64)     NOT NULL,
  pw            VARCHAR(64)     NOT NULL,

  -- role: admin, simple, member, leader, judge
  role          VARCHAR(64)     NOT NULL,
  judge         INTEGER         ,
  camp          INTEGER         ,

);

首次测试后插入用户和序列 增加一,在第二次测试之前,我可以放弃 用户表,但我不知道如何返回user_id_seq 回到1。

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = BasicApplication.class)
public class UserRepositoryTest {

    @Autowired
    UserRepository userRepository;

    @Test
    public void insertUser(){

        User user = new User();
        user.setNick("roman");
        user.setRole("ROLE_ADMIN");

        PasswordEncoder pwEncoder = new BCryptPasswordEncoder();
        String pw = pwEncoder.encode("123");

        user.setPw(pw);

        userRepository.save(user);

        long userCount = userRepository.count();
        isTrue(userCount == 1);

        boolean exists = userRepository.exists(1);
        isTrue(exists);

        User fetchedUser = userRepository.findOne(1);

        assertEquals(fetchedUser.getNick(), "roman");
        assertEquals(fetchedUser.getPw(), pw);
        assertEquals(fetchedUser.getRole(), "ROLE_ADMIN");

    }

}

欢迎任何建议。

1 个答案:

答案 0 :(得分:0)

找到答案, 这就是你所需要的:

https://github.com/flyway/flyway-test-extensions