简体   繁体   中英

Is there a common way to validate if a (xml based) spring configuration is valid?

Question: Is there a common way to validate if a (xml based) spring configuration is valid?

Further explanation: With "valid" I mean not if the xml itself is valid (I don't talk about xsd validation), I mean more "logical valid", eg if all referenced classes are available, or if a specific reference is available / could be resolved.

The background of this question is a QA-process within a CI-environment for a spring-mvc application:

Assuming a developer have a typo in a class name, or a reference is not unique in a webcontext configuration file, and he commits this change. Now an automated build is triggered:

  1. The application compiles successfully
  2. unit tests are "green" (since they don't need any spring configuration)
  3. integration tests are "green" (since integration tests does not rely on webcontext configurations)
  4. functional / regression testing starts

In current setup we would note this simple typo in step 4 - but it is quite time consuming to reach this point.

It would be great to have a mechanism / tool which can validate if a spring context could be loaded in order to save some time.

integration tests does not rely on webcontext configurations

My integration tests run against the deployed application. If you want to test the web context then... test it. Specifics depends on what you want to test.

  • You can use Maven or whatever build tool you have to deploy your app in a test environment and run the integration tests afterwards.

  • You can use a lightweight server like Grizzly , reuse it across test classes etc.

Since those tests are costly I usually have one that checks the app can be deployed and the context starts. I use Grizzly and run this test along the rest of the Unit tests so issues are detected asap. Other similar test cases can be added depending on the situation.

A simple way to "pre-validate" the XML configuration before integration testing would be to use a JUnit test using SpringJUnit4ClassRunner . This is available in the spring-test JAR.

eg

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/foo.xml" })
public class XmlValidate {

    @Test
    public void loadsWithoutError() {
        // nothing to do, SpringJUnit4ClassRunner will throw exception
        // if there are any class-not-found exceptions etc.
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM