我有我的父抽象JUnitTest类:
public abstract class RestWSTest
{
public RestWSTest()
{
}
@Before
public void setUp() throws Exception
{
...
}
@After
public void tearDown() throws Exception
{
...
}
}
然后我想有一个类扩展RestWSTest
,像这样:
public class RestWSCreateGroupTest extends RestWSTest
{
public RestWSCreateGroupTest()
{
super();
}
@Before
public void setUp() throws Exception
{
super(); --> *Constructor call must be the first statement in a constructor*
...
}
@After
public void tearDown() throws Exception
{
super(); --> *Constructor call must be the first statement in a constructor*
...
}
@Test
public void testCreateGroup()
{
...
}
}
为什么我得到错误信息?我有一个构造函数,我调用super()
,所以我不知道该怎么做。
方法public void setUp()
不是构造函数。你不能在里面调用super();
。我想你是想要super.setUp();
不能在构造函数方法之外使用super()调用
换句话说,setUp()和tearDown()是方法,它们不是构造函数,所以你不能使用super()调用。
相反,您可以使用以下语法访问/调用超类方法:super. mysuperclassmethod ();
那么修改代码如下:
public class RestWSCreateGroupTest extends RestWSTest
{
public RestWSCreateGroupTest()
{
super();
}
@Before
public void setUp() throws Exception
{
super.setUp();
...
}
@After
public void tearDown() throws Exception
{
super.tearDown();
...
}
@Test
public void testCreateGroup()
{
...
}
}
详情请参阅以下连结:https://docs.oracle.com/javase/tutorial/java/IandI/super.html