英文:
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
所以我解释一下异常的输出:
- 将修饰符public添加到你的方法setup,否则JUnit无法调用它。
- 从方法中移除参数,@Before、@After等不允许带参数。
如何正确设置MockMvc是另一个问题。关于最近的Spring和与web范围初始化和行为相关的额外注释超出了本回答的范围。(这也需要更多的澄清,例如使用哪个JDK,哪个Spring或Spring Boot版本... XML配置,dbunit和JUnit 4建议使用传统上下文。)
英文:
So I explain the output of the exception:
- Add the modifier public to your method setup, otherwise JUnit can't invoke it
- 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.)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论