gpt4 book ai didi

java - 可以用Fiddler观察MockMvc测试吗?

转载 作者:太空宇宙 更新时间:2023-11-04 13:04:31 24 4
gpt4 key购买 nike

我正在测试我的Web应用程序

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MyApp.class)
//@WebAppConfiguration
@WebIntegrationTest
public class MyTest {

private MockMvc mockMvc;

@Autowired
private WebApplicationContext webApplicationContext;

}

@Before
public void setup() throws Exception {
this.mockMvc = webAppContextSetup(webApplicationContext)
.apply(springSecurity())
.build();
}

@Test
public void someTest() throws Exception {
result = mockMvc.perform(get("/user/"));

result
.andExpect(status().isOk())
.andExpect(content().contentType(contentType))
...
}


有可能观察到如何使用Filler进行测试吗?还是没有网络发送?是否可以强制 MockMvc使用网络?

目前,我在Fiddler中什么都看不到。

设定

  System.getProperties().put("http.proxyHost", "127.0.0.1");
System.getProperties().put("http.proxyPort", "8888");


没有帮助。

最佳答案

简而言之,您不能使用HTTP代理,因为


  [...]您仅通过DispatcherServlet测试网络层
  但是使用实际的Spring配置,就像您可以测试
  数据访问层与上面的层隔离。
  
  [...]这样的测试在服务器端内部,因此您可以检查使用了哪个处理程序,如果使用HandlerExceptionResolver处理了异常,
  模型的内容是什么,存在什么绑定错误,等等。
  这意味着更容易编写期望,因为服务器不是
  通过实际的HTTP客户端对其进行测试时的黑匣子。


(请参见docs

如果您确实想使用HTTP代理检查响应,则需要使用RestTemplate(请参见docs):

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(MyApp.class)
@WebIntegrationTest("server.port:9000")
public class MyTest {

private static RestTemplate restTemplate = new TestRestTemplate();

@BeforeClass
public static void setup() {
// configure proxy host and port here
restTemplate = new TestRestTemplate();
}

@Test
public void test() {
restTemplate.get(URI.create("http://localhost:9000/your-app", Object.class);
}
}

关于java - 可以用Fiddler观察MockMvc测试吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34610918/

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