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在控制台上
前
但是,如果我删除 @After
和 @Before
它运行@Test。我的代码在这里:
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
同时工作?
原文由 abhishek ameta 发布,翻译遵循 CC BY-SA 4.0 许可协议
AbstractTransactionalDataSourceSpringContextTests
类强制使用旧的 JUnit 3.x 语法,这意味着任何 JUnit 4 注释都不起作用。Your method
runBare()
is executed not because of the@Before
annotation, but because it is namedrunBare()
, which is a method provided byConditionalTestCase
和 JUnitTestCase
类。所以你有2个解决方案:
AbstractTransactionalDataSourceSpringContextTests
, but use theonSetUp
andonTearDown
methods instead of the@Before
and@After
methods .