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:
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/ .
Also note that we can wire other spring beans in our jUnit test classes using @Autowired annotation.
To check the Service class, we need to have an instance of the Service class created and available as a @Bean so that we can @Autowire it in our test class. We can achieve this configuration using the @TestConfiguration annotation.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With