找不到依赖项的类型为[org.springframework.test.web.servlet.MockMvc]的合格Bean:

时间:2018-06-23 17:37:12

标签: spring-mvc spring-test-mvc springmockito

我正在尝试在我的spring控制器上运行测试用例。我收到以下错误。

  

未找到依赖项的类型为[org.springframework.test.web.servlet.MockMvc]的合格Bean:期望至少有1个符合此依赖项自动候选条件的bean。依赖注释

在阅读了关于SO的几个QA之后,我正确理解了build.gradle。但是现在我停留在这一点上。关于SO的其他与此相关的问题也无法帮助确定下面的代码有问题。

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;
        }
    }

}

我的Spring控制器代码段供您参考

@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) {

我的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")

编辑 根据{{​​3}}

尝试了另一种变体

仍然没有运气,停留在同一点。

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;
        }
    }
}

编辑1

已修改

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 {

@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 {

例外

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

编辑2,将ContextConfiguration替换为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

1 个答案:

答案 0 :(得分:0)

您需要完全重做Gradle构建脚本。

我建议您首先使用https://start.spring.io创建build.gradle脚本。这将配置兼容版本。例如,您不应将Spring Framework 4.3.5与Spring Boot 2.0.3等配合使用。

如果正确完成操作,您将发现不需要为大多数依赖项指定版本。实际上,您不需要显式配置大多数依赖项。 Spring Boot将为您做到这一点。