如何使用@Resource 对布尔字段进行单元测试 [英] How to unit test Boolean field with @Resource

查看:54
本文介绍了如何使用@Resource 对布尔字段进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测试某个类.那个类有带有@Resource 的布尔字段.我不能模拟这个字段.因此它给测试失败并出现一些错误.如果有人能告诉我如何测试这个类.

I want to test some class.That class have boolean field with @Resource.I can't mock this field. Therefor it give test failed with some error.if anyone can tell me how can I test this class.

这是我的java类

public class RefreshHandlerImpl implements RefreshHandler
{
  @Resource(name = "readOnlyMode")
  private Boolean readOnlyMode;


  @Override
  public ContactBedRefreshResult refreshContactsAndBeds(final Unit unit, final boolean hasWritableTransaction)
throws RequiresWritableTransactionException
  {

    if (!isReadOnlyMode())
    {
      //some code here
    }

  }



  private boolean isReadOnlyMode()
  {
    return readOnlyMode;
  }

}

我尝试模拟readOnlyMode"字段.但它给出了错误.

I try to mock "readOnlyMode" field.But it give error.

org.mockito.exceptions.base.MockitoException:无法模拟/监视类 java.lang.BooleanMockito 不能模拟/监视以下内容:- 期末班- 匿名类- 原始类型

org.mockito.exceptions.base.MockitoException: Cannot mock/spy class java.lang.Boolean Mockito cannot mock/spy following: - final classes - anonymous classes - primitive types

这是我的testng测试类

This is my testng test class

public class RefreshHandlerImplTest
{

 @Mock(name = "readOnlyMode")
 private Boolean readOnlyMode;

 @InjectMocks
 private RefreshHandlerImpl refreshHandlerImpl;

 @BeforeMethod
 public void setUp() throws Exception {
   initMocks(this);
 }

 @Test
 public void testRefreshContactsAndBeds_ReturnsZeroContactsWhenCollaboratorsDoes()
  throws Exception
 {
   ContactBedRefreshResult result = refreshHandlerImpl.refreshContactsAndBeds(unit, true);
   assertThat(result.getContacts()).isEmpty();
 }
}

我可以使用反射然后它是如何使用的吗?我不能改变我的java类.只能改变测试类.

Can I use reflection and then how it use?I can't change my java class.Only can change test class.

推荐答案

我使用 org.springframework.test.util.ReflectionTestUtils 修复了这个问题

I fixed the issue using org.springframework.test.util.ReflectionTestUtils

我删除了@Mock(name = "readOnlyMode")private Boolean readOnlyMode;并在我的@BeforeMethod 方法中使用了ReflectionTestUtils.setField(refreshHandlerImpl,RefreshHandlerImpl.class,"readOnlyMode",true,Boolean.class);.这是我的测试课,

I removed@Mock(name = "readOnlyMode") private Boolean readOnlyMode; and usedReflectionTestUtils.setField(refreshHandlerImpl,RefreshHandlerImpl.class,"readOnlyMode",true,Boolean.class); in my @BeforeMethod method. This is my test class,

public class RefreshHandlerImplTest
{
 @InjectMocks
 private RefreshHandlerImpl refreshHandlerImpl;

 @BeforeMethod
 public void setUp() throws Exception {
  initMocks(this);
  ReflectionTestUtils.setField(refreshHandlerImpl,RefreshHandlerImpl.class,"readOnlyMode",true,Boolean.class);
 }

 @Test
 public void testRefreshContactsAndBeds_ReturnsZeroContactsWhenCollaboratorsDoes() throws Exception
 {
   ContactBedRefreshResult result = 
   refreshHandlerImpl.refreshContactsAndBeds(unit, true);
   assertThat(result.getContacts()).isEmpty();
 }
}

这篇关于如何使用@Resource 对布尔字段进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆