这个类是我测试层次结构的顶层:
@TestPropertySource("/test.properties")
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public abstract class ApplicationAbstractTest {
}
还有几个测试类:
@WebAppConfiguration
@ActiveProfiles("mysql")
abstract public class AbstractControllerTest extends ApplicationAbstractTest {
protected MockMvc mockMvc;
@Autowired
private WebApplicationContext webApplicationContext;
@PostConstruct
private void postConstruct() {
mockMvc = MockMvcBuilders
.webAppContextSetup(webApplicationContext)
.apply(springSecurity())
.build();
}
}
JsonUserServiceTest:
@ActiveProfiles("json")
public class JsonUserServiceTest extends ApplicationAbstractTest {
@Before
public void setUp() throws Exception {
...
}
}
ContactControllerTest:
public class ContactControllerTest extends AbstractControllerTest {
@Test
public void testGet() throws Exception {
mockMvc.perform(get("/update-" + ID + "-contact")
.with(userAuth(USER)))
// .andExpect(status().isOk())
.andDo(print())
.andExpect(view().name("details"))
.andExpect(forwardedUrl("/WEB-INF/jsp/details.jsp"));
}
}
因此,当我运行
ContactControllerTest
时,它是成功的,并且print
方法向我显示:Handler:
Type = com.telecom.web.ContactController
Method = public java.lang.String com.myApp.web.ContactController.details(java.lang.Integer,org.springframework.ui.ModelMap)
但是当我运行所有测试时,
JsonUserServiceTest
先运行,ContactControllerTest
失败了。并且 print
显示:Handler:
Type = null
...
java.lang.AssertionError: No ModelAndView found
配置出了什么问题?或者如何进行故障排除?
更新: 同时,像这样的测试总是正常工作:
public class UserControllerTest extends AbstractControllerTest {
@Test
public void testRegister() throws Exception {
mockMvc.perform(get("/register"))
.andDo(print())
.andExpect(view().name("profile"))
.andExpect(forwardedUrl("/WEB-INF/jsp/profile.jsp"));
}
}
更新: 我正在测试的是控制器方法:
@GetMapping("/update-{id}-contact")
public String details(@PathVariable Integer id, ModelMap model) {
Integer userId = AuthorizedUser.id();
LOG.info("get contact {} for User {}", id, userId);
Contact contact = service.get(id, userId);
model.addAttribute("contact", contact);
return "details";
}
我也有这样的bean:
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/jsp/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
更新:我已经尝试在单独的类中配置mockMvc:
@Configuration
public class TestConfig {
@Autowired
private WebApplicationContext webApplicationContext;
@Bean
public MockMvc mockMvc() {
return MockMvcBuilders
.webAppContextSetup(webApplicationContext)
.apply(springSecurity())
.build();
}
}
并将其添加到此处:
@WebAppConfiguration
@ContextConfiguration(classes = {TestConfig.class})
@ActiveProfiles("mysql")
abstract public class AbstractControllerTest extends ApplicationAbstractTest {
但是我收到了以下信息:
java.lang.IllegalStateException: springSecurityFilterChain 不能为空。请确保存在名称为springSecurityFilterChain的Bean,且实现了Filter接口或注入要使用的Filter。
@ActiveProfiles
接受一个数组。如果你给它@ActiveProfiles({ "json", "mysql" })
,会发生什么? - Roddy of the Frozen PeasNoUniqueBeanDefinitionException
异常,因为我有两个存储库实现,分别标记有不同的配置文件:@Profile("json")
和@Profile("mysql")
。 - Pavlo Plynko