此类在我的测试层次结构的顶部:
@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
配置有什么问题?或如何解决?
UPD:同时,像这样进行测试,始终可以正常工作:
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"));
}
}
UPD:我正在测试控制器的方法:
@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
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/jsp/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
UPD:我已经尝试在单独的类中配置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不能为null。确保存在名为springSecurityFilterChain的实现Filter的Bean,或注入要使用的Filter。
WARN消息不会导致测试用例失败。只是说实体管理器工厂已注册两次。仅当您使用相同的Entity Manager Factory集群应用程序时,这才是问题。对于测试用例,它不是引起关注的原因。
测试用例失败的根本原因在于这两行
.andExpect(view().name("details"))
.andExpect(forwardedUrl("/WEB-INF/jsp/details.jsp"));
请检查项目是否有一个名为“ details”的视图,转发的URL为“ /WEB-INF/jsp/details.jsp”
更新资料
你可以试试这个吗
@Configuration
public class TestConfig {
@Autowired
private Filter springSecurityFilterChain;
@Autowired
private WebApplicationContext webApplicationContext;
@Bean
public MockMvc mockMvc() {
return MockMvcBuilders
.webAppContextSetup(webApplicationContext)
.apply(springSecurityFilterChain)
.build();
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句