我有一个XML TestNG套件:
<suite name="mySuite" parallel="classes" thread-count="5">
<test name="myTest">
<packages>
<package name="mypack.*"/>
</packages>
</test>
</suite>
Run Code Online (Sandbox Code Playgroud)
我想在套房前每次都运行一个方法.
有可能有这样的事情:
<suite name="mySuite" parallel="classes" thread-count="5">
<before-suite>...</before-suite> <!-- Here I want to run a single method -->
<test name="myTest">
<packages>
<package name="mypack.*"/>
</packages>
</test>
</suite>
Run Code Online (Sandbox Code Playgroud)
?
我有一个方法获取 MultipartFile 对象作为参数。在我使用的方法中ImageIO.read(some_value)和ImageIO.write(some_value). 我想用模拟图像测试此方法(我不想将图像存储在资源文件夹下)。
我已经尝试过:
MockMultipartFile file = new MockMultipartFile("file", "boat.jpg", "image/jpeg", "content image".getBytes());但没有成功。
public void f(MultipartFile file) throws IOException {
final BufferedImage read = ImageIO.read(new ByteArrayInputStream(file.getBytes()));
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
ImageIO.write(read, "jpg", baos);
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行测试时,read变量具有null值。我认为这个问题来自"content image".getBytes().
是否可以使用模拟图像代替真实图像?
我有一个公共方法:
public class methodUnderTest() {
A();
B();
}
Run Code Online (Sandbox Code Playgroud)
和两个私有方法:
private class A() {
...
okHttpClient.newCall(request).execute();
...
}
private class B() {
...
okHttpClient.newCall(request).execute();
...
}
Run Code Online (Sandbox Code Playgroud)
我想测试该方法,methodUnderTest但我不知道如何模拟 okHttp 调用。我已经模拟了第一个调用(来自 A 类)及其响应,但会被来自 B 类的第二个调用覆盖(例如,来自 A 类的调用的响应将是来自 B 类的响应响应)。可以区分每个类的调用吗?
//inside the test method [with pesudocode]:
// for class A
ResponseA = response.body({"id":"1"});
when(call.execute()).thenReturn(response1);
when(okkHttpClient.newCall(any(Request.class))).thenReturn(call);
// for class B
ResponseB = response.body({"type"="car"});
when(call.execute()).thenReturn(responseB);
when(okkHttpClient.newCall(any(Request.class))).thenReturn(call);
Run Code Online (Sandbox Code Playgroud)