Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 129765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:50:30+00:00 2026-05-11T05:50:30+00:00

Should I initialize class fields at declaration like this? public class SomeTest extends TestCase

  • 0

Should I initialize class fields at declaration like this?

public class SomeTest extends TestCase {     private final List list = new ArrayList();      public void testPopulateList()     {         // Add stuff to the list         // Assert the list contains what I expect     } } 

Or in setUp() like this?

public class SomeTest extends TestCase {     private List list;      @Override     protected void setUp() throws Exception     {         super.setUp();         this.list = new ArrayList();     }      public void testPopulateList()     {         // Add stuff to the list         // Assert the list contains what I expect     } } 

I tend to use the first form because it’s more concise, and allows me to use final fields. If I don’t need to use the setUp() method for set-up, should I still use it, and why?

Clarification: JUnit will instantiate the test class once per test method. That means list will be created once per test, regardless of where I declare it. It also means there are no temporal dependencies between the tests. So it seems like there are no advantages to using setUp(). However the JUnit FAQ has many examples that initialize an empty collection in setUp(), so I figure there must be a reason.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T05:50:30+00:00Added an answer on May 11, 2026 at 5:50 am

    If you’re wondering specifically about the examples in the JUnit FAQ, such as the basic test template, I think the best practice being shown off there is that the class under test should be instantiated in your setUp method (or in a test method).

    When the JUnit examples create an ArrayList in the setUp method, they all go on to test the behavior of that ArrayList, with cases like testIndexOutOfBoundException, testEmptyCollection, and the like. The perspective there is of someone writing a class and making sure it works right.

    You should probably do the same when testing your own classes: create your object in setUp or in a test method, so that you’ll be able to get reasonable output if you break it later.

    On the other hand, if you use a Java collection class (or other library class, for that matter) in your test code, it’s probably not because you want to test it–it’s just part of the test fixture. In this case, you can safely assume it works as intended, so initializing it in the declaration won’t be a problem.

    For what it’s worth, I work on a reasonably large, several-year-old, TDD-developed code base. We habitually initialize things in their declarations in test code, and in the year and a half that I’ve been on this project, it has never caused a problem. So there’s at least some anecdotal evidence that it’s a reasonable thing to do.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 159k
  • Answers 159k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You could do it the quick-way: $(".winning-col", this) .text($("td.win", this).length)… May 12, 2026 at 11:29 am
  • Editorial Team
    Editorial Team added an answer Because of how data-binding works, this type of 2-way binding… May 12, 2026 at 11:29 am
  • Editorial Team
    Editorial Team added an answer Sure. function CheckStuff($arr) { foreach($arr as $key => $val) {… May 12, 2026 at 11:29 am

Related Questions

I'm coming from a C# background and I really like the type inference that
I've been programming in C# and Java recently and I am curious where the
I've written an abstract base class for unit tests that sets up just enough
I often find myself writing a property that is evaluated lazily. Something like: if

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.