问题描述
我已开始测试,现在我想使用 @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");
}
}
为什么不是 @在
之后, @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 class。
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.
所以你有2个解决方案:
So you have 2 solutions:
- 使用AlexR答案使用JUnit 4测试和Spring;
- 保留
AbstractTransactionalDataSourceSpringContextTests
的继承,但是使用onSetUp
和onTearDown
方法而不是@Before
和@After
方法。
- Use the AlexR answer to use JUnit 4 tests and Spring;
- Keep your inheritance of
AbstractTransactionalDataSourceSpringContextTests
, but use theonSetUp
andonTearDown
methods instead of the@Before
and@After
methods.
这篇关于@After,@之前没有在testcase中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!