0

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>
Hywel Griffiths
  • 287
  • 5
  • 16

2 Answers2

1

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.

Jeff Bennett
  • 996
  • 7
  • 18
  • Thanks Jeff. I set up break points in the mocks so that when I ran a test I could see it bouncing between the mocks it shouldn't have been going to! I was thinking something to do with the pom so I'll take a second look there – Hywel Griffiths Jul 01 '20 at 11:17
  • breakpoints and mock-frameworks sometimes do not play well together (due to the use of proxy code for the mocks). It usually works (95+%), but when it doesn't... well, I want those hours of my life back :) Anyway, it may seem inelegant, but trust me, when you get those edge-case WTF issues with mocks, throw a good'ol-always-trustworthy system.out.println, and confirm what you think you are seeing. – Jeff Bennett Jul 03 '20 at 02:59
  • P.S. add your POM, and I'll take a look at it – Jeff Bennett Jul 03 '20 at 03:00
0

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

tibor17
  • 1,043
  • 6
  • 9
  • Does this apply to mockit as well as mockito as I use the former. – Hywel Griffiths Jul 01 '20 at 11:19
  • Not sure about JMockit. I am the user of Mockito and Powermock. Try to google the compatibility between JMockit the Mockito. – tibor17 Jul 01 '20 at 12:22
  • I use mockito in one of my tests somewhere but I'm currently am addict of mockit! – Hywel Griffiths Jul 01 '20 at 12:29
  • JMockit and Mockito are both mocking frameworks, but they are not compatible with each other. The OP is using JMockit annotations. Adding a Mockito annotation does not seem like it will do good things. – Jeff Bennett Jul 03 '20 at 02:54