I am trying to write integration test with spring. Below is the test class
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath*:**/context*.xml"})
public class MyFirstTestClass {
@AutoWired
private ApplicationContext applicationContext;
@Test
public void testApplicationContext {
applicationContext.getName();
}
}
When I run the test from maven I am getting NullPointerException
because applicationContext
is null
. I am not able to understand the reason. Also in logs also i don't see any error. I tried putting configuration <context:annotation-config/>
in one of the application context files. Still I am getting same error.
When i use classpath*:**/context*.xml to create context manually it works. But i believe when i use context configuraton its not loading the application context file.I am not getting any error as well. Kindly suggest
Okay i managed to load the application context . but now the iam getting NoSuchBeanDefinition
exception.
bean file
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="messageService"
class="com.mycompany.app.MessageService">
</bean>
</beans>
Test class
package com.mycompany.app;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import junit.framework.TestSuite;
@ContextConfiguration(locations={"classpath*:*/AppTest.context.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class AppTest
{
@Autowired
private ApplicationContext applicationContext;
public AppTest( )
{
}
@Test
public void testApp(){
assertNotNull(applicationContext);
for(String str : applicationContext.getBeanDefinitionNames()){
System.out.println("Bean feifniaio count"+str);
}
applicationContext.getBean("messageService");
}
}
Log after execution of test
INFO: Refreshing org.springframework.context.support.GenericApplicationContext@196751b: startup date [Fri Nov 20 00:00:11 IST 2015]; root of context hierarchy Bean feifniaio countorg.springframework.context.annotation.internalConfigurationAnnotationProcessor Bean feifniaio countorg.springframework.context.annotation.internalAutowiredAnnotationProcessor Bean feifniaio countorg.springframework.context.annotation.internalRequiredAnnotationProcessor Bean feifniaio countorg.springframework.context.annotation.internalCommonAnnotationProcessor Bean feifniaio countorg.springframework.context.event.internalEventListenerProcessor Bean feifniaio countorg.springframework.context.event.internalEventListenerFactory Bean feifniaio countorg.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor Bean feifniaio countorg.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor
I am not able to see message service bean. What could be the issue??
Finally i identified the issue. There was issue with my context file. I placed an empty context file in the resources folder of test which was getting copied in the target folders by maven. And the actual context file was not getting copied in target.Because of empty context file the context was getting initialized but since it did not had bean definition it was throwing NoSuchBeanException.Now i should highlight that ContextConfiguration was not throwing any error when it was not able to find the context file specified in the location attribute. Which i thought it ideally should.I tried and put some dummy file paths which does not exists in the location attributes and did not threw error.
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.