错误在运行JUnitWeb测试时发生在setup方法上。

huangapple go评论60阅读模式
英文:

Error running JUnitWeb Test on Method setup

问题

我有一个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: 无效的测试类'com.pastis.UserControllerTests':
  1. 方法setup()应该是public的
  2. 方法setup不应该有参数
英文:

I have a Spring application,
and I've created this test:

@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();
	}
..
}

but when I start the test I got this error:

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

答案1

得分: 2

这里是一个示例:

@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);
    }
}
英文:

Here an example:

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);

    }
}

答案2

得分: 0

所以我解释一下异常的输出:

  1. 将修饰符public添加到你的方法setup,否则JUnit无法调用它。
  2. 从方法中移除参数,@Before、@After等不允许带参数。

如何正确设置MockMvc是另一个问题。关于最近的Spring和与web范围初始化和行为相关的额外注释超出了本回答的范围。(这也需要更多的澄清,例如使用哪个JDK,哪个Spring或Spring Boot版本... XML配置,dbunit和JUnit 4建议使用传统上下文。)

英文:

So I explain the output of the exception:

  1. Add the modifier public to your method setup, otherwise JUnit can't invoke it
  2. Remove the parameter from the method, @Before, @After and such don't allow parameters.

How to setup MockMvc correctly is another question. Recent Spring and additional annotations regarding web scope initialization and -behavior leave the scope of this answer. (This also requires more clarification, for example which JDK, which Spring or Spring Boot… XML configuration, dbunit and JUnit 4 suggest a legacy context.)

huangapple
  • 本文由 发表于 2020年10月1日 01:49:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/64143120.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定