静下心来的个人专栏
上一篇

JUnit如何测试Repository层

广告
选中文字可对指定文章内容进行评论啦,→和←可快速切换按钮,绿色背景文字可以点击查看评论额。
大纲

JUnit是一个优秀的java单元测试框架,今天我们用例子来说明如何使用JUnit来测试Repository层。

我们先来看看被测试对象:

@Repository
public interface UserCreditRepository extends JpaRepository<UserCredit, Long> {

    Optional<UserCredit> findByUserId(UserId userId);
}

这个Repository接口是根据用户id来查询对象。

显然我们要测试的方法是根据用户id来查询对象,因此,我们需要

1、先创建表,

2、插入一些数据,

3、执行测试

4、删除数据

spring:
  datasource:
    url: jdbc:h2:~/credit
    driverClassName: org.h2.Driver
    username: sa
    password: password
  jpa:
    database-platform: org.hibernate.dialect.H2Dialect
    hibernate:
      ddl-auto: update
    properties:
      hibernate:
        format_sql: true
    show_sql: true
  h2:
    console:
      enabled: true
      path: /h2-console
      settings:
        trace: false
        web-allow-others: false

有了以上配置,我们在执行测试的时候会创建相应的表结构。

然后我们再一个测试类中执行以上三步:

@DataJpaTest
@RunWith(SpringRunner.class)
public class UserCreditJpaRepositoryTest {

    @Autowired
    private DataSource dataSource;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private EntityManager entityManager;
    @Autowired
    private UserCreditRepository userCreditRepository;

    private UserCredit initUserCredit;

    private UserCredit initWithStaticUserCredit;

    private UserCredit initWithDynamicUserCredit;

    @Before
    @Transactional
    public void initClass() {
        initUserCredit = UserCredit.newUserCredit(1000L);
        userCreditRepository.save(initUserCredit);

        initWithStaticUserCredit = UserCredit.newUserCredit(1001L);
        UserStaticCredit userStaticCredit = new UserStaticCredit(1001L,UserCreditType.USER_REGISTER);
        initWithStaticUserCredit.addUserStaticCredit(userStaticCredit);
        userCreditRepository.save(initWithStaticUserCredit);

        initWithDynamicUserCredit = UserCredit.newUserCredit(1002L);
        UserDynamicCredit userDynamicCredit = new UserDynamicCredit(1002L,UserCreditType.USER_PUBLISH);
        userDynamicCredit.buildTargetId(1000L);
        initWithDynamicUserCredit.addUserDynamicCredit(userDynamicCredit);
        userCreditRepository.save(initWithDynamicUserCredit);
    }

    @Test
    @Transactional
    public void testQueryByUserId() {

        UserId userId = new UserId(1001L);
        UserCredit userCredit = userCreditRepository.findByUserId(userId).get();
        Assert.assertEquals(userCredit.getUserId().id(),1001L);
        Assert.assertEquals(userCredit.getUserDynamicCredits().size(),0);
        Assert.assertEquals(userCredit.getUserStaticCredits().size(),1);

        userId = new UserId(1002L);
        UserCredit userCredit1 = userCreditRepository.findByUserId(userId).get();
        Assert.assertEquals(userCredit1.getUserId().id(),1002L);
        Assert.assertEquals(userCredit1.getUserDynamicCredits().size(),1);
        Assert.assertEquals(userCredit1.getUserStaticCredits().size(),0);

        userId = new UserId(1000L);
        UserCredit userCredit2 = userCreditRepository.findByUserId(userId).get();
        Assert.assertEquals(userCredit2.getUserId().id(),1000L);
        Assert.assertEquals(userCredit2.getUserDynamicCredits().size(),0);
        Assert.assertEquals(userCredit2.getUserStaticCredits().size(),0);
    }

    @After
    @Transactional
    public void removeClass(){
        System.out.println("id1:"+initUserCredit.getId());
        System.out.println("id2:"+initWithStaticUserCredit.getId());
        System.out.println("id3:"+initWithDynamicUserCredit.getId());
        userCreditRepository.delete(initUserCredit);
        userCreditRepository.delete(initWithStaticUserCredit);
        userCreditRepository.delete(initWithDynamicUserCredit);
    }

    @Test
    public void injectedComponentsAreNotNull(){
        Assert.assertNotNull(dataSource);
        Assert.assertNotNull(jdbcTemplate);
        Assert.assertNotNull(entityManager);
        Assert.assertNotNull(userCreditRepository);
    }
}

我们在@Before中插入一些不同的数据,然后在@After中执行数据的删除,同时在testQueryByUserId方法中执行不同的查询测试验证;

注意,我们在测试类中增加了一个注解@DataJpaTest这个注解可以验证injectedComponentsAreNotNull方法中的四个对象是否已经准备好进行测试。具体的业务逻辑就不用介绍啦。

欢迎各位进行讨论。

 

版权声明:著作权归作者所有。

X

欢迎加群学习交流

联系我们