-2
我有一些代碼,類似於:開始使用JUnit的,如何運行簡單的測試
public class Foo
{
private String Bar;
public Foo(String _bar)
{
Bar = new String(_bar);
}
public String getBar() { return Bar; }
@Override
public boolean equals(Object o)
{
if (o == this) return true;
if (!(o instanceof Foo)) return false;
Foo temp = (Foo)o;
if (Bar == null) return (Bar==temp.Bar);
return (Bar.equals(temp.Bar));
}
@Override
public int hashCode()
{
return Objects.hash(this.Bar);
}
}
我想嘗試寫一個測試用例該類這將考驗我的所有方法。我也試圖瞭解suite()方法以及如何使用TestRunner。
我該如何開始做這件事?
編輯1:固定以上
錯誤此外,測試類
public class FooTest
{
private FooTest ft;
@Before
public void setUp() throws Exception
{
ft = new FooTest("baz");
}
@Test
public void testGetFoo()
{
assertEquals(ft.getBar(),"baz");
}
@Test
public void testEquals()
{
assertEquals(ft.equals(new Foo("baz")), true);
assertEquals(ft.equals(new String("no")),false);
assertEquals(ft.equals(ft), true);
assertEquals(ft.equals(null),false);
}
}
我也碰到過這個網站(http://users.csc.calpoly.edu/~djanzen/tdl/tddintro/helloworld/),這是非常有幫助的。但是,我有問題的代碼的AllTests.java在頁面上的部分,具體是:
public static junit.framework.Test suite()
{
return new JUnit4TestAdapter(AllTests.class);
}
在Eclipse中,我得到了以下錯誤消息:
The method JUnit4TestAdapter(Class<Foo>) is undefined for the type AllTests
如何解決這個錯誤?
結帳https://stackoverflow.com/documentation/junit/topics –
謝謝你,這是一個很好的資源,但它很薄弱,並沒有什麼幫助的是理解我正在嘗試做什麼。 –
@Héctor你需要從簡單的東西開始,並從那裏建立。嘗試尋找https://github.com/junit-team/junit4/wiki/Getting-started – chocksaway