简体   繁体   English

静态或非静态方法中的 JUnit 测试数据

[英]JUnit test data in static or non static methods

Like to know which one is good.想知道哪个好。

I have to keep some JUnit test data in a different file.我必须将一些 JUnit 测试数据保存在不同的文件中。 Lets call it as TestingData.java .让我们称之为TestingData.java I thought of doing it in two ways.我想到了两种方法。

First Way第一种方式

TestingData.java测试数据.java

public class TestingData {
  protected String getHelloWorld() {
    return "Hello World";
  }
}

Second Way第二种方式

TestingData.java测试数据.java

public class TestingData {
  public static String getHelloWorld() {
    return "Hello World";
  }
}

I can call the First Way like this in my testing service by extends TestingData class我可以通过 extends TestingData类在我的测试服务中像这样调用第一种方式

@RunWith(MockitoJUnitRunner.class)
public class SomeServiceTest extends TestingData {
  @Test
  public void helloWorldTest() {
    assertEquals("Hello World", getHelloWorld());
  }
}

calling Second Way in my testing service by calling the static function TestingData.getHelloWorld()通过调用静态函数TestingData.getHelloWorld()在我的测试服务中调用Second Way

@RunWith(MockitoJUnitRunner.class)
public class SomeServiceTest {
  @Test
  public void helloWorldTest() {
    assertEquals("Hello World", TestingData.getHelloWorld());
  }
}

Like to know which way is better in clean code principle想知道在干净的代码原则中哪种方式更好

The answer might not be related to "clean code", but rather be different depending on your current and future use cases.答案可能与“干净的代码”无关,而是根据您当前和未来的用例而有所不同。

Extending the fixture class in the First Way creates an important limitation - you won't be able to extend any other base class, and such case happens quite often in unit testing.第一种方式扩展夹具类会产生一个重要的限制 - 您将无法扩展任何其他基类,并且这种情况在单元测试中经常发生。

A static method in the Second Way can't be overridden.第二种方式中的静态方法不能被覆盖。

You could use a Third Way , an interface with a default method.你可以使用第三种方式,一个带有默认方法的接口。 This won't limit you to a specific base class, and its default method may be overridden if you need this in the future:这不会限制你使用特定的基类,如果你将来需要它,它的默认方法可能会被覆盖:

public interface TestingData {
  default String getHelloWorld() {
    return "Hello World";
  }
}

@RunWith(MockitoJUnitRunner.class)
public class SomeServiceTest implements TestingData {
  @Test
  public void helloWorldTest() {
    assertEquals("Hello World", getHelloWorld());
  }
}

If you have multiple test classes sharing the same parent.如果您有多个测试类共享同一个父类。 you can define abstract class and puts all the common attributes and behavoirs.您可以定义abstract类并放置所有公共属性和行为。

, And I would recommend to declare the static content as static final not be hard coded. ,并且我建议将静态内容声明为static final而不是硬编码。

in this way you can choose to create a method and call it from the child in each test cases, Like this:通过这种方式,您可以选择创建一个方法并在每个测试用例中从子级调用它,如下所示:

public abstract class TestingData {
  
  private static final GET_HELLO_WORLD = "Hello World";

  public void setUp() {
      // common setUp
  }

  protected String getHelloWorld() {
    return GET_HELLO_WORLD;
  }
}
@RunWith(MockitoJUnitRunner.class)
public class SomeServiceTest extends TestingData {

  @Before
  public void setUp() {
      super.setUp();
      // Add more settings
  }
  
  @Test
  public void helloWorldTest() {
    assertEquals("Hello World", getHelloWorld());
  }
}

, Or just call the constants from the child directly , 或者直接从孩子那里调用常量

public abstract class TestingData {
  
  protected static final GET_HELLO_WORLD = "Hello World";

  public void setUp() {
      // common setUp
  }
}
@RunWith(MockitoJUnitRunner.class)
public class SomeServiceTest extends TestingData {

  @Before
  public void setUp() {
      super.setUp();
      // Add more settings
  }
  
  @Test
  public void helloWorldTest() {
    assertEquals(callTargetTestMethod(), GET_HELLO_WORLD);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM