Spring JUnit test getting NullPointerException with TestContext.retrieveContextLoaderClass(TestContext .java:197) - spring

Using Helios, spring 3.0.5 (TestContext Framework) and JUnit 4.7. I am getting an initialization error indicating that it cannot find the ContextConfiguration. I ran ProcMon in the background and determined it is not apparently looking at all. I have tried the logical locations for the xml file to no avail. I am unclear of what I am doing incorrectly. Here is the code:
package com.hwcs.veri.agg.dao;
import static org.junit.Assert.assertEquals;
import java.util.List;
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.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;
import com.hwcs.veri.jpa.License;
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = { "/JpaIntegrationTests-context.xml" })
#TransactionConfiguration( transactionManager = "transactionManager",
defaultRollback = true )
#Transactional
public class JpaIntegrationTests
extends AbstractTransactionalJUnit4SpringContextTests
{
#Autowired
protected LicenseDao licenseDao;
#Test
public void getLicenses()
{
List<License> licenses = this.licenseDao.getLicenses();
assertEquals( "Expecting 1 license from the query",
super.countRowsInTable( "product_schema.license" ),
licenses.size() );
}
}
Is there some particular step that needs to be done to run this as a JUnit test inside Eclipse?

First and foremost, set the log level for org.springframework.test.context to DEBUG. That should tell you everything that the Spring TestContext Framework (TCF) is doing.
Note that with your above configuration, the TCF will attempt to load your application context from classpath:/JpaIntegrationTests-context.xml (i.e., in the root of your classpath). So make sure that the JpaIntegrationTests-context.xml file in fact exists in the root of the classpath (e.g., /src/test/resources/JpaIntegrationTests-context.xml for a Maven project layout). For the Maven project layout, you need to make sure that /src/test/resources is configured as a source folder in your IDE.
If this doesn't help you solve your problem, post the DEBUG output from the log.
Regards,
Sam (author of the Spring TestContext Framework)

Quoting Java Project: Failed to load ApplicationContext
"From the Sping-Documentation: A plain path, for example "context.xml", will be treated as a classpath resource from the same package in which the test class is defined. A path starting with a slash is treated as a fully qualified classpath location, for example "/org/example/config.xml".

Related

org.springframework.boot.SpringApplication import throwing error

I am working on this tutorial using Spring Tool Suites.
I've copied what it says and my code and pom look the same (copied for easy review)
package com.javatpoint.spring_boot_example_sts;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class SpringBootExampleSts
{
public static void main(String[] args){
SpringApplication.run(SpringBootExampleSts.class, args);
}
}
My issue is that I am getting a red line under ' org.springframework.boot.SpringApplication. To my knowledge, this library is not deprecated and I can't seem to find any instance of it being moved.
Thanks in advance.
May be maven either maven dependency is not downloaded or your IDE has some issues.
You can try to restart your IDE.
If issue still persist, Please build using mvn clean package.

Cucumber Java - No features found at file <location>

I am getting the following error while running the maven command "mvn clean verify". I have placed the "feature" file at "C:/Users/304090/eclipse-workspace/evms-qa-testautomation/src/test/resources/". However, its unable to identify the file. Please suggest.
Apr 21, 2020 7:19:43 PM io.cucumber.core.runtime.FeaturePathFeatureSupplier get
WARNING: No features found at file:/C:/Users/304090/eclipse-workspace/evms-qa-testautomation/src/test/resources/
0 Scenarios
0 Steps
0m
0.000s
Folder structure:
check your test runner class where you have provided path to featch your feature files.currently its looking for C:/Users/304090/eclipse-workspace/evms-qa-testautomation/src/test/resources/ folder for a .feature files.
cucumber.api.CucumberOptions imports the #CucumberOptions annotation and it wwll tell where to look for feature files
You need to create features folder into the src/test/resources and past your .feature file in it. After that just update your runner class
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
#RunWith(Cucumber.class)
#CucumberOptions(
features = "src/test/resources/features/featurefileanme.feature",
glue={"path of glue package"}
)
public class TestRunner {
}
with serenity:
import cucumber.api.CucumberOptions;
import net.serenitybdd.cucumber.CucumberWithSerenity;
import org.junit.runner.RunWith;
#RunWith(CucumberWithSerenity.class)
#CucumberOptions(
features = "src/test/resources/CreatePreVioltReport.feature",
glue = "stepsDef"
)
public class RunTests {}

Spring Boot #RunWith(SpringRunner.class) results in InitializationError

I have had this problem for a few days, checked similar questions on this forum and Googled in various ways but could not find an answer.
This is a Spring Boot starter project: I have a class Graphs which is annotated by #Component in a package under "src/main/java":
#Component
public class Graphs {
}
Then, I created test classes under "src/test/java". One of them (to test Graphs):
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.example.demo.Graphs;
#RunWith(SpringRunner.class)
#SpringBootTest
public class GraphsTest {
#Test
public void testRun () {
Graphs graph = new Graphs();
}
}
Then I ran testRun in Eclipse (right-click on "testRun"--> Run As --> Junit Test). The result I got was:
initializationError [Runner: Junit 4] (0.0001s)
The Failure Trace in Eclipse shows the following:
java.lang.Exception: No tests found matching [{ExactMatcher:fDisplayName=testRun], {ExactMatcher:fDisplayName=testRun(com.example.demoTest.GraphsTest)], {LeadingIdentifierMatcher:fClassName=com.example.demoTest.GraphsTest,fLeadingIdentifier=testRun]] from org.junit.internal.requests.ClassRequest#1f7030a6
at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:40)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createFilteredTest(JUnit4TestLoader.java:83)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:74)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:49)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:525)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
I made a few tweaks without any luck:
I bought my Eclipse Oxygen up-to-date, also tried in Eclipse 2018-12 edition, the same result.
If I remove #RunWith annotation and run testRun, it produced expected results.
I added a Public static void main and ran it as "Java Application", it produced expect results. I guess running it as a Java app bypassed #RunWith(SpringRunner.class).
Where did I do wrong?

Specifying a spring bean xml file from unit test

I am learning Spring and am working on a unit test, with the beans being specified in an XML config file. I am wondering how I tell it to use a file on the file system.
The following code* will run:
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("beans.xml")
public class CDPlayerTest {
...
}
The beans.xml file is in the same package as the test class.
But when using
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration("/Users/pabernathy/pworkspace/springtraining/beans.xml")
public class CDPlayerTest {
...
}
It gives me this error message:
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [Users/pabernathy/pworkspace/springtraining/beans.xml]; nested exception is java.io.FileNotFoundException: class path resource [Users/pabernathy/pworkspace/springtraining/beans.xml] cannot be opened because it does not exist
I can assure you, the file does exist.
Does anyone know how to specify an arbitrary XML file for the Spring beans?
*based on the example in chapter 2 of Spring in Action
If you look at the error message closely, you'll see that spring stripped away the leading slash on the file path. By default, spring assumes the file exists in the class path somewhere. Try this:
#ContextConfiguration("file:/Users/pabernathy/pworkspace/springtraining/beans.xml")
file: overrides the default spring behavior and informs spring that it is an absolute path.
try annotating like this
#ContextConfiguration(locations={"file://beans.xml"}). if that wont work just specify the whole path as #ContextConfiguration(locations={"file://users/blah/blah/beans.xml"}) and you should be good.

org.springframework.boot.autoconfigure cannot be resolved in eclipse

I imported the spring-boot-1.1.10.RELEASE.jar into my j2ee project.
But i can not found the class of autoconfigure.
Here is code:
import org.springframework.boot.autoconfigure;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.ComponentScan;
#ComponentScan
#EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
The Compile error:
The import org.springframework.boot.autoconfigure cannot be resolved
I had the same problem and I tried above solutions. But I have found a different issue.
In latest springframework, this org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder has been moved to
org.springframework.boot.jdbc.DataSourceBuilder. so you have to edit in import
And then you will notice that you have to use DataSourceBuilder.create(classLoader)
instead of new DataSourceBuilder(classLoader)
Go to your maven repository directory
For windows on below path
C:\Users\YourUser\.m2\repository\org\springframework\boot
Then delete spring-boot-autoconfigure folder.
Now go to eclipse and do maven update.
There is a copy/paste error - the code from the official documentation (https://spring.io/guides/gs/rest-service/) is:
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.ComponentScan;
#ComponentScan
#EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
The code in the question is missing ".EnableAutoConfiguration" in the first line.
In case anyone else has this issue, I was able to solve this by deleting the spring-boot-autoconfigure artifacts from my local Maven repository and updating my project in Eclipse (which re-downloads the artifacts).
if you are using eclise then Right button on project -> Maven -> Update Project
If you are using IntelliJ IDEA Right click on project -> Maven -> Reimport
Also, try "Generate source and update folder" below the reimport if above method doesn't work. No need to delete any spring or maven folder or files.

Resources