简体   繁体   English

简单的WebMvcTest失败,Spring Boot缺少SpringBootConfiguration

[英]Simple WebMvcTest fails with Spring Boot Missing SpringBootConfiguration

Test 1 测试1

package test.java.rest.springframework.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootWebTest {

    @Test
    public void contextLoads() {
    }

   }

Test 2 测试2

package test.java.rest.springframework.test;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

public class TestWebApp extends SpringBootWebTest {

@Autowired
private WebApplicationContext webApplicationContext;

private MockMvc mockMvc;

@Before
public void setup() {
    mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}

@Test
public void testEmployee() throws Exception {
    mockMvc.perform(get("/")).andExpect(status().isOk())
            .andExpect(content().contentType("application/json;charset=UTF-8"));

}

}

Error 错误

java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
    at org.springframework.util.Assert.state(Assert.java:392)
    at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.getOrFindConfigurationClasses(SpringBootTestContextBootstrapper.java:173)
    at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.processMergedContextConfiguration(SpringBootTestContextBootstrapper.java:133)
    at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:409)
    at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildDefaultMergedContextConfiguration(AbstractTestContextBootstrapper.java:323)
    at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:277)
    at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildTestContext(AbstractTestContextBootstrapper.java:112)
    at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.buildTestContext(SpringBootTestContextBootstrapper.java:78)
    at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:120)
    at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:105)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:152)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:143)
    at org.springframework.test.context.junit4.SpringRunner.<init>(SpringRunner.java:49)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:84)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:70)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:43)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

I am trying to do a simple junit test for my mvc controller index page. 我正在尝试为我的mvc控制器索引页做一个简单的junit测试。 I followed Tutorial link But I am getting a missing springbootconfiguarion error? 我遵循了教程链接,但是我遇到了缺少的springbootconfiguarion错误? How can I create simple JUnit test with spring boot. 如何使用Spring Boot创建简单的JUnit测试。

使用@ContextConfiguration注释测试类,如下所示。

@ContextConfiguration(classes = {TestBeanConfig.class})

An empty annotation 空注解

@SpringBootTest

is not enough. 是不足够的。 You need to declare the configuration classes the test should use. 您需要声明测试应使用的配置类。 You can do it like this : 您可以这样做:

@SpringBootTest(classes = { MyConfig1.class,MyConfig2.class })

I am not sure, if annotating the test super class works, so you may try to annotate the test class 'TestWebApp' directly. 我不确定,如果注释测试超类有效,那么您可以尝试直接注释测试类“ TestWebApp”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:在为 Spring Controller 执行 @WebMvcTest 时无法找到 @SpringBootConfiguration - Error: Unable to find @SpringBootConfiguration when doing @WebMvcTest for Spring Controller 在Atlassian Bitbucket管道上使用@WebMvcTest进行Spring Boot测试失败 - Spring Boot Test with @WebMvcTest fails on Atlassian Bitbucket Pipeline 使用@SpringBootConfiguration的Spring Boot多模块应用程序 - Spring Boot Multi Module application with @SpringBootConfiguration Spring Boot WebMvcTest 需要 EntityManager - Spring Boot WebMvcTest require EntityManager Spring 引导@WebMvcTest 与@SpringBootTest - Spring Boot @WebMvcTest vs @SpringBootTest 使用 Spring 构建库 Boot: Unable to find @SpringBootConfiguration - Building a library using Spring Boot: Unable to find @SpringBootConfiguration Spring 启动 Maven 安装错误 - 找不到@SpringBootConfiguration - Spring boot Maven install error - Unable to find a @SpringBootConfiguration 春季启动集成测试-数据库未与@WebMvcTest自动关联 - spring boot integration test - database not autowired with @WebMvcTest Spring Boot 1.4.2 @WebMvcTest返回状态404 - Spring Boot 1.4.2 @WebMvcTest returns status 404 Spring 启动 WebMvcTest,这里是什么导致 IllegalArgumentException? - Spring Boot WebMvcTest, what is causing IllegalArgumentException here?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM