-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