问题描述
我正在使用JUnit 4,Maven 2和最新的Eclipse。问题很简单:在执行测试之前,我想执行一些设置(连接到数据库)。我在许多不同的位置尝试过@BeforeClass,但Eclipse和Maven正在忽略这个。任何有关完成初始设置的帮助?
谢谢!
注意:此问题与Eclipse或Maven无关。
>I am using JUnit 4, Maven 2 and latest Eclipse. Problem is simple: I would like to perform some setup (connecting to a database) before my tests are executed.
I tried @BeforeClass in many different locations but Eclipse and Maven are ignoring this. Any help on accomplishing this initial setup?
Thanks!
public abstract class BaseTestCase extends TestCase { @BeforeClass public static void doBeforeClass() throws Exception { System.out.println("No good @BeforeClass"); // DO THE DATABASE SETUP } }Now the tests extending BaseTestCase:
public class LoginActionTest extends BaseTestCase { @Test public void testNothing() { System.out.println("TEST HERE"); assertEquals(true, true); } }Maven and Eclipse just ignore my @BeforeClass ??? Any other way to perform setup before tests?
解决方案Sergio, you were right about extending TestCase causing the problem. If you extend TestCase, JUnit treats your test class as an old (pre JUnit 4 class) and picks org.junit.internal.runners.JUnit38ClassRunner to run it. JUnit38ClassRunner does not know about @BeforeClass annotation. Check out source code of runnerForClass method of AllDefaultPossibilitiesBuilder and runnerForClass method of JUnit3Builder for more details.
Note: This problem is not related to Eclipse or Maven.
这篇关于JUnit + Maven + Eclipse:为什么@BeforeClass不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!