Search code examples
javamavenjunitmockingjmockit

Why are my tests running on the same branch?


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 = "src/test/resources/AudioTestFile.mp3";
private ServiceUpload serviceUpload = new ServiceUpload();

@Test
@DisplayName("TestConversionOfMp4ToMp3")
void testConversionOfMp4ToMp3() {

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

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

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

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

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

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

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

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

    new MockUp<ServiceUpload>() {
        @Mock
        public String convertToMp3(String mp4File) {
            return "src/test/java/resources/fakeMp4.txt";
        }
    };

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

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

    new MockUp<ServiceUpload>() {
        @Mock
        public String convertToMp3(String mp4File) throws Exception {
            throw new Exception("Forced Exception");
        }
    };

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

@Test
@DisplayName("Test convertToMp3 catch block")
void testConvertToMp3CatchBlock() {

    new MockUp<ServiceUpload>() {
        @Mock
        public String createMp3(String mp4file) throws Exception {
            throw new Exception("Forced Exception");
        }
    };

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

NOTE:

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

    <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>

and changed it to

    <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>

Solution

  • 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.