0

I am following this tutorial about integration test in Spring Boot: http://www.baeldung.com/spring-boot-testing

Well, but when I try to Inject MockMvc I always get:

Error creating bean with name 'mockMvcBuilder' defined in class path resource [org/springframework/boot/test/autoconfigure/web/servlet/MockMvcAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder]: Factory method 'mockMvcBuilder' threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/web/servlet/DispatcherServlet

My test class is:

@RunWith(SpringRunner.class)
@SpringBootTest(
        webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
        classes = MyApplication.class)
@AutoConfigureMockMvc
public class MyControllerIntegrationTest {

    @Inject
    private MockMvc mockMvc;

    //Some code here

Well, I can see that it can't find the DispatcherServlet, but I'm not sure why is this happening... The test seams to be correct.

Does anyone knows what I am missing?

Edit:

This is the relevant part of my POM:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RC1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jersey</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.2</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>
Leandro Borges Ferreira
  • 10,852
  • 8
  • 48
  • 69

1 Answers1

1

MockMvc is, as its name suggests, a mocked-up stack of Spring's Web MVC infrastructure (with mocked requests, responses, and so on). Since you're using Jersey, you won't be able to use MockMvc for testing. With Boot, you may want to use @SpringBootTest to launch the container on a random port and use TestRestTemplate to make (real, local) HTTP requests for testing.

chrylis -cautiouslyoptimistic-
  • 67,584
  • 19
  • 106
  • 140