在Eclipse中使用JUnit 4.5時遇到了一些困難,當我使用@Before註解時它什麼都不做(我可能會使用setUp(),當然這很有用,但我只是想知道是錯誤的),雖然它在Netbeans中完美運行..任何想法?Eclipse JUnit @Before註解問題
回答
如果您使用的是JUnit 4,那麼只需使用@Test註釋來註釋測試類或測試方法,而不是擴展TestCase。
他在詢問@Before註釋,他沒有提到有關無法運行的測試的任何信息。 – 2012-01-18 19:59:57
儘管這是正確的,但這並沒有解決OP詢問的問題。 – 2014-07-20 08:29:08
因爲我在這裏凸輪通過谷歌搜索,並有挖非常深一點,看看實際的解決方案: 作爲@Pace在評論中說,如果你extend TestCase
,Eclipse的把測試作爲JUnit版本3或以上和不尊重@Before
註解 - 在這裏也descripred:JUnit + Maven + Eclipse: Why @BeforeClass does not work?
因此,由於您使用JUnit 4+有寫測試用例兩種方法去除extend TestCase
原因解決問題
1>您的測試課程爲extend TestCase
。在這種情況下,拾取對應於Junit 3的類別,其不知道@Before
註釋。在這種情況下,您必須覆蓋
/**
* Sets up the fixture, for example, open a network connection.
* This method is called before a test is executed.
*/
protected void setUp() throws Exception {
}
2>使用註釋。對測試類中的方法使用@Test
註釋,您有興趣將其作爲測試運行。您的班級不需要extend TestCase
。你也不必重寫任何方法。只需定義您自己的方法,即在測試方法運行之前具有要執行的邏輯,並使用@Before
註釋對其進行註釋。
- 1. Junit @Before註解給出Nullpointer異常
- 2. JUnit如何處理@Before註釋
- 3. JUnit測試用例@Before和@After註釋
- 4. 的JUnit @Before VS @rule
- 5. Junit @Before not working properly
- 6. Eclipse - JUnit/m2eclipse類路徑問題
- 7. JUnit執行程序@before
- 8. 何時使用@Pointcut&@Before,@AOP AOP註解
- 9. Junit,Testing,Flyway:您可以結合@Before和@Flyway註釋嗎?
- 10. 使用Spring和JUnit注入@Before和@Test之間的邏輯
- 11. 未解決的編譯問題junit
- 12. Jmeter Junit問題
- 13. Ant + Junit問題
- 14. 如何在Junit中使用註釋 - eclipse
- 15. 解決SQL注入問題
- 16. Java彈簧註解問題
- 17. Spring @Transactional註解問題
- 18. JPA註解的問題@Transient
- 19. Spring註解基本問題
- 20. Spring框架註解問題
- 21. 解決YII註銷問題?
- 22. MapKit註解的問題
- 23. 通過@Resource註解WebServiceContext注入問題
- 24. Groovy中的JUnit @Before方法排序
- 25. 春季安全,JUnit的:@WithUserDetails在@Before
- 26. 如何區分JUnit @Before和測試
- 27. 如何確保JUnit @Before附帶@After
- 28. JUnit測試問題
- 29. JUnit + Java + ErrorCollector問題
- 30. JUnit理論問題
發佈示例測試,以便我們可以看看 – skaffman 2010-06-12 20:53:55
您是否在擴展TestCase?如果是這樣,我認爲它忽略註釋。 – Pace 2010-06-12 20:53:58
你是否爲你的項目指定了好的jar? – Aif 2010-06-12 20:54:54