java中设置字段为必填_java – 最佳实践 – 在单元测试中设置不带setter的字段

假设您要测试以下课程:

public class SomeService {

public String someMethod(SomeEntity someEntity) {

return someEntity.getSomeProperty();

}

}

SomeEntity看起来像这样:

public class SomeEntity {

private String someProperty;

public getSomeProperty() {

return this.someProperty;

}

}

您想要做的断言可能如下:

String result = someService.someMethod(someEntity);

assertThat(result).isEqualTo("someValue");

你怎么能让这个测试工作?

1)在SomeEntity类中为’someProperty’添加一个setter.我不认为这是一个很好的解决方案,因为您不会更改生产代码以使测试工作.

2)使用ReflectionUtils设置此字段的值.测试看起来像这样:

public class TestClass {

private SomeService someService;

@Test

public void testSomeProperty() {

SomeEntity someEntity = new SomeEntity();

ReflectionTestUtils.setField(someEntity,"someProperty","someValue");

String result = someService.someMethod(someEntity);

assertThat(result).isEqualTo("someValue");

}

}

3)在测试类中创建一个扩展SomeEntity类的内部类,并为该字段添加setter.但是,要实现此功能,您还需要更改SomeEntity类,因为该字段应该变为“受保护”而不是“私有”.测试类可能如下所示:

public class TestClass {

private SomeService someService;

@Test

public void testSomeProperty() {

SomeEntityWithSetters someEntity = new SomeEntityTestWithSetters();

someEntity.setSomeProperty("someValue");

String result = someService.someMethod(someEntity);

assertThat(result).isEqualTo("someValue");

}

public class SomeEntityWithSetters extends SomeEntity {

public setSomeProperty(String someProperty) {

this.someProperty = someProperty;

}

}

}

4)你使用Mockito来模拟SomeEntity.如果您只需要在类中仅模拟一个属性,那么似乎很好,但如果您需要像10个属性那样进行模拟,那该怎么办呢.测试可能如下所示:

public class TestClass {

private SomeService someService;

@Test

public void testSomeProperty() {

SomeEntity someEntity = mock(SomeEntity.class);

when(someEntity.getSomeProperty()).thenReturn("someValue");

String result = someService.someMethod(someEntity);

assertThat(result).isEqualTo("someValue");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值