英文:
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 = "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>
</dependency>
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>
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论