小编bra*_*and的帖子

通过XML在TestNG中运行套件之前的方法

我有一个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)

xml testng

5
推荐指数
1
解决办法
1095
查看次数

使用mockito和junit测试图像

我有一个方法获取 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().

是否可以使用模拟图像代替真实图像?

java junit mockito

5
推荐指数
1
解决办法
6901
查看次数

使用 junit 测试两个连续的 okhttp 调用

我有一个公共方法:

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)

java junit mockito

3
推荐指数
1
解决办法
1232
查看次数

标签 统计

java ×2

junit ×2

mockito ×2

testng ×1

xml ×1