gpt4 book ai didi

java - 如何修复 "No ModelAndView found"失败的测试?

转载 作者:搜寻专家 更新时间:2023-10-31 19:52:58 25 4
gpt4 key购买 nike

这个类在我的测试层次结构的顶部:

@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 {
...
}
}

接触 Controller 测试:

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

更新:我正在测试 Controller 的方法:

@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;
}

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 cannot be null. Ensure a Bean with the name springSecurityFilterChain implementing Filter is present or inject the Filter to be used.

最佳答案

WARN 消息不会导致测试用例失败。它只是说实体管理器工厂被注册了两次。如果您使用相同的 Entity Manager Factory 集群您的应用程序,这只会是一个问题。对于测试用例运行,这不是一个值得关注的问题。

测试用例失败的根本原因就在这两行

 .andExpect(view().name("details"))
.andExpect(forwardedUrl("/WEB-INF/jsp/details.jsp"));

请检查项目是否有名为“details”的 View ,转发的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();
}
}

关于java - 如何修复 "No ModelAndView found"失败的测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43033898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com