Spring MockMvc ExceptionInInitializerError

时间:2016-04-13 07:46:51

标签: java spring unit-testing spring-mvc testing

我正在尝试使用 Spring Test Framework 测试我的 Spring MVC控制器。当我尝试构建独立的MockMvc 时,Java会抛出 java.lang.ExceptionInInitializerError

这是我的源代码

public class HelloSpringControllerTest {

    private List<UserInfo> expectedUsers;

    @Mock
    private Users users;

    @InjectMocks
    private HelloSpringController controller;

    private MockMvc mockMvc;


    @Before
    public void setUp() {
        initMocks(this);
        mockMvc = standaloneSetup(controller)
                .setSingleView(new InternalResourceView("/WEB-INF/jsp/askname.jsp"))
                .build();

        expectedUsers = getExpectedUsers();
        when(users.getUsers(Long.MAX_VALUE, 20)).thenReturn(expectedUsers);
    }

    @Test
    public void testAskName() throws Exception {
        mockMvc.perform(get("/"))
                .andExpect(view().name("askname"));
    }

    private List<UserInfo> getExpectedUsers() {
        List<UserInfo> users = new ArrayList<>();
        for (int i=0; i<20; i++) {
            users.add(new UserInfo("test", "test", i));
        }
        return users;
    }
 }

这是堆栈跟踪

-------------------------------------------------------------------------------
Test set: com.tuoppi.tehtava1.controller.HelloSpringControllerTest
-------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 1.037 sec <<< FAILURE!
testAskName(com.tuoppi.tehtava1.controller.HelloSpringControllerTest)  Time elapsed: 0.738 sec  <<< ERROR!
java.lang.ExceptionInInitializerError
    at org.springframework.test.web.servlet.MockMvcBuilderSupport.createMockMvc(MockMvcBuilderSupport.java:49)
    at org.springframework.test.web.servlet.setup.AbstractMockMvcBuilder.build(AbstractMockMvcBuilder.java:146)
    at com.tuoppi.tehtava1.controller.HelloSpringControllerTest.setUp(HelloSpringControllerTest.java:43)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
    at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)
    at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)
Caused by: java.util.MissingResourceException: Can't find bundle for base name javax.servlet.LocalStrings, locale fi_FI
    at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1564)
    at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1387)
    at java.util.ResourceBundle.getBundle(ResourceBundle.java:773)
    at javax.servlet.GenericServlet.<clinit>(GenericServlet.java:95)
    ... 33 more

这是我的pom.xml依赖项

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.2.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>4.2.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.10</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>7.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>2.0.2-beta</version>
    </dependency>
</dependencies>

这是控制器

@Controller
@RequestMapping(value = "/")
public class HelloSpringController {

private final Users users;

@Autowired
public HelloSpringController(Users users) {
    this.users = users;
}

public HelloSpringController() {
    this.users = null;
}

@RequestMapping(method = RequestMethod.GET)
public String askname() {
    return "askname";
}

@RequestMapping(method = RequestMethod.POST)
public String greet(
        @ModelAttribute("userinfo") @Valid UserInfo userInfo, Errors errors, 
        Model model) {
    if (!errors.hasErrors()) {
        model.addAttribute("userinfo", userInfo);
        return "greet";
    }
    else {
        return "redirect:/";
    }
}
}

我错过了什么?

0 个答案:

没有答案