I'm trying to use setUp to initialise an array of objects in JUnit for my test methods but I'm doing something wrong as the tests throw up errors (null pointer exception). They run fine when I initialise arrays in the test methods themselves but this obviously isn't ideal. Can anyone point out what I'm doing wrong here?
我尝试使用安装程序在JUnit中初始化一个对象数组来测试我的测试方法,但是我做错了,因为测试抛出了错误(空指针异常)。当我在测试方法本身初始化数组时,它们运行良好,但这显然不是理想的。谁能指出我在这里做错了什么吗?
class MainTest {
Lord baratheons[];
Lord starks[];
//Setup & Teardown
@Before
static void setUp() throws Exception {
Lord baratheons[] = new Lord[3];
baratheons[0] = new Lord("Robert", 15);
baratheons[1] = new Lord("Renly", -5);
baratheons[2] = new Lord("Stannis", 30);
System.out.println("Baratheons initialised!");
Lord starks[] = new Lord[3];
starks[0] = new Lord("Robb", -60);
starks[1] = new Lord("Eddard", 0);
starks[2] = new Lord("Jon", 90);
System.out.println("Starks initialised!");
}
//Tests
@Test
void testGratefulLord() {
// Lord baratheons[] = new Lord[3];
// baratheons[0] = new Lord("Robert", 15);
int x = baratheons[0].getRelationship();
baratheons[0].giveFief();
assertEquals(baratheons[0].getRelationship(), (x+=10));
}
EDIT:
编辑:
N.B In addition to following the steps outlined in the below solutions, I'd like to note for posterity that I was also using the wrong tag for the setup. As this is JUnit 5, the tag is @BeforeEach. @Before is the tag for JUnit 4, which was why the setup method wasn't being called. I hope this is helpful to future users.
N。B除了遵循下面解决方案中列出的步骤之外,我还想提醒后代注意,我在设置时使用了错误的标记。由于这是JUnit 5,标签是@BeforeEach。@Before是JUnit 4的标记,这就是为什么没有调用setup方法。我希望这对未来的用户有帮助。
4 个解决方案
#1
3
The issue here is that you are re-declaring the arrays inside of your setUp()
method. This is messing with the Scope of the objects you want to use.
这里的问题是您正在重新声明setUp()方法中的数组。这破坏了您想要使用的对象的范围。
Remove the static
from the the setUp()
method as that is not needed.
从setUp()方法中删除不需要的静态。
Change your code from
改变你的代码
Lord baratheons[] = new Lord[3];
Lord starks[] = new Lord[3];
to
来
baratheons = new Lord[3];
starks = new Lord[3];
Lastly, you need to change your methods to be public
. Why? Because JUnit uses reflection behind the scenes and they need to be public for it to recognize. You can view the JUnit JavaDoc and see that is explicitly mentions a public void
method
最后,您需要更改方法以使其成为公共的。为什么?因为JUnit在幕后使用反射,它们需要公开,以便它识别。您可以查看JUnit JavaDoc,并看到它显式地提到了一个公共void方法
#2
1
Remove static
from setUp
method.
从setUp方法中移除静态。
Also
也
Lord baratheons[] = new Lord[3];
should just be
应该是
baratheons = new Lord[3];
Same goes for the starks
.
史塔克家族也是如此。
#3
0
The setUp() should not be static.
setUp()不应该是静态的。
The arrays you instantiate in setUp() method are assigned to local variables.
在setUp()方法中实例化的数组被分配给本地变量。
Lord baratheons[] = new Lord[3];
The variable you declare here is shadowing your class attribute. You should just remove the class here (Lord) to assign your new array to the good variable.
这里声明的变量是隐藏类属性。您应该在这里删除类(主),将新的数组分配给好变量。
baratheons = new Lord[3];
Also be careful with your assert statement:
还要注意你的断言声明:
assertEquals(baratheons[0].getRelationship(), (x+=10));
This statement is working but using '+=' syntax here may induce errors. In this case, the code is pretty simple but you'll gain in clarity if you extract the statement 'x+=10' if you want to change the value of x. If you don't need to change the value, you may use x+10 in place.
这个语句是有效的,但是使用'+='语法可能会导致错误。在这种情况下,代码非常简单,但如果您想要更改x的值,则提取“x+=10”语句,您将获得清晰的结果。
#4
0
The issue here is with scope of your variables baratheons
and starks
.
这里的问题是你的变量baratheons和starks的范围。
Have a read of the Java Specification on the Scope of a Declaration to better understand the issue.
请阅读关于声明范围的Java规范,以更好地理解这个问题。
Hope this helps you understand the issue a little more.
希望这能帮助你更好地理解这个问题。
Your code should be as follows:
您的代码应如下:
class MainTest {
private Lord[] baratheons;
private Lord[] starks;
//Setup & Teardown
@Before
public void setUp() throws Exception {
baratheons= new Lord[3];
baratheons[0] = new Lord("Robert", 15);
baratheons[1] = new Lord("Renly", -5);
baratheons[2] = new Lord("Stannis", 30);
System.out.println("Baratheons initialised!");
starks = new Lord[3];
starks[0] = new Lord("Robb", -60);
starks[1] = new Lord("Eddard", 0);
starks[2] = new Lord("Jon", 90);
System.out.println("Starks initialised!");
}
@Test
public void testGratefulLord() {
int x = baratheons[0].getRelationship();
baratheons[0].giveFief();
assertEquals(baratheons[0].getRelationship(), (x += 10));
}
}
#1
3
The issue here is that you are re-declaring the arrays inside of your setUp()
method. This is messing with the Scope of the objects you want to use.
这里的问题是您正在重新声明setUp()方法中的数组。这破坏了您想要使用的对象的范围。
Remove the static
from the the setUp()
method as that is not needed.
从setUp()方法中删除不需要的静态。
Change your code from
改变你的代码
Lord baratheons[] = new Lord[3];
Lord starks[] = new Lord[3];
to
来
baratheons = new Lord[3];
starks = new Lord[3];
Lastly, you need to change your methods to be public
. Why? Because JUnit uses reflection behind the scenes and they need to be public for it to recognize. You can view the JUnit JavaDoc and see that is explicitly mentions a public void
method
最后,您需要更改方法以使其成为公共的。为什么?因为JUnit在幕后使用反射,它们需要公开,以便它识别。您可以查看JUnit JavaDoc,并看到它显式地提到了一个公共void方法
#2
1
Remove static
from setUp
method.
从setUp方法中移除静态。
Also
也
Lord baratheons[] = new Lord[3];
should just be
应该是
baratheons = new Lord[3];
Same goes for the starks
.
史塔克家族也是如此。
#3
0
The setUp() should not be static.
setUp()不应该是静态的。
The arrays you instantiate in setUp() method are assigned to local variables.
在setUp()方法中实例化的数组被分配给本地变量。
Lord baratheons[] = new Lord[3];
The variable you declare here is shadowing your class attribute. You should just remove the class here (Lord) to assign your new array to the good variable.
这里声明的变量是隐藏类属性。您应该在这里删除类(主),将新的数组分配给好变量。
baratheons = new Lord[3];
Also be careful with your assert statement:
还要注意你的断言声明:
assertEquals(baratheons[0].getRelationship(), (x+=10));
This statement is working but using '+=' syntax here may induce errors. In this case, the code is pretty simple but you'll gain in clarity if you extract the statement 'x+=10' if you want to change the value of x. If you don't need to change the value, you may use x+10 in place.
这个语句是有效的,但是使用'+='语法可能会导致错误。在这种情况下,代码非常简单,但如果您想要更改x的值,则提取“x+=10”语句,您将获得清晰的结果。
#4
0
The issue here is with scope of your variables baratheons
and starks
.
这里的问题是你的变量baratheons和starks的范围。
Have a read of the Java Specification on the Scope of a Declaration to better understand the issue.
请阅读关于声明范围的Java规范,以更好地理解这个问题。
Hope this helps you understand the issue a little more.
希望这能帮助你更好地理解这个问题。
Your code should be as follows:
您的代码应如下:
class MainTest {
private Lord[] baratheons;
private Lord[] starks;
//Setup & Teardown
@Before
public void setUp() throws Exception {
baratheons= new Lord[3];
baratheons[0] = new Lord("Robert", 15);
baratheons[1] = new Lord("Renly", -5);
baratheons[2] = new Lord("Stannis", 30);
System.out.println("Baratheons initialised!");
starks = new Lord[3];
starks[0] = new Lord("Robb", -60);
starks[1] = new Lord("Eddard", 0);
starks[2] = new Lord("Jon", 90);
System.out.println("Starks initialised!");
}
@Test
public void testGratefulLord() {
int x = baratheons[0].getRelationship();
baratheons[0].giveFief();
assertEquals(baratheons[0].getRelationship(), (x += 10));
}
}