在 JUnit (4.8.1) 测试套件中分配 static final int

发布于 2024-08-26 00:45:22 字数 657 浏览 2 评论 0原文

我有一个 JUnit 测试类,其中有几个 static final int ,可以在测试器代码的顶部重新定义它们,以允许测试值发生一些变化。我的 @BeforeClass 方法中有逻辑,以确保开发人员输入的值不会破坏我的测试。

如果开发人员设置了 boolean useRandomValues = true;,我希望通过允许在 @BeforeClass 方法中将这些整数设置为(合理的)随机值来进一步改进变化。我可以简单地删除 final 关键字以允许随机值覆盖初始化值,但我有 final 来确保这些值不会无意中更改,正如一些测试一样依赖于这些值的一致性。

  1. 我可以在 JUnit 测试类中使用构造函数吗?如果我尝试将 @BeforeClass 放入测试类的构造函数中,并且创建单独的构造函数似乎不允许对这些变量进行赋值(即使我在声明时将它们保留为未赋值),则 Eclipse 会开始在各处添加红色下划线;< /p>

  2. 是否有另一种方法可以确保在 @BeforeClass 方法之后更改这些变量的任何尝试都会导致编译时错误?

    是否有其他方法可以确保在 @BeforeClass 方法之后
  3. 我可以在初始化后做一些最终的事情吗?

I have a JUnit test class in which I have several static final ints that can be redefined at the top of the tester code to allow some variation in the test values. I have logic in my @BeforeClass method to ensure that the developer has entered values that won't break my tests.

I would like to improve variation further by allowing these ints to be set to (sensible) random values in the @BeforeClass method if the developer sets a boolean useRandomValues = true;. I could simply remove the final keyword to allow the random values to overwrite the initialisation values, but I have final there to ensure that these values are not inadvertently changed, as some tests rely on the consistency of these values.

  1. Can I use a constructor in a JUnit test class? Eclipse starts putting red underlines everywhere if I try to make my @BeforeClass into a constructor for the test class, and making a separate constructor doesn't seem to allow assignment to these variables (even if I leave them unassigned at their declaration);

  2. Is there another way to ensure that any attempt to change these variables after the @BeforeClass method will result in a compile-time error?

  3. Can I make something final after it has been initialised?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪冰清 2024-09-02 00:45:23

您可以使用静态构造函数来执行此操作:

import java.util.Random;
public class StaticRandom
{
  private static final boolean useRandomValues = true;
  private static final Random r = new Random();
  private static final int value1;
  private static final int value2;
  private static final int value3;
  static
  {
    if(useRandomValues)
    {
      value1 = r.nextInt();
      value2 = r.nextInt();
      value3 = r.nextInt();
    }
    else
    {
      value1 = 0;
      value2 = 0;
      value3 = 0;
    }
  }
}

You can do this with a static constructor:

import java.util.Random;
public class StaticRandom
{
  private static final boolean useRandomValues = true;
  private static final Random r = new Random();
  private static final int value1;
  private static final int value2;
  private static final int value3;
  static
  {
    if(useRandomValues)
    {
      value1 = r.nextInt();
      value2 = r.nextInt();
      value3 = r.nextInt();
    }
    else
    {
      value1 = 0;
      value2 = 0;
      value3 = 0;
    }
  }
}
追我者格杀勿论 2024-09-02 00:45:23

您可以使用静态初始值设定项:

final static boolean useRandomValues = true;

final static int valueA;
final static int valueB;
static {
    if(!useRandomValues) {
        valueA = 42;
        valueB = 1337;
    }
    else {
        Random rnd = new Random();
        valueA = rnd.nextInt(100);
        valueB = rnd.nextInt(100);
    }
}

You can use a static initializer:

final static boolean useRandomValues = true;

final static int valueA;
final static int valueB;
static {
    if(!useRandomValues) {
        valueA = 42;
        valueB = 1337;
    }
    else {
        Random rnd = new Random();
        valueA = rnd.nextInt(100);
        valueB = rnd.nextInt(100);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文