How to check for errors in Spring configuration?

In recent years, I have been working on Java-based web applications using the Spring MVC framework. Projects have good testing coverage with JUnit and Selenium. However, in two cases, errors in the Spring configuration went through the testing process.

In one case, the change was made to the parent bean in the controllerContext.xml file, which also required a change for the two inheriting beans. But the required change was made only for one of the two inheriting beans. The error was visible only in a small but important part of the web application. Selenium UA tests were later extended to test this directly in the web application. before deployment, but the damage has already been done since the mistake turned it into a living environment.

In another case, the property required to set the data format was not correctly entered through applicationContext.xml. The only visible error was in the date format of the generated report downloaded from the web application. Hard to check with selenium.

One of the benefits of using Spring MVC is the ability to set the injected objects into your JUnit tests (i.e. the layout of the object), but this does not tell you what you are actually going to type when the application is running in a live environment.

The answer may be integration testing, but setting up and running integration testing in the past proved difficult ... but this is another question ...

So, I would be very interested to know how people tried to catch the possible errors made to the Spring configuration files.

So my question is:

What is the best way to check for errors in a Spring configuration?

+6
source share
3 answers

The type of errors you describe will only be checked when someone thinks / remembers to check these conditions:

In one case, the change was made to the parent bean in the controllerContext.xml file, which also required a change for the two inheriting beans. But the required change was made only for one of the two inheriting beans. The error was visible only in a small but important part of the web application. Selenium UA tests were later extended to test this directly in the web application. before deployment, but the damage has already been done since the mistake turned it into a living environment.

In another case, the property required to set the data format was not correctly entered through applicationContext.xml. The only visible error was in the date format of the generated report downloaded from the web application. Hard to check with selenium.

In both cases, you have a developer who made changes without verifying that there are no errors wherever changes occur. How can you catch this type of error in a JUnit-style test without relying on the person making this change to explicitly add a test for the error they are making? In other words, you can catch only these types of errors when you do not check them.

Personally, I think the best way to catch bugs like this is with selenium-like tests that actually invoke the application and claim the correct behavior.

But if your tests do not know the correct behavior for testing, you cannot catch errors like this - in order to catch errors, you must first understand what errors need to be caught.

In other words, you cannot verify that the correct values ​​are entered without a test, knowing what the correct values ​​are. These tests will not catch scripts when what someone considers the correct value is actually incorrect.

+1
source

This test case will do the magic

import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @ContextConfiguration(locations={"/server-application-context.xml"}) @RunWith(SpringJUnit4ClassRunner.class) public class SpringConfigurationTest { @Test public void testSpringConfiguration() { } } 
+4
source

@ContextConfiguration(..) and @RunWith(SpringJUnit4ClassRunner.class) in the test class

This will load the entire context. You will need the @Test dummy for the context to load.

+1
source

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


All Articles