I want to write a test case to check my controller (getPersons). This is a server side code. I have confusion what should i put inside #ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/app-contest.xml"})
Secondly, I'm getting some errors like this:
Failed to load application context. Can not find the path [which I specified in #ContextConfiguration]
I have a structure like this:
restAPI
*src/main/java
com.company.controller
personController.java
*Test
com.company.testController
personControllerTest.java
*src
main
webapp
WEBINF
app-context.xml
#Autowired
private PersonService personService;
#RequestMapping(value="/t2/{yy_id}/person", method=RequestMethod.GET)
#ResponseBody
public PersonInfo[] getPersons() {
return personService.getPersons();
}
This is my Test
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations={"classpath:WEB-INF/app-context.xml"})
#WebAppConfiguration
public class PersonControllerTest {
#Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
#Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
#Autowired
private PersonService personService;
#Test
public void getPersons() throws Exception {
this.mockMvc.perform(get("/t2/1/person")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}
Trace
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:157)
at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:103)
at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:73)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:313)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:284)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [WEB-INF/application-context.xml]; nested exception is java.io.FileNotFoundException: class path resource [WEB-INF/application-context.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:243)
at org.springframework.test.context.web.GenericXmlWebContextLoader.loadBeanDefinitions(GenericXmlWebContextLoader.java:38)
at org.springframework.test.context.web.AbstractGenericWebContextLoader.loadContext(AbstractGenericWebContextLoader.java:113)
at org.springframework.test.context.web.AbstractGenericWebContextLoader.loadContext(AbstractGenericWebContextLoader.java:59)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:100)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:248)
at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:124)
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:148)
... 24 more
Caused by: java.io.FileNotFoundException: class path resource [WEB-INF/app-context.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:157)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
Can some one please help me out to figure it out what's wrong here?
As mentioned in the discussion: WEB-INF is not really part of the class path. If you use a common template, such as maven, use src/main/resources or src/test/resources to place the app-context.xml in. Then you can use classpath:.
Place your config file in src/main/resources/app-context.xml and use the following code:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = "classpath:app-context.xml")
public class PersonControllerTest {
...
}
You can also create your test context with different configurations of beans.
Place your config file into src/test/resources/test-app-context.xml and use:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = "classpath:test-app-context.xml")
public class PersonControllerTest {
...
}
There can be multiple root causes for this exception. For me, my mockMvc wasn't getting auto-configured. I solved this exception by using #WebMvcTest(MyController.class) at the class level. This annotation will disable full auto-configuration and instead apply only configuration relevant to MVC tests.
An alternative to this is, If you are looking to load your full application configuration and use MockMVC, you should consider #SpringBootTest combined with #AutoConfigureMockMvc rather than #WebMvcTest
If you are using Maven, add the below config in your pom.xml:
<build>
<testResources>
<testResource>
<directory>src/main/webapp</directory>
</testResource>
</testResources>
</build>
With this config, you will be able to access xml files in WEB-INF folder.
From Maven POM Reference:
The testResources element block contains testResource elements. Their definitions are similar to resource elements, but are naturally used during test phases.
In case you landed here based on the title, desperate for a solution and happen to be using Junit 5 Jupiter, you need to use
#ExtendWith(SpringExtension.class)
instead of
#RunWith(SpringJUnit4ClassRunner.class)
Try this with Junit5:
#SpringBootTest(classes = {ServletWebServerFactoryAutoConfiguration.class},
webEnvironment = RANDOM_PORT,
properties = {"spring.cloud.config.enabled=false"})
#ExtendWith(MockitoExtension.class)
#AutoConfigureMockMvc
Using same version of spring boot and spring cloud dependency works for me.
Sovled by editing the annotation
#SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
Solved by adding the following dependency into pom.xml file :
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
Related
I'm working on a Spring Boot Application - and adding #RestController beans after the RequestMapping has already been initialized.
I can see the new Controller beans are in the web context, but since they were added after, there is no mapping when the application finishes starting.
I was hoping I could call "afterPropertiesSet()" on the RequestMappingHandlerMapping, but I end up with ambiguous mappings [I assume calling the method re-registers all beans with mappings]
Is there a way to register certain beans, or refresh the RequestMappingHandlerMapping?
Caused by: java.lang.IllegalStateException: Ambiguous handler methods mapped for HTTP path 'http://localhost:52765/error': {public org.springframework.http.ResponseEntity org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest), public org.springframework.http.ResponseEntity org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)}
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lookupHandlerMethod(AbstractHandlerMethodMapping.java:367) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:314) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:61) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.servlet.handler.AbstractHandlerMapping.getHandler(AbstractHandlerMapping.java:352) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.getHandler(DispatcherServlet.java:1160) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:940) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
... 26 common frames omitted
Asking another way -
I have a #RestController in a separate project that is not referenced by the configuration project.
I have a third project that references both the configuration project and the 'module' project with the #RestController.
How do I ensure the #RestController in the module project is added to the application context before the RequestMappingHandlerMapping is configured?
I created a custom annotation, thinking maybe I could use it somehow.. See the projects here -
Main platform - https://github.com/savantly-net/sprout-platform/tree/development/
Autoconfiguration project - https://github.com/savantly-net/sprout-platform/tree/development/spring/sprout-spring-boot-starter
Module Project - https://github.com/savantly-net/sprout-platform/tree/development/modules/sprout-wiki-module
Full Web Project - https://github.com/savantly-net/garden/tree/development
If you build this project, you will see the wiki module beans are added to the application context, but the #RestController is never mapped.
Any thoughts?
I changed the module declaration in the web app from a #Bean to an #Import and it resolved the issue. I'm assuming this bumped up the order of the beans in the module project.
Before -
#SpringBootApplication
public class GardenWebApplication {
public static void main(String[] args) {
SpringApplication.run(GardenWebApplication.class, args);
}
#Bean
public WikiModule wikiModule() {
return new WikiModule();
}
}
After -
#SpringBootApplication
#Import(WikiModule.class)
public class GardenWebApplication {
public static void main(String[] args) {
SpringApplication.run(GardenWebApplication.class, args);
}
}
I have a maven project which i'm using MapStruct to generate mappers to help in the job of translating entities into DTOs and vice-versa.
This mappers are generated during the generate-sources phase of maven, and stored into target/generated-sources and target/AppName/WEB-INF/classes folders.
For example, I have this Mapper
#Mapper
public interface RuleMapper {
RuleDto ruletoDto(Rule rule);
//other cool stuf
}
I configurated MapStruct to use CDI, so it will generate the following:
#Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2016-12-19T23:19:36-0200",
comments = "version: 1.1.0.CR1, compiler: javac, environment: Java 1.8.0_112"
)
#Singleton
#Named
public class RuleMapperImpl implements RuleMapper {
#Override
public RuleDto ruletoDto(Rule rule) {
ruleDto ruleDto = new ruleDto();
if ( rule != null ) {
ruleDto.setIdRule( rule.getIdRule() );
}
return ruleDto;
}
}
It works perfectely when running on Wildfly server, the problem is that I'm trying to junit test this class, for this, I implemented a custom runner as shown bellow:
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.InitializationError;
public class WeldJUnit4Runner extends BlockJUnit4ClassRunner {
public WeldJUnit4Runner(Class<Object> clazz) throws InitializationError {
super(clazz);
}
#Override
protected Object createTest() throws Exception {
final Class<?> test = getTestClass().getJavaClass();
return WeldContext.INSTANCE.getBean(test);
}
}
And:
import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;
public class WeldContext {
public static final WeldContext INSTANCE = new WeldContext();
private final Weld weld;
private final WeldContainer container;
private WeldContext() {
this.weld = new Weld();
this.container = weld.initialize();
Runtime.getRuntime().addShutdownHook(new Thread() {
#Override
public void run() {
weld.shutdown();
}
});
}
public <T> T getBean(Class<T> type) {
return container.instance().select(type).get();
}
}
These implementations were taken from here.
Finally, the test:
#RunWith(WeldJUnit4Runner.class)
public class RuleMapperTest {
#Inject
private RuleMapper ruleMapper;
#Test
public void coolTestName() {
Assert.assertTrue(Boolean.TRUE);
}
}
When I try to run, this is the console output:
log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
warning about logs, and the following exception:
java.lang.ExceptionInInitializerError
at br.com.treinoos.common.cdi.WeldJUnit4Runner.createTest(WeldJUnit4Runner.java:15)
at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type RuleMapper with qualifiers #Default
at injection point [BackedAnnotatedField] #Inject private br.com.treinoos.model.core.business.treinoos.mappers.RuleMapperTest.ruleMapper
at br.com.treinoos.model.core.business.treinoos.mappers.RuleMapperTest.ruleMapper(RuleMapperTest.java:0)
at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProblems(Validator.java:359)
at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:281)
at org.jboss.weld.bootstrap.Validator.validateGeneralBean(Validator.java:134)
at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:155)
at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:518)
at org.jboss.weld.bootstrap.ConcurrentValidator$1.doWork(ConcurrentValidator.java:68)
at org.jboss.weld.bootstrap.ConcurrentValidator$1.doWork(ConcurrentValidator.java:66)
at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:63)
at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:56)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Like Weld wasn't abble to lookup the generated class.
The beans.xml is already created under src/test/resources/META-INF/beans.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
version="1.1" bean-discovery-mode="all">
</beans>
Can anybody point me a solution to this problem? I've already searched something simillar, but no success.
Here's a full explanation to your problem and why what I wrote fixes it.
In Maven, you have at least 2 classloader. Your test classpath and your main classpath each have their own classloader. You can have others depending on your dependency structure. CDI identifies each classloader as a separate bean archive when running this way. src/main/webapp is explicitly for your WAR file. The beans.xml there does not give you a bean archive. Adding one to src/main/resources does. This problem is specific to how you're instantiating weld.
There are other projects that do this correctly - CDI-unit and Arquillian, specifically the Weld Embedded container. If you were to use one of these, this would not be an issue.
I am having hard time with JerseyTest and Spring. Previously in non-Spring Java projects what I usually did for my REST APIs was to extend my Test from JerseyTest, mocking the Service classes and simply (unit)testing my REST API. Now I'm using spring in my project where in my REST resource classes the Services are annotated with #Autowired. Now that I'm using the same scenario. Spring jumps in and nags about stuff like lack of applicationcontext.xml. I do want to use spring in my production but for my unit test I don't need my test know anything about Spring and all its autowiring and classpath annotation processing! How can I get this right? The classes look like this:
public class RESTResource{
#Autowired
MyService service;
#GET
public Response getSomeStuff(){
...
service.getStuff()
}
}
And here is the Test class
public class RESTResourceTest extends JerseyTest{
private Service service;
#Override
public Application configure(){
RESTResource resource = new RESTResource();
service = Mockito.mock(Service.class);
resource.setService(service);
ResourceConfig config = new ResourceConfig();
config.register(resource);
return config;
}
}
This is the stacktrace:
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:344)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:252)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:538)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:452)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
at org.glassfish.jersey.server.spring.SpringComponentProvider.createXmlSpringConfiguration(SpringComponentProvider.java:173)
at org.glassfish.jersey.server.spring.SpringComponentProvider.createSpringContext(SpringComponentProvider.java:164)
at org.glassfish.jersey.server.spring.SpringComponentProvider.initialize(SpringComponentProvider.java:99)
at org.glassfish.jersey.server.ApplicationHandler$4.get(ApplicationHandler.java:408)
at org.glassfish.jersey.server.ApplicationHandler$4.get(ApplicationHandler.java:399)
at org.glassfish.jersey.internal.util.collection.Values$LazyValueImpl.get(Values.java:340)
at org.glassfish.jersey.server.ApplicationHandler$3.call(ApplicationHandler.java:350)
at org.glassfish.jersey.server.ApplicationHandler$3.call(ApplicationHandler.java:347)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.processWithException(Errors.java:255)
at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:347)
at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:299)
at org.glassfish.jersey.test.inmemory.InMemoryTestContainerFactory$InMemoryTestContainer.<init>(InMemoryTestContainerFactory.java:77)
at org.glassfish.jersey.test.inmemory.InMemoryTestContainerFactory$InMemoryTestContainer.<init>(InMemoryTestContainerFactory.java:63)
at org.glassfish.jersey.test.inmemory.InMemoryTestContainerFactory.create(InMemoryTestContainerFactory.java:111)
at org.glassfish.jersey.test.JerseyTest.createTestContainer(JerseyTest.java:277)
at org.glassfish.jersey.test.JerseyTest.setUp(JerseyTest.java:609)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:330)
... 56 more
P.S. I'm using spring boot.
What you can do is to simply exclude the jersey-spring3 using the the sure-fire plugin for test phase.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<classpathDependencyExcludes>
<classpathDependencyExclude>
org.glassfish.jersey.ext:jersey-spring3
</classpathDependencyExclude>
</classpathDependencyExcludes>
</configuration>
</plugin>
Here is some code snippets that might help in your case :
Option 1. Your REST resource class :
#Component
#Path("/api/helloworld")
public class RESTResource{
#Autowired
MyService service;
#GET
#Produces(MediaType.TEXT_PLAIN)
public String helloMessage() {
return "Hello World Jersey Way!";
}
}
Your test config class :
#Component
public class JerseyConfig extends ResourceConfig {
/**
* In constructor we can define Jersey Resources & Other Components
*/
public JerseyConfig() {
register(RESTResource.class);
}
}
3.Your test base class :
#RunWith(SpringJUnit4ClassRunner.class)
#SpringApplicationConfiguration(classes = Application.class)//NOTE : Application is a spring boot main class,or if you have specific configuration class similar to test config class above use #ContextConfiguration(classes = MyServiceSpringConfig.class)
public class RESTResourceTest extends JerseyTest {
#Override
protected Application configure() {
return new JerseyConfig();
}
#Test
public void contextLoads() {
}
#Test
public void someTest() throws Exception {
// Very useful test
}
}
Option 2 :
#Priority(value = 1)
public class MySpringWebInitializer implements WebApplicationInitializer
{
#Override
public void onStartup(ServletContext container)
{
//Tell jersey-spring3 the context is already initialized
container.setInitParameter("contextConfigLocation", "NOTNULL");
AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
appContext.register(RESTResource.class);
container.addListener(new ContextLoaderListener(appContext));
}
}
Also,for spring boot you can run test cases using an active profile or a config name ,pass runtime VM args with -Dspring.config.name=test for running test cases etc..
Hope this help and the rest is self explanatory.
The better way to integrate JerseyTest with Spring
No inheritance from JerseyTest in junit test class
No dependencies on Jersey in junit test class, just pure jax-rs dependency
perfectly integrate SpringJUnit4ClassRunner and #ContextConfiguration
My solution is hosted on github. Hope this help for you.
I'v setup a Spring/Hibernate backend based on following example using Spring 4.1 and Hibernate 4.3.6:
http://www.sivalabs.in/2011/02/springhibernate-application-with-zero.html
I basically got everything working so far, but there is one little configuration problem I can't overcome: If I try to save an Entity I get the following StackTrace:
org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
at org.springframework.orm.hibernate4.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1135)
at org.springframework.orm.hibernate4.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:620)
at org.springframework.orm.hibernate4.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:617)
at org.springframework.orm.hibernate4.HibernateTemplate.doExecute(HibernateTemplate.java:340)
at org.springframework.orm.hibernate4.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:308)
at org.springframework.orm.hibernate4.HibernateTemplate.save(HibernateTemplate.java:617)
at com.test.database.dao.UserSecurityQuestionDAOImpl.save(UserSecurityQuestionDAOImpl.java:40)
at com.test.database.service.UserSecurityQuestionService.save(UserSecurityQuestionService.java:20)
at com.test.database.UserSecurityQuestionTest.basicCreationAndRetrievalTest(UserSecurityQuestionTest.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:24)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
My transactional directive is placed in the Service Bean and looks like this:
#Service
#Transactional
public class UserSecurityQuestionService implements UserSecurityQuestionDAO{
#Autowired
private UserSecurityQuestionDAO userSecurityQuerstionDAO;
#Override
#Transactional(readOnly=false)
public void save(UserSecurityQuestion userSecurityQuestion) {
userSecurityQuerstionDAO.save(userSecurityQuestion);
}
This is my DAO Implementation of the save Method:
#Repository
public class UserSecurityQuestionDAOImpl implements UserSecurityQuestionDAO {
#Autowired
private HibernateTransactionManager transactionManager;
public void save(UserSecurityQuestion userSecurityQuestion) {
Session session = transactionManager.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
session.persist(userSecurityQuestion);
tx.commit();
session.close();
}
Selecting from the Database works fine, but Inserts/Updates are refused throwing above Error Message. What am I missing?
UPDATE
UserSecurityQuestionDAOImpl:
public class UserSecurityQuestionDAOImpl implements UserSecurityQuestionDAO{
#Autowired
private SessionFactory sessionFactory;
public void save(UserSecurityQuestion userSecurityQuestion) {
this.sessionFactory.getCurrentSession().save(userSecurityQuestion);
}
UserSecurityQuestionDAO / UserSecurityQuestionService - Interfaces:
public interface UserSecurityQuestionService {
public void save(UserSecurityQuestion userSecurityQuestion);
public UserSecurityQuestion findById(Short id);
public List<UserSecurityQuestion> findAll();
public void delete(UserSecurityQuestion userSecurityQuestion);
public void deleteById(Short... id);
}
Service Implementation:
#Service
public class UserSecurityQuestionServiceImpl implements UserSecurityQuestionService {
#Autowired
private UserSecurityQuestionDAO userSecurityQuestionDAO;
#Transactional
public void save(UserSecurityQuestion userSecurityQuestion) {
userSecurityQuestionDAO.save(userSecurityQuestion);
}
Unit test code Used for Testing:
public class UserSecurityQuestionTest {
private AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
private UserSecurityQuestionService usqService;
#Before
public void setUp(){
//Setup App Context
ctx.scan("com.test.database");
ctx.refresh();
//Initilize Service Bean
usqService = ctx.getBean("userSecurityQuestionService",UserSecurityQuestionServiceImpl.class);
}
New Problem: If I add #Autowired to the private UserSecurityQuestionService usqService; and omit the Setup-Part, I will get a Null pointer Exception. If I leave the code as it is, I get org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'userSecurityQuestionService' is defined
I can see that both you service class and your dao implements same interface, and that you autowire the dao in service by its class.
I assume your service is also autowired by its class in your controller. It would be better to have separate interfaces for service and dao to allow autowiring by type using an interface. That way, Spring will use a JDK proxy (the default) to implement the transactional proxy around your service bean.
The alternative would be to use <tx:annotation-driven proxy-target-class="true/>", but you then need to have CGLIB in your path.
I have a Spring MVC project(contains Controller & jsp files) and DAO project(Contains DB operation related files) & both the project are configured using annotations. MVC project depends on DAO project. Now, I am writing JUnit test for MVC and DAO projects. Hence I have created a separate 'Test project' in which I have added MVC project as dependency(Java Build Path -> Projects tab -> Add -> MVC Project & ok) and included all needed jar files (JUnit & spring-test.jar).
My project structure:
MVCProject (Depends on DAOProject --> Web App project)
|
WebContent
|
WEB-INF
|
spring-context.xml
DAOProject (Java project)
|
src
TestProject(Depends on MVCProject --> Java project)
|
src (packages)
|
JunitTestClass.java
Here is my test project code,
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = {"file:MVCProjectName/WebContent/WEB-INF/spring-context.xml"})
public class DAOImplTest {
#Autowired
private DAO dao;
String id = null;
/**
* Test method for {#link com.xyz.core.dao.DAOImpl#getById(java.lang.String)}.
*/
#Test
public void testGetById() {
id = "12345";
try {
dao.getById(id);
} catch (Exception e) {
e.printStackTrace();
}
}
}
I am getting 'File not found exception' as below.
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:308)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:220)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:301)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:303)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from URL [file:../../../../../../../RSPWebAPP/WebContent/WEB-INF/RoadSmartPro-servlet.xml]; nested exception is java.io.FileNotFoundException: MVCProjectName/WebContent/WEB-INF/spring-context.xml (The system cannot find the path specified.)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:212)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:81)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:1)
at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:280)
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:304)
... 24 more
Caused by: java.io.FileNotFoundException: MVCProjectName/WebContent/WEB-INF/spring-context.xml (The system cannot find the path specified.)
at java.io.FileInputStream.<init>(FileInputStream.java:149)
at java.io.FileInputStream.<init>(FileInputStream.java:108)
at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:103)
at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:201)
at org.springframework.core.io.UrlResource.getInputStream(UrlResource.java:124)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
... 33 more
But, When I copy the spring-context.xml file from 'MVC project' into 'Test project' everything works fine. I copied the context file to following location in the test project and its working fine.
TestProject
|_
config(packageName)
|_
test-spring-context.xml
and its corresponding annotation
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = {"classpath:config/test-spring-context.xml"})
public class DAOImplTest {
.....
.....
}
But I don't want this approach since any change in the original context file(spring-context.xml) will not reflect in the copied file(test-spring-context.xml).
Is there a way to configure original context file in 'Test project'? Kindly suggest something.
Thanks in Advance.
Why dont you use the Maven Inheritence model? This is a good usecase for that since, you would want your tests to go along with your code. Also, any changes to the dependencies of the project would be handled using the Maven dependency management for you. I am assuming the two modules MVC Project and the DAOProject are packaged together.
Reference :
http://maven.apache.org/guides/introduction/introduction-to-the-pom.html#Project_Inheritance