问题
I wrote a simple test code. It is a circle. I suppose most people can image what is a circle class, so I will not paste it.
In the test code, I try to test the circle constructor with invalid point, and assume to throw an exception. But a bug occures. I check online, but still do not know how to solve the problem. Is there any one can help me? Thanks
code information, bug is in the last sentence of following code
/**
* Tests that the Circle constructor throws an exception for center Point.
*/
@Test (expected = IllegalArgumentException.class)
public void testIllegalCenter() {
//Instantiates a circle with an incorrect center point.
@SuppressWarnings("unused")
final Circle testCircle = new Circle(VALID_RADIUS, INVALID_POINT, VALID_COLOR);
}
bug report
Bug: Dead store to testCircle in CircleTest.testIllegalCenter()
This instruction assigns a value to a local variable, but the value is not read or used in any subsequent instruction. Often, this indicates an error, because the value computed is never used.
Note that Sun's javac compiler often generates dead stores for final local variables. Because FindBugs is a bytecode-based tool, there is no easy way to eliminate these false positives.
回答1:
Just remove the variable and call the constructor like this:
@Test (expected = IllegalArgumentException.class)
public void testIllegalCenter() {
new Circle(VALID_RADIUS, INVALID_POINT, VALID_COLOR);
}
来源:https://stackoverflow.com/questions/23004309/how-to-deal-with-bug-dead-store-to-local-variable-in-java