原因:java.lang.ClassNotFoundException:javax.servlet.ServletException

时间:2018-07-12 10:01:39

标签: spring-boot junit4 spring-boot-test

当我尝试运行控制器时,使用Spring-Boot-Test在Spring-boot版本1.5.3中进行测试,我得到的错误是由于:java.lang.ClassNotFoundException:javax.servlet.ServletException

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

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.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.test.context.junit4.SpringRunner;

import com.hanselnpetal.domain.CustomerContact;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
public class ContactsManagementControllerIntegrationTest {

    @Autowired
    ContactsManagementController contactsManagementController;

    @Test
    public void testAddContactHappyPath() {

        CustomerContact aContact = new CustomerContact();
        aContact.setFirstName("Jenny");
        aContact.setLastName("Johnson");

        // POST our CustomerContact form bean to the controller; check the outcome
        String outcome = contactsManagementController.processAddContactSubmit(aContact);

        // Assert THAT the outcome is as expected
        assertThat(outcome, is(equalTo("success")));
    }


}

我正在使用(eclipse)右键单击该文件来运行ContactsManagementControllerIntegrationTest.java类,并以junit身份运行。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

确保pom.xml(maven)中具有以下依赖项

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
</dependency>