- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在测试我的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))
...
}
MockMvc
使用网络?
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/
我有一个想要测试的 REST 端点。通过 POST 请求命中此端点会上传远程 git 存储库中的文件。我正在尝试使用mockMvc测试对此端点的POST调用(我只想查看返回状态“isOk()”)——我
当我使用 WebApplicationContext 创建 MockMvc 以使用 Spring MVC 3.2.3 进行 restful-webservice 测试时,如下所示: @Autowire
我尝试测试注入(inject) UserPrincipal 的 mvcControllers 的访问: restPockMockMvc .perform(get("/pocs").principal(
我有一个带有 MySQL 数据库的简单 REST 应用程序,一切正常,但是在测试时我们是否需要创建一个虚拟对象并对其进行测试,或者通过模拟数据库进行测试?虚拟对象有相当大的构造函数和嵌套类,这是一个很
我有一个使用 MockMvc 的 springBoot 2.1.9.RELEASE 应用程序。 我想知道是否有办法从文件中获取正文内容 mockMvc.perform(post("/hostel")
我想测试包含请求正文的请求类型 post 的其余 Controller 方法。所有其他获取方法在发布后都可以正常工作。我期待 200 OK 但它返回 417。我的目的是让它通过我运行测试但失败了。我是
我正在尝试添加 Cucumber到我已经在使用 spring-test 和 JUnit 的 Spring Web MVC 项目。我已经编写的非 Cucumber 集成测试 Autowiring 了 W
我想向 Controller 发送一个由文件和简单类型组成的复杂对象。 public class ContributionNew { private List elementsToAdd;
我正在为我的 Spring 数据休息存储库编写测试用例: @RepositoryRestResource(collectionResourceRel = "teams", path = "teams"
当我模拟传递有效id时,测试运行成功,但是当我传递无效id时,返回返回404,这是预期的,但没有响应,当我正常的Http请求时,它返回响应正文,消息未找到,状态404。我在这里做错了什么? @RunW
当我使用以下 Controller /测试配置收到“请求的映射错误”时,目前正在努力解决问题。 Controller : @Slf4j @Validated @RestController @Requ
在我的测试中,我设置了 MockMvc @Before 中的对象像这样 mockMvc = MockMvcBuilders.webAppContextSetup(context)
有人有任何提示,或者有人知道如何测试 HTTP 响应对象返回的“错误消息”吗? @Autowired private WebApplicationContext ctx; private MockMv
我正在为 Spring Rest 服务编写测试,它将 url 重定向到另一个 Spring 服务。所以目标是使用书签名称找到书签。第一个服务使用 Bookmark 名称获取 BookmarkId,然后
我需要测试一个调用异步服务的 Controller 。 Controller 代码 @RequestMapping(value = "/path", method = RequestMethod.PO
我刚刚使用 MockMvc 为 Controller 创建了一个简单的集成测试。一切正常,但即使 Controller 方法返回某些内容也没有任何响应。这是 Controller : import d
我编写了 Controller ,它为每个映射使用不同的值。现在我将其折射为所有映射使用相同的值,但是我不知道如何进行测试,因为它对每个映射都返回 404。 这是我的 Controller : @Cr
我想在 Spring Boot 中为我的 Controller 创建一些测试。具体来说,我想创建一个测试来处理表单以添加新项目。该项目属于 Drug 类,并且有一个集合作为 参数: @ManyToMa
使用 Spring boot 2 和 Spring mvc。我正在尝试使用 mockMvc 测试我的休息 Controller @PostMapping( value = "/
大多数时候,我们不是在普通的 JUnit 断言中添加注释,而是向断言添加一条消息,以解释为什么这是断言: Person p1 = new Person("Bob"); Person p2 = new
我是一名优秀的程序员,十分优秀!