Spring MVC 找不到相依性的合格Bean类型[org.springframework.test.web.servlet.MockMvc]:

qni6mghb  于 2022-11-14  发布在  Spring
关注(0)|答案(1)|浏览(197)

I am trying to run test cases on my spring controllers. I get the following error.
No qualifying bean of type [org.springframework.test.web.servlet.MockMvc] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations
After reading several QA's on SO, I got my build.gradle right. But now Im stuck at this point. The other questions on SO related to this did not help identify an issue with the code below.

package test.controllers;

import static org.hamcrest.Matchers.equalTo;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

import com.google.gson.Gson;
import com.mnox.database.pojo.wrapper.v2.VehicleMasterPojoWrapper.VehiclePurpose;

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
@ContextConfiguration({
// "file:src/main/webapp/WEB-INF/web.xml"
"file:src/main/webapp/WEB-INF/spring/root-context.xml",
"file:src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml"
// ,
// "file:src/main/webapp/WEB-INF/spring/spring-security.xml"
})
public class TestSpringOnLocalhost {

    @Autowired
    public MockMvc mvc;

    @Test
    public void getHello() throws Exception {
        mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(content().string(equalTo("Greetings from Spring Boot!")));
    }

    @Test
    public void test1CreateClient() {
        SaveOrUpdateVehicleAjaxRequest vehicle = new SaveOrUpdateVehicleAjaxRequest("123", "KA-02-1234", 12,
                VehiclePurpose.MAIN_VEHICLE.name(), "some alias");
        MvcResult mvcResult = null;
        try {
            MockHttpServletRequestBuilder request = MockMvcRequestBuilders.post("/saveVehicleAjaxMethod")
                    .content(new Gson().toJson(vehicle).getBytes()).contentType(MediaType.APPLICATION_JSON)
                    .accept(MediaType.APPLICATION_JSON);
            mvcResult = mvc.perform(request).andReturn();
            mvcResult.getModelAndView();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    private class SaveOrUpdateVehicleAjaxRequest {
        private String vehicleId;
        private String registrationNumber;
        private int seatingCapacity;
        private String vehicleType;
        private String vehicleAlias;

        public SaveOrUpdateVehicleAjaxRequest(String vehicleId, String registrationNumber, int seatingCapacity,
                String vehicleType, String vehicleAlias) {
            super();
            this.vehicleId = vehicleId;
            this.registrationNumber = registrationNumber;
            this.seatingCapacity = seatingCapacity;
            this.vehicleType = vehicleType;
            this.vehicleAlias = vehicleAlias;
        }

        public String getVehicleId() {
            return vehicleId;
        }

        public void setVehicleId(String vehicleId) {
            this.vehicleId = vehicleId;
        }

        public String getRegistrationNumber() {
            return registrationNumber;
        }

        public void setRegistrationNumber(String registrationNumber) {
            this.registrationNumber = registrationNumber;
        }

        public int getSeatingCapacity() {
            return seatingCapacity;
        }

        public void setSeatingCapacity(int seatingCapacity) {
            this.seatingCapacity = seatingCapacity;
        }

        public String getVehicleType() {
            return vehicleType;
        }

        public void setVehicleType(String vehicleType) {
            this.vehicleType = vehicleType;
        }

        public String getVehicleAlias() {
            return vehicleAlias;
        }

        public void setVehicleAlias(String vehicleAlias) {
            this.vehicleAlias = vehicleAlias;
        }
    }

}

My Spring controller code snippet for your reference

@Controller
public class SaveOrUpdateVehicleController extends AVehicleModel implements IViewOperation {

    @RequestMapping(value = { "/saveVehicleAjaxMethod", "/modifyVehicleAjaxMethod" }, method = RequestMethod.POST)
    public @ResponseBody String saveOrModifyVehicleAjaxMethod(
            @RequestParam(value = "vehicleId", required = false) String vehicleId,
            @RequestParam(value = "registrationNumber") String registrationNumber,
            @RequestParam(value = "seatingCapacity") int seatingCapacity,
            @RequestParam(value = "vehicleType") String vehicleType,
            @RequestParam(value = "vehicleAlias") String vehicleAlias, HttpServletRequest servletRequest) {

My build.gradle

compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.0'
// https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api
compile group: 'javax.servlet', name: 'javax.servlet-api', version: '4.0.0-b07'
    // The production code uses the SLF4J logging API at compile time
    compile 'org.slf4j:slf4j-api:1.7.21'
    compile group: 'org.apache.tomcat', name: 'tomcat-catalina', version: '8.5.0'
    compile group: 'log4j', name: 'log4j', version: '1.2.16'
    // https://mvnrepository.com/artifact/commons-io/commons-io
compile group: 'commons-io', name: 'commons-io', version: '2.6'
// https://mvnrepository.com/artifact/commons-cli/commons-cli
compile group: 'commons-cli', name: 'commons-cli', version: '1.2'
// https://mvnrepository.com/artifact/jstl/jstl
compile group: 'jstl', name: 'jstl', version: '1.2'
// compile group: 'commons-io', name: 'commons-io', version: '2.5'
compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.4'
// https://mvnrepository.com/artifact/net.sf.json-lib/json-lib
compile group: 'net.sf.json-lib', name: 'json-lib', version: '2.4', classifier: 'jdk15'
// https://mvnrepository.com/artifact/com.sun.jersey/jersey-client
compile group: 'com.sun.jersey', name: 'jersey-client', version: '1.15'
// https://mvnrepository.com/artifact/com.sun.jersey/jersey-core
compile group: 'com.sun.jersey', name: 'jersey-core', version: '1.15'
// https://mvnrepository.com/artifact/javax.ws.rs/jsr311-api
compile group: 'javax.ws.rs', name: 'jsr311-api', version: '1.1.1'
// https://mvnrepository.com/artifact/net.java.dev.jna/jna
compile group: 'net.java.dev.jna', name: 'jna', version: '4.1.0'
// https://mvnrepository.com/artifact/net.java.dev.jna/jna-platform
compile group: 'net.java.dev.jna', name: 'jna-platform', version: '4.1.0'
// https://mvnrepository.com/artifact/org.json/json
compile group: 'org.json', name: 'json', version: '1.5-20090211'
// https://mvnrepository.com/artifact/com.sun.istack/istack-commons-tools
compile group: 'com.sun.istack', name: 'istack-commons-tools', version: '3.0.5'
// https://mvnrepository.com/artifact/com.google.code.gson/gson
compile group: 'com.google.code.gson', name: 'gson', version: '2.2.4'
// https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient
compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.4'
// https://mvnrepository.com/artifact/org.apache.httpcomponents/fluent-hc
compile group: 'org.apache.httpcomponents', name: 'fluent-hc', version: '4.4'
// https://mvnrepository.com/artifact/com.google.gcm/gcm-server
compile group: 'com.google.gcm', name: 'gcm-server', version: '1.0.0'
// https://mvnrepository.com/artifact/commons-codec/commons-codec
compile group: 'commons-codec', name: 'commons-codec', version: '1.9'
// https://mvnrepository.com/artifact/commons-logging/commons-logging
compile group: 'commons-logging', name: 'commons-logging', version: '1.2'
// https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple
compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1'
// https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore
compile group: 'org.apache.httpcomponents', name: 'httpcore', version: '4.4'
// https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient
compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.4'
// https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient-cache
compile group: 'org.apache.httpcomponents', name: 'httpclient-cache', version: '4.4'
// https://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime
compile group: 'org.apache.httpcomponents', name: 'httpmime', version: '4.4'
// https://mvnrepository.com/artifact/org.glassfish/javax.json
compile group: 'org.glassfish', name: 'javax.json', version: '1.0.2'
// https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-catalina
compile group: 'org.apache.tomcat', name: 'tomcat-catalina', version: '8.0.39'
// https://mvnrepository.com/artifact/org.springframework/spring-context
compile group: 'org.springframework', name: 'spring-context', version: '4.3.5.RELEASE'
// https://mvnrepository.com/artifact/org.springframework/spring-web
compile group: 'org.springframework', name: 'spring-web', version: '4.3.5.RELEASE'
// https://mvnrepository.com/artifact/org.springframework/spring-core
compile group: 'org.springframework', name: 'spring-core', version: '4.3.5.RELEASE'
// https://mvnrepository.com/artifact/org.springframework/spring-webmvc
compile group: 'org.springframework', name: 'spring-webmvc', version: '4.3.5.RELEASE'
compile name:'jsongpshelper'
// https://mvnrepository.com/artifact/org.springframework/spring-webmvc
compile group: 'org.springframework', name: 'spring-webmvc', version: '4.3.5.RELEASE'
// https://mvnrepository.com/artifact/springframework/spring-mock
compile group: 'springframework', name: 'spring-mock', version: '1.2.6'

// https://mvnrepository.com/artifact/org.springframework/spring-context-support
compile group: 'org.springframework', name: 'spring-context-support', version: '4.3.5.RELEASE'
// https://mvnrepository.com/artifact/org.mockito/mockito-core
testCompile group: 'org.mockito', name: 'mockito-core', version: '2.1.0'
// https://mvnrepository.com/artifact/org.springframework/spring-test
testCompile group: 'org.springframework', name: 'spring-test', version: '4.3.5.RELEASE'
// https://mvnrepository.com/artifact/junit/junit
//  testCompile group: 'junit', name: 'junit', version: '4.11'
//https://mvnrepository.com/artifact/org.springframework.security/spring-security-test
testCompile group: 'org.springframework.security', name: 'spring-security-test', version: '4.3.5.RELEASE'

// https://mvnrepository.com/artifact/org.springframework/spring-test-mvc
compile group: 'org.springframework', name: 'spring-test-mvc', version: '1.0.0.M2'

// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test
testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '2.0.3.RELEASE'
//testCompile("org.springframework.boot:spring-boot-starter-test")

Edit Tried one more variation based on https://www.luckyryan.com/2013/08/25/testing-spring-mvc-controllers/
Still no luck, stuck at the same point.

package test.controllers;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import com.google.gson.Gson;
import com.mnox.database.pojo.wrapper.v2.VehicleMasterPojoWrapper.VehiclePurpose;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration({"file:src/main/webapp/WEB-INF/spring/root-context.xml",
    "file:src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml"})
public class ControllerTess {

    @Autowired
    private WebApplicationContext wac;

    private MockMvc mockMvc;

    @Before
    public void setup() {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
    }
    @Test
    public void test1CreateClient() {
        SaveOrUpdateVehicleAjaxRequest vehicle = new SaveOrUpdateVehicleAjaxRequest("123", "KA-02-1234", 12,
                VehiclePurpose.MAIN_VEHICLE.name(), "some alias");
        MvcResult mvcResult = null;
        try {
            MockHttpServletRequestBuilder request = MockMvcRequestBuilders.post("/saveVehicleAjaxMethod")
                    .content(new Gson().toJson(vehicle).getBytes()).contentType(MediaType.APPLICATION_JSON)
                    .accept(MediaType.APPLICATION_JSON);
            mvcResult = mockMvc.perform(request).andReturn();
            mvcResult.getModelAndView();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    private class SaveOrUpdateVehicleAjaxRequest {
        private String vehicleId;
        private String registrationNumber;
        private int seatingCapacity;
        private String vehicleType;
        private String vehicleAlias;

        public SaveOrUpdateVehicleAjaxRequest(String vehicleId, String registrationNumber, int seatingCapacity,
                String vehicleType, String vehicleAlias) {
            super();
            this.vehicleId = vehicleId;
            this.registrationNumber = registrationNumber;
            this.seatingCapacity = seatingCapacity;
            this.vehicleType = vehicleType;
            this.vehicleAlias = vehicleAlias;
        }

        public String getVehicleId() {
            return vehicleId;
        }

        public void setVehicleId(String vehicleId) {
            this.vehicleId = vehicleId;
        }

        public String getRegistrationNumber() {
            return registrationNumber;
        }

        public void setRegistrationNumber(String registrationNumber) {
            this.registrationNumber = registrationNumber;
        }

        public int getSeatingCapacity() {
            return seatingCapacity;
        }

        public void setSeatingCapacity(int seatingCapacity) {
            this.seatingCapacity = seatingCapacity;
        }

        public String getVehicleType() {
            return vehicleType;
        }

        public void setVehicleType(String vehicleType) {
            this.vehicleType = vehicleType;
        }

        public String getVehicleAlias() {
            return vehicleAlias;
        }

        public void setVehicleAlias(String vehicleAlias) {
            this.vehicleAlias = vehicleAlias;
        }
    }
}

EDIT 1

Modified

FROM

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
@ContextConfiguration({
// "file:src/main/webapp/WEB-INF/web.xml"
"file:src/main/webapp/WEB-INF/spring/root-context.xml",
"file:src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml"
// ,
// "file:src/main/webapp/WEB-INF/spring/spring-security.xml"
})
public class TestSpringOnLocalhost {

TO

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.MOCK)
@AutoConfigureMockMvc
@ContextConfiguration({
// "file:src/main/webapp/WEB-INF/web.xml"
"file:src/main/webapp/WEB-INF/spring/root-context.xml",
"file:src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml"
// ,
// "file:src/main/webapp/WEB-INF/spring/spring-security.xml"
})
public class TestSpringOnLocalhost {

Exception

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'test.controllers.TestSpringOnLocalhost': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public org.springframework.test.web.servlet.MockMvc test.controllers.TestSpringOnLocalhost.mvc; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.test.web.servlet.MockMvc] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:384)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110)
    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: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:290)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    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.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:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    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:538)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: public org.springframework.test.web.servlet.MockMvc test.controllers.TestSpringOnLocalhost.mvc; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.test.web.servlet.MockMvc] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
    ... 26 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.test.web.servlet.MockMvc] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1103)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:963)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
    ... 28 more

EDIT 2, replace ContextConfiguration with ImportResource

java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:157)
    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: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:290)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    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.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:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    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:538)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
Caused by: java.lang.IllegalArgumentException: Can not load an ApplicationContext with a NULL 'contextLoader'. Consider annotating your test class with @ContextConfiguration.
    at org.springframework.util.Assert.notNull(Assert.java:112)
    at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:117)
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:148)
    ... 24 more
f0brbegy

f0brbegy1#

您需要彻底修改Gradle构建脚本。
我建议你使用https://start.spring.io重新创建你的build.gradle脚本。这将配置兼容的版本。例如,你不应该使用Spring Framework 4.3.5和Sping Boot 2.0.3等。
如果操作正确,您会发现您不需要为大多数依赖项指定版本。事实上,您不需要显式地配置大多数依赖项。Sping Boot 会为您完成这些操作。

相关问题