微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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");
  }
}

解决方法

您可以使用反射设置值.它不需要对生产代码进行任何更改.

ReflectionTestUtils.setField(YourClass.class,“fieldName”,fieldValue);

原文地址:https://www.jb51.cc/java/120723.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐