Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在方法安装程序上运行JUnitWeb测试时出错_Java_Spring_Junit_Log4j_Dbunit - Fatal编程技术网

Java 在方法安装程序上运行JUnitWeb测试时出错

Java 在方法安装程序上运行JUnitWeb测试时出错,java,spring,junit,log4j,dbunit,Java,Spring,Junit,Log4j,Dbunit,我有一个Spring应用程序, 我创建了这个测试: @RunWith(SpringRunner.class) @SpringJUnitWebConfig(locations = { "classpath:testDatabaseContext.xml", "classpath:testServicesContext.xml", "classpath:backoffice-servlet.xml&qu

我有一个Spring应用程序, 我创建了这个测试:

@RunWith(SpringRunner.class)
@SpringJUnitWebConfig(locations = {
        "classpath:testDatabaseContext.xml",
        "classpath:testServicesContext.xml",
        "classpath:backoffice-servlet.xml"
})
public class UserControllerTests {

    @Autowired
    private MockMvc mockMvc;

    @Before
    void setup(WebApplicationContext wac) {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
    }
..
}
但当我开始测试时,我得到了这个错误:

rg.junit.runners.model.InvalidTestClassError: Invalid test class 'com.pastis.UserControllerTests':
  1. Method setup() should be public
  2. Method setup should have no parameters

因此,我解释异常的输出:

  • 将修改器public添加到方法设置中,否则JUnit无法调用它
  • 从方法中删除参数@Before、@After等不允许使用的参数

  • 如何正确设置MockMvc是另一个问题。最近的Spring和关于web作用域初始化和行为的附加注释保留了这个答案的范围。(这还需要进一步澄清,例如哪个JDK、哪个Spring或Spring Boot…XML配置、dbunit和JUnit 4建议使用遗留上下文。)

    因此我解释了异常的输出:

  • 将修改器public添加到方法设置中,否则JUnit无法调用它
  • 从方法中删除参数@Before、@After等不允许使用的参数
  • 如何正确设置MockMvc是另一个问题。最近的Spring和关于web作用域初始化和行为的附加注释保留了这个答案的范围。(这还需要进一步澄清,例如,哪个JDK、哪个Spring或Spring Boot…XML配置、dbunit和JUnit 4建议使用传统上下文。)

    下面是一个示例:

    RunWith(SpringJUnit4ClassRunner.class)
    @WebAppConfiguration
    @ContextConfiguration(classes = MyWebConfig.class)
    public class CustomerControllerTest {
    
        @Autowired
        private WebApplicationContext wac;
    
        private MockMvc mockMvc;
    
        @Before
        public void setup () {
            DefaultMockMvcBuilder builder = MockMvcBuilders.webAppContextSetup(this.wac);
            this.mockMvc = builder.build();
        }
    
        @Test
        public void testUserController () throws Exception {
            ResultMatcher ok = MockMvcResultMatchers.status()
                                                    .isOk();
    
            MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/customers");
            this.mockMvc.perform(builder)
                        .andExpect(ok);
    
        }
    }
    
    这里有一个例子:

    RunWith(SpringJUnit4ClassRunner.class)
    @WebAppConfiguration
    @ContextConfiguration(classes = MyWebConfig.class)
    public class CustomerControllerTest {
    
        @Autowired
        private WebApplicationContext wac;
    
        private MockMvc mockMvc;
    
        @Before
        public void setup () {
            DefaultMockMvcBuilder builder = MockMvcBuilders.webAppContextSetup(this.wac);
            this.mockMvc = builder.build();
        }
    
        @Test
        public void testUserController () throws Exception {
            ResultMatcher ok = MockMvcResultMatchers.status()
                                                    .isOk();
    
            MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/customers");
            this.mockMvc.perform(builder)
                        .andExpect(ok);
    
        }
    }