【问题标题】:Junit Jmock testing: multiple @Before for testsJunit Jmock 测试:多个@Before 用于测试
【发布时间】:2014-07-09 00:06:21
【问题描述】:

我不确定是否可以这样做,但我需要根据测试调用不同的@Before 方法。是否有可能为它制作一些解析器?

@Before
performBeforeOne();

@Before
performBeforeTwo();

@Test
callBeforeOneAndExecuteTestOne();

@Test
callBeforeTwoAndExecuteTestTwo();

或者我应该只创建几个方法并从每个测试中手动调用它们?

【问题讨论】:

    标签: java junit jmock


    【解决方案1】:

    不,每个生命周期注释只能有一个方法。创建一个复合方法,如果它们太大而无法组合,则调用其他方法。

    【讨论】:

    • 你可以有多个 @Before 方法。你只是不能保证它们的运行顺序。
    • 谢谢,我忘了没有订单 :(.
    【解决方案2】:

    我认为实现这一点的最好方法(也是最清晰的)是这样重构你的测试:

    @Before
    public void performBeforeForAll() {}
    
    @Test
    testOne() {
        testOneBefore();
       //.. test execution
    }
    
    @Test
    testTwo() {
       testTwoBefore();
       //.. test execution
    }
    
    private void testOneBefore() {}
    
    private void testTwoBefore() {}
    

    这样,您可以在每个测试运行之前准确地看到它的设置。你可能会发现一些测试共享相同的设置代码,你可以在其中有一个私有方法来防止重复。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-05
      • 2013-01-21
      • 1970-01-01
      • 1970-01-01
      • 2015-08-27
      • 1970-01-01
      相关资源
      最近更新 更多