@After ,@before 不在测试用例中工作 [英] @After ,@before not working in testcase

查看:34
本文介绍了@After ,@before 不在测试用例中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始测试,现在我想使用 @After@Before@Test 但我的应用程序只运行 @Before 方法并在控制台上给出输出

I have started testing and now i want to use @After, @Before and @Test but my application only runs the @Before method and gives output on console

之前

但是,如果我删除 @After@Before,它会运行 @Test.我的代码在这里:

However, if I remove @After and @Before it runs the @Test. My code is here:

public class TestPractise extends AbstractTransactionalDataSourceSpringContextTests{

    @Before
    public void runBare(){
        System.out.println("before");
    }

    @Test
    public void testingMethod(){
        System.out.println("testing");
    }

    @After
    public void setDirty(){
        System.out.println("after");
    }
}

为什么 @After@Test@before 不能同时工作?

Why aren't @After, @Test and @before working simultaneously?

推荐答案

AbstractTransactionalDataSourceSpringContextTests 类强制使用旧的 JUnit 3.x 语法,这意味着任何 JUnit 4 注释都不会工作.

The AbstractTransactionalDataSourceSpringContextTests class forces the use of the old JUnit 3.x syntax, which means that any of the JUnit 4 annotation will not work.

你的方法 runBare() 被执行不是因为 @Before 注释,而是因为它被命名为 runBare(),它是ConditionalTestCase 和 JUnit 提供的方法TestCase 类.

Your method runBare() is executed not because of the @Before annotation, but because it is named runBare(), which is a method provided by ConditionalTestCase and JUnit TestCase class.

所以你有两个解决方案:

So you have 2 solutions:

  • 使用 AlexR 答案来使用 JUnit 4 测试和 Spring;
  • 保留对 AbstractTransactionalDataSourceSpringContextTests 的继承,但使用 onSetUponTearDown 方法而不是 @Before@After 方法.
  • Use the AlexR answer to use JUnit 4 tests and Spring;
  • Keep your inheritance of AbstractTransactionalDataSourceSpringContextTests, but use the onSetUp and onTearDown methods instead of the @Before and @After methods.

这篇关于@After ,@before 不在测试用例中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆