为什么我的测试在同一个分支上运行?

huangapple go评论68阅读模式
英文:

Why are my tests running on the same branch?

问题

当我运行我的测试类时,后面的测试使用了前面的模拟对象。我在Maven中使用了JMockit。我读到说它们可能在同一个JVM分支上运行?如果是这种情况,有人能解释一下如何在不同的分支上运行它们吗?如果不是这种情况,那么有人能解释一下为什么会出现模拟对象的重用(从而导致测试失败)。

public class ServiceUploadTest {
    // ... (代码部分省略)
}

注意:

原来问题出在我的POM依赖上(感谢Jeff),我之前使用了:

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <version>RELEASE</version>
</dependency>

然后将它改成了:

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-params</artifactId>
    <version>5.3.1</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.3.1</version>
    <scope>test</scope>
</dependency>
英文:

When I'm running my test class the later tests are using the mocks of the previous ones. I use JMockit in maven. I've read that they might be running on the same jvm branch? If this is the case can someone explain how I run them on different branches? If its not, then can anyone explain why the re-use of mocks is occurring (and thus breaking tests).

public class ServiceUploadTest {
private String filePath = &quot;src/test/resources/AudioTestFile.mp3&quot;;
private ServiceUpload serviceUpload = new ServiceUpload();

@Test
@DisplayName(&quot;TestConversionOfMp4ToMp3&quot;)
void testConversionOfMp4ToMp3() {

    new MockUp&lt;Encoder&gt;() {
        @Mock
        public void encode(MultimediaObject multimediaObject, File target, EncodingAttributes attributes) throws IllegalArgumentException, InputFormatException, EncoderException {
        }
    };
    assertEquals(&quot;src/test/resources/Audio.mp3&quot;, serviceUpload.convertToMp3(filePath));
}

@Test
@DisplayName(&quot;Test cutting loop when length is over 5000000&quot;)
void testLongCuttingLoop() throws IOException {
    InputStream inputStream = new FileInputStream(&quot;/Users/hywelgriffiths/Documents/IntellijProjects/sipho/transcriptionSoftware/audio.transcribe.front/src/test/java/resources/base64.txt&quot;);
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
    String base64 = bufferedReader.readLine();
    ServiceUpload serviceUpload = new ServiceUpload();

    new MockUp&lt;ProviderUpload&gt;() {
        @Mock
        public String executeUploadHttp(String mp3Base64, String jobName, String tag, String email) {
            return null;
        }
    };

    assertNull(serviceUpload.cuttingLoop(base64, &quot;JOBNAME&quot;, null));
}

@Test
@DisplayName(&quot;Test cutting loop when length is under 5000000&quot;)
void testShortCuttingLoop() throws IOException {
    ServiceUpload serviceUpload = new ServiceUpload();

    new MockUp&lt;ProviderUpload&gt;() {
        @Mock
        public String executeUploadHttp(String mp3Base64, String jobName, String tag, String email) {
            return null;
        }
    };

    assertNull(serviceUpload.cuttingLoop(&quot;SHORTBASE64&quot;, &quot;JOBNAME&quot;, null));
}

@Test
@DisplayName(&quot;Test convertToBase64AndSend&quot;)
void testConvertToBase64AndSend(){
    ServiceUpload serviceUpload = new ServiceUpload();
    File file = new File (&quot;src/test/java/resources/fakeMp4.txt&quot;);
    String jobName = &quot;JOBNAME&quot;;

    new MockUp&lt;ServiceUpload&gt;() {
        @Mock
        public String convertToMp3(String mp4File) {
            return &quot;src/test/java/resources/fakeMp4.txt&quot;;
        }
    };

    assertNull(&quot;\&quot;complete\&quot;&quot;, serviceUpload.convertToBase64AndSend(jobName, file, null, false));
}

@Test
@DisplayName(&quot;Test convertToBase64andSendCatchBlock&quot;)
void testConvertToBase64AndSendCatch(){
    ServiceUpload serviceUpload = new ServiceUpload();
    File file = new File (&quot;src/test/java/resources/fakeMp4.txt&quot;);
    String jobName = &quot;JOBNAME&quot;;

    new MockUp&lt;ServiceUpload&gt;() {
        @Mock
        public String convertToMp3(String mp4File) throws Exception {
            throw new Exception(&quot;Forced Exception&quot;);
        }
    };

    assertEquals(&quot;\&quot;complete\&quot;&quot;, serviceUpload.convertToBase64AndSend(jobName, file, null, false));
}

@Test
@DisplayName(&quot;Test convertToMp3 catch block&quot;)
void testConvertToMp3CatchBlock() {

    new MockUp&lt;ServiceUpload&gt;() {
        @Mock
        public String createMp3(String mp4file) throws Exception {
            throw new Exception(&quot;Forced Exception&quot;);
        }
    };

    assertNull(serviceUpload.convertToMp3(filePath));
}
}

NOTE:

It turns out it was my dependencies in the POM (thanks Jeff) I was using :

    &lt;dependency&gt;
        &lt;groupId&gt;junit&lt;/groupId&gt;
        &lt;artifactId&gt;junit&lt;/artifactId&gt;
        &lt;version&gt;4.13&lt;/version&gt;
        &lt;scope&gt;test&lt;/scope&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
        &lt;artifactId&gt;junit-jupiter&lt;/artifactId&gt;
        &lt;version&gt;RELEASE&lt;/version&gt;

</dependency>

and changed it to

    &lt;dependency&gt;
        &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
        &lt;artifactId&gt;junit-jupiter-params&lt;/artifactId&gt;
        &lt;version&gt;5.3.1&lt;/version&gt;
        &lt;scope&gt;test&lt;/scope&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
        &lt;artifactId&gt;junit-jupiter-engine&lt;/artifactId&gt;
        &lt;version&gt;5.3.1&lt;/version&gt;
        &lt;scope&gt;test&lt;/scope&gt;
    &lt;/dependency&gt;

答案1

得分: 1

你这里有一些微妙的情况,我建议在你抓狂之前先检查一下你的假设。首先,确认一下 MockUp 在测试之间是否真的存在泄漏(其实不应该有泄漏)。一个简单的方法是在每个 MockUp 中添加 System.out.println(也可以在 setup/teardown 中添加),然后在运行每个测试时,你应该会看到一些意料之外的 println 输出。如果没有的话,那么 JMockIt 的行为是符合预期的。

如果你的理论是正确的,我建议检查一下 pom 文件。具体来说,是 surefire 的设置(如果你能把它的内容贴出来会很好)。我猜你提到的“branches”可能实际上是指 surefire 所做的分叉/线程/测试并行化。你可能在这方面遇到了一些问题,调整起来可能会有些棘手。

英文:

You've got something subtle going on, and I'd check your assumptions before you pull your hair out. First, confirm that the MockUp is truly leaking between tests (it shouldn't be). An easy way to do that would be to add a System.out.println in each MockUp (and maybe in setup/teardown), and then as it runs each test, you should see printlns that are not expected. If you don't, then JMockIt is behaving as one would expect.

Assuming your theory is sound, I'd take a look at the pom. Specifically, the surefire settings (it would be nice if you posted it). I'm guessing your comment on 'branches' is really addressed at the forking/threading/test-parallelization that surefire does. You may have something glitchy there and it can be tricky to get it tuned properly.

答案2

得分: 0

我认为你错过了测试类顶部的注解,可以查看这个提示

英文:

I think you missed the annotation the top of the test class, see this hint.

huangapple
  • 本文由 发表于 2020年6月29日 04:48:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/62628049.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定