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 6184281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:30:38+00:00 2026-05-24T01:30:38+00:00

As a beginner in Test Driven Development I just encountered a problem. My test

  • 0

As a beginner in Test Driven Development I just encountered a problem. My test class begins as follows:

@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
@DirtiesContext
@ContextConfiguration(locations = {"/web-test.xml"})
public class XXTest {

  @Autowired
  XX xx;

  @Autowired
  HibernateTemplate template;

  @Test
  public void testSetGetXXValue() throws Exception {
    final Map<String, YY> profilMap = new HashMap<String, YY>(2);
    profilMap.put("1", new YY());
    profilMap.put("2", new YY());

    simpleCockpit.setValues(profilMap);

    assertEquals(profilMap, simpleCockpit.getValues());
  }

As you can see, the first test method alters the autowired XX-class. That affects all following test methods, which relies on XX having the autowired-values.

How can I test getter and setter from XX AND make sure XX has the autowired values for the rest of the test methods?

Thoughts:

  • Reset the right values at the end of test method. Bad because if the getter / setter are not working, this will also not work.
  • Place the first test method at the end of the test class. Bad because that makes the tests dependent on their execution order.
  • Do not test the getter / setter of XX. Bad because getter / setter have to be tested like every method.

Thanks for you answers! I`m pretty sure this has an easy solution … 🙂

EDIT: Regarding the questions whether unit testing getters/setters or not, I decided to do so mainly because of the reasons statet at http://www.sundog.net/sunblog/posts/should-we-test-getters-and-setters/ .

  • 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. Editorial Team
    Editorial Team
    2026-05-24T01:30:38+00:00Added an answer on May 24, 2026 at 1:30 am

    If you modify an spring managed bean, then you could use the @DirtiesContext Annotation. This Annotation can be put on Test Classes as well as on Test Methods!

    From @DirtiesContext Java Doc:

    Test annotation which indicates that the {@link
    org.springframework.context.ApplicationContext ApplicationContext}
    associated with a test is dirty and should be closed:

    • after the current test, when declared at the method level
    • after each test method in the current test class, when declared at the class
      level with class mode set to {@link ClassMode#AFTER_EACH_TEST_METHOD
      AFTER_EACH_TEST_METHOD}
    • after the current test class, when declared
      at the class level with class mode set to {@link ClassMode#AFTER_CLASS
      AFTER_CLASS}

    And even in Test Driven Development (to my understanding): write explicite tests only for stuff that has a minimum complexity. So I never write explicite tests for getter and setter. I normally have a test that checks some functionality, and when this functionality needs the getter and setter so I write this getter and setter (at this point in time) and that they works will be checked by the functionality I started with implicit.


    Especially in your case: why do you use the Spring Bean, why not using “normal” Objects created with new. I use the “normal” classes as long as it is usefull for the tests, mostly for simple tests. I use Spring Beans for “bigger” tests as well.

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

Sidebar

Related Questions

After spending some hours reading about Unit Testing and test driven development here on
I am writing a test code (I'm just a beginner) where I need to
I'm a beginner using ANTLR. I'm just doing the following for test purposes and
I'm looking for suggestions for a beginner level ASP/XML test. Essentially I'm trying to
A beginner question, bear with me: I'm just wondering under what circumstances one should
The very common beginner mistake is when you try to use a class property
[Edit] Solved, small mistake done by me being an ant beginner. In the test
I'm an absolute beginner to programming and i'm just doing some exercises exercises for
I a unit-testing beginner and seem to be stuck on how to unit test
I have barely created a script (as I'm just a beginner in ajax), in

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.