Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RunWith and ContextConfiguration weird behaviour

I have this very simple class :

 @RunWith(SpringJUnit4ClassRunner.class)
 @ContextConfiguration(locations={"classpath*:/application-context-this-does-not-exist.xml"})
  public class HTMLSourceExtractorImplTest {

    @Autowired
    ApplicationContext context;

    @Test
    public void test(){
         String [] beans = context.getBeanDefinitionNames();
             for(String bean : beans){
                 System.out.println(bean);
             }
         System.out.println("Testing");
    }
}

This context file that is specified in classpath DOES NOT EXIST. I can put virtually any name I want and the code does not break. I mean the test runs just fine, as if that file really exists.

If I do a small change, from : classpath* to classpath , then it beaks, saying that this file does not exist, which is the behavior I would expect in the first case also.

Spring Version 3.2.3.RELEASE.

Can someone explain this weird behavior?

EDIT

Things from logs as suggested:

     20:47:26,923 INFO  [GenericApplicationContext] Refreshing org.springframework.context.support.GenericApplicationContext@3df6c65c: startup date [Fri Jun 07 20:47:26 PDT 2013]; root of context hierarchy

I even tried to output all beans from application context:

  org.springframework.context.annotation.internalConfigurationAnnotationProcessor
  org.springframework.context.annotation.internalAutowiredAnnotationProcessor
  org.springframework.context.annotation.internalRequiredAnnotationProcessor
  org.springframework.context.annotation.internalCommonAnnotationProcessor                   
  org.springframework.context.annotation.ConfigurationClassProcessor.importAwareProcessor

Seems to me that in case of a wildcard, Spring will create a default empty Application Context

like image 766
Eugene Avatar asked Jun 07 '13 13:06

Eugene


People also ask

What happened to the @RunWith annotation in JUnit?

In JUnit 5, the @RunWith annotation has been replaced by the more powerful @ExtendWith annotation. However, the @RunWith annotation can still be used in JUnit5 for the sake of the backward compatibility.

What is @contextconfiguration in Spring Boot?

@ContextConfiguration loads an ApplicationContext for Spring integration test. @ContextConfiguration can load ApplicationContext using XML resource or the JavaConfig annotated with @Configuration. The @ContextConfiguration annotation can also load a component annotated with @Component, @Service, @Repository etc.

How to load applicationcontext using @contextconfiguration?

@ContextConfiguration can load ApplicationContext using XML resource or the JavaConfig annotated with @Configuration. The @ContextConfiguration annotation can also load a component annotated with @Component, @Service, @Repository etc.

How to load multiple configuration classes using @contextconfiguration?

We use @ContextConfiguration as following. Find the sample test class. Find the print screen of the output. To load multiple configuration classes we can specify them as following. Here we will load XML configuration class.


1 Answers

Quote from JavaDoc will probably answer your question:

/**
 * Pseudo URL prefix for all matching resources from the class path: "classpath*:"
 * This differs from ResourceLoader's classpath URL prefix in that it
 * retrieves all matching resources for a given name (e.g. "/beans.xml"),
 * for example in the root of all deployed JAR files.
 * @see org.springframework.core.io.ResourceLoader#CLASSPATH_URL_PREFIX
 */
String CLASSPATH_ALL_URL_PREFIX = "classpath*:";

As there are no XML files matching the name application-context-this-does-not-exist.xml on your classpath, your configuration is equal to @ContextConfiguration(locations={}) => empty application context.

However when you use CLASSPATH_URL_PREFIX = "classpath:", that equals to saying "load this non-existing file" => error loading context configuration.

like image 51
Pavel Horal Avatar answered Sep 28 '22 01:09

Pavel Horal