JUnit Independent Tests with @Autowired Springs

As a newbie to the development under test, I just ran into a problem. My test class starts 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 changes the auto-level class XX. This affects all subsequent testing methods that are based on XX, which have auto-support values.

How can I test getter and setter from XX and make sure that XX has values ​​for all other testing methods?

Thoughts:

  • Reset the correct values ​​at the end of the test method. Bad, because if the getter / setter does not work, this also will not work.
  • Put the first test method at the end of the test class. Bad because it makes tests dependent on their order of execution.
  • Do not check getter / setter XX. Bad, because getter / setter needs to be tested, like every method.

Thanks for answers! I am sure this is a simple solution ... :)

EDIT . Regarding questions about whether unit tests were getters / seters or not, I decided to do this mainly due to statet reasons at http://www.sundog.net/sunblog/posts/should-we-test-getters- and-setters / .

+6
source share
1 answer

If you changed the spring bean spring, you can use the @DirtiesContext annotation. This annotation can be placed on test classes, as well as on test methods!

From @DirtiesContext Java Doc:

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

  • after the current test declared at the method level
  • after each test method in the current test class declared in 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 (in my opinion): write explicite tests only for things that have minimal complexity. Therefore, I never write express tests for getters and setters. Usually I have a test that checks some functionality, and when this function needs getter and setter, so I write this getter and setter (at the moment), and that they work, will be checked using the functions that I started with implicit.


Especially in your case: why are you using spring bean, why not use the "regular" objects created with new . I use "regular" classes if this is useful for tests, mainly for simple tests. I use spring beans for "big" tests.

+7
source

Source: https://habr.com/ru/post/893232/


All Articles