I am trying to run my first test with junit on a Spring Web Flow Project from within Eclipse and also from the console with mvn test and but give me the same error.
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [WEB-INF/spring/root-config.xml]; nested exception is java.io.FileNotFoundException: class path resource [WEB-INF/spring/root-config.xml] cannot be opened because it does not exist
I checked and I do have this file in the location so I dont know why Eclipse and Maven is not finding it. Can someone please help me out... below is my test classs
package org.uftwf.memberinquiry.text;
import junit.framework.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.uftwf.memberinquiry.model.MemberInquiryInformation;
import org.uftwf.memberinquiry.model.MemberRequest;
import org.uftwf.memberinquiry.service.MemberInquiryService;
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = "classpath:/WEB-INF/spring/root-config.xml")
public class TestApp {
#Autowired
private MemberInquiryService service;
#Test
public void testgetMemeberRequestInformation() {
MemberRequest inMemberRequest = new MemberRequest();
MemberInquiryInformation testInfo = service.getMemeberRequestInformation(inMemberRequest);
inMemberRequest.setRequestor("cpilling04#aol.com.dev");
Assert.assertEquals(testInfo.getFirst_Name(), "Christine");
Assert.assertEquals(testInfo.getLast_Name(), "Pillings");
}
}
#ContextConfiguration(locations = {classpath:spring/root-config.xml})
or create
TestApp-context.xml
I updated to the up to date spring-test and junit and now everything is find
#ContextConfiguration(locations = "classpath:WEB-INF/spring/root-config.xml")
When you start the path by /, it is understood, as an absolute path.
Related
I´m trying to configurate new properties for the test, so I created a test config class:
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.scheduling.annotation.EnableAsync;
#Configuration
#EnableAsync
#ComponentScan("ar.com.yo")
#PropertySource("test.properties")
public class TestConfig {
}
Properties file is in src/test/resources/test.properties
and in the test class :
#RunWith(SpringRunner.class)
#SpringBootTest
#ContextConfiguration(classes = TestConfig.class)
public class InsumoServiceTest {
...
}
when I execute the tests the error is:
Failed to parse configuration class [ar.com.yo.myproject.main.TestConfig]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/test.properties]
It seems that the requested properties can't be found. I would recommend doing this:
is that resolve your problem :
#PropertySource("classpath:test.properties")
I have a new springboot application I am attempting to get started.
The error I receive is
org.springframework.context.ApplicationContextException: Unable to start reactive web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ReactiveWebApplicationContext due to missing ReactiveWebServerFactory bean.
at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.onRefresh(ReactiveWebServerApplicationContext.java:76) ~[spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
src/main/java/bubbleshadow/RootController.java
package bubbleshadow;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
#RestController
public class RootController {
public RootController() {
}
#GetMapping("/")
public Mono<HttpStatus> returnOk() {
return Mono.just(HttpStatus.OK);
}
}
src/test/java/test/bubbleshadow/RootControllerTest.java
package test.bubbleshadow;
import bubbleshadow.RootController;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
// import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
#ExtendWith(SpringExtension.class)
#SpringBootTest(classes=RootController.class, webEnvironment = WebEnvironment.RANDOM_PORT)
#AutoConfigureWebTestClient
public class RootControllerTest {
#Autowired
WebTestClient webTestClient;
#Test
public void baseRouteShouldReturnStatusOK() {
webTestClient.head().uri("/").exchange().expectStatus().isOk();
}
}
Your configuration is not sufficient for reactive tests.
The reactive WebTestClient as well as ReactiveWebApplicationContext need reactive server in the application context. Add annotation #EnableAutoConfiguration to your RootControllerTest and let Spring's do it for you.
The autoconfiguration searches your class path and after find reactive classes and reactive context then create ReactiveWebServerFactory bean.
I assume you are using maven to get your dependencies.
I solved the problem by using:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<version>2.0.3.RELEASE</version>
</dependency>
Instead of:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webflux</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
For me, the error was being caused by a missing #SpringBootApplication annotation on the Spring class containing the main() method entry point which actually starts the Boot application. Using the following resolved the error:
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Likely a corrupt download. Try removing ~/.m2/repository.
You actually just need to change webEnvironment = WebEnvironment.RANDOM_PORT to webEnvironment = WebEnvironment.MOCK in your #SpringBootTest annotation.
#vdou's answer helped me to resolve my issue.
In addition to adding #EnableAutoConfiguration, I also had to manually add the spring application type:
spring:
main:
web-application-type: reactive
There is obviously something in my dependencies that is causing Spring not to be able to discover the type.
I hope this helps somebody...
If you are using Kotlin, check if in your Application class that contains the main method, doesnt have this:
runApplication<Application>{
webApplicationType = WebApplicationType.REACTIVE
}
Then change the "REACTIVE" to "SERVELET", will work like a charm.
If None of the above solutions work, try adding
#ContextConfiguration(loader = AnnotationConfigContextLoader.class)
It may help you
import org.springframework.test.context.support.AnnotationConfigContextLoader;
Yet another reason this can occur is if you're importing in a configuration class for your test that is not marked with #TestConfiguration annotation
I have the following test code:
package soundSystem;
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
#RunWith(SpringJUnit4ClassRunner.class )
#ContextConfiguration(classes = CDPlayerConfig.class)
public class SonyCDPlayerTest {
#Autowired
private ICompactDisk cd;
#Test
public void cdShouldNotBeNull() {
assertNotNull(cd);
}
}
This is a maven project, the problem is the exact same code would run in eclipse, but not in intellij.
I just can't find a way to resolve #RunWith
The #RunWith annotation has been replaced with #ExtendWith in JUnit 5.0 and above (which the latest spring version is now using).
Example:
#ExtendWith(SpringExtension.class)
#ContextConfiguration(classes = { SpringTestConfiguration.class })
public class GreetingsSpringTest {
// ...
}
Quoted from Baeldung:
Note that SpringExtension.class is provided by Spring 5 and integrates
the Spring TestContext Framework into JUnit 5.
Ref: https://www.baeldung.com/junit-5-runwith
Simple: your IDE is not configured to for unit testing.
In other words: you are missing all the JUnit related classes. You can see that all those JUnit imports are underlined; as IntelliJ simply doesn't know about the JARs that contain the corresponding classes.
See here on how to fix that.
We are using Spring Framework (XML Version) 4.0.5.RELAESE in our Java project.
In our application the context.xml is instantiated at the begin of the application start, and provides every properties via dependecy injection.
Now I am wondering what is the best (and commonly used) strategy on where to instantiate it in the test environment. (We have some Unit, Integration and System tests who at least need the databaseBaseConnector bean provided in the context.xml,)
I thought about making an abstract class which every test extends from, but in this case it would be newly instantiated before every test. I would like to a have a solution similiar to the main application, where the context is only instantiated one time, and everything else needed is set via dependency injection.
Researching this topic didn`t help much yet, so I decided to ask the question.
Spring comes with an SpringJUnit4ClassRunner and a #ContextConfiguration -annotation. If you use them, then spring will reuse the same Spring Context in different tests.
So a Spring test class can look like this:
package com.queomedia;
import javax.annotation.Resource;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
#RunWith(SpringJUnit4ClassRunner.class)
//#Transactional
#ContextConfiguration(SpringTestContext.APPLICATION)
public class SpringContextTest {
#Autowire
private ApplicationContext applicationContext;
//Test that the spring context can been loaded
#Test
public void testSpringContextLoad() {
Assert.assertNotNull("application context expected", this.applicationContext);
}
}
I am trying to run my first junit class in eclipse. When I right click on the new class and select run as junit it gives me a failed to load applicationcontext error. The direct path to the spring-servlet.xml is correct.
Here's the code:
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations={"file:C:/MyProject/WEB-INF/spring-servlet.xml"
})
public class MyUnitTest {
#Autowired
private ApplicationContext applicationContext;
#Before
public void setUp() {
}
#Test
public void testFunctionality() throws Exception {
assertTrue(true);
}
}
error -
Caused by: org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'aop:scoped-proxy'.
The direct path to the
spring-servlet.xml is correct.
No, it's not. If it was, JUnit would have found it.
I think it should look like this:
#ContextConfiguration(locations={"file:///C:/MyProject/WEB-INF/spring-servlet.xml"
http://en.wikipedia.org/wiki/File_URI_scheme
UPDATE:
The error message you posted says you did read the context XML, but there's an error.
I find that it's helpful to paste any error message I get into Google. When I did that with yours, it sent me this:
http://forum.springsource.org/showthread.php?35417-The-matching-wildcard-is-strict-but-no-declaration-can-be-foun...-aop-scoped-proxy
Check for missing JARs.