为什么我的测试 运行 在同一个分支上?
Why are my tests running on the same branch?
当我 运行 进行我的测试时 class 后面的测试使用前面的模拟。我在 Maven 中使用 JMockit。我读到它们可能 运行ning 在同一个 jvm 分支上?如果是这种情况,有人可以解释我如何 运行 他们在不同的分支上吗?如果不是,那么谁能解释为什么会重复使用模拟(从而破坏测试)。
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));
}
}
注意:
事实证明这是我在 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>
<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>
你发生了一些微妙的事情,在你拔头发之前我会检查你的假设。首先,确认 MockUp 在测试之间确实存在泄漏(不应该)。一个简单的方法是在每个 MockUp 中添加一个 System.out.println(也许在 setup/teardown 中),然后当它运行每个测试时,您应该会看到不期望的 printlns。如果你不这样做,那么 JMockIt 的行为就像人们所期望的那样。
假设你的理论是正确的,我会看看 pom.xml 文件。具体来说,万无一失的设置(如果你发布它会很好)。我猜你对 'branches' 的评论确实在 forking/threading/test-parallelization 中得到了解决,surefire 确实如此。您可能在那里遇到了一些小问题,要正确调整它可能很棘手。
我认为您错过了测试顶部的注释 class,请参阅此 。
当我 运行 进行我的测试时 class 后面的测试使用前面的模拟。我在 Maven 中使用 JMockit。我读到它们可能 运行ning 在同一个 jvm 分支上?如果是这种情况,有人可以解释我如何 运行 他们在不同的分支上吗?如果不是,那么谁能解释为什么会重复使用模拟(从而破坏测试)。
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));
}
}
注意:
事实证明这是我在 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>
<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>
你发生了一些微妙的事情,在你拔头发之前我会检查你的假设。首先,确认 MockUp 在测试之间确实存在泄漏(不应该)。一个简单的方法是在每个 MockUp 中添加一个 System.out.println(也许在 setup/teardown 中),然后当它运行每个测试时,您应该会看到不期望的 printlns。如果你不这样做,那么 JMockIt 的行为就像人们所期望的那样。
假设你的理论是正确的,我会看看 pom.xml 文件。具体来说,万无一失的设置(如果你发布它会很好)。我猜你对 'branches' 的评论确实在 forking/threading/test-parallelization 中得到了解决,surefire 确实如此。您可能在那里遇到了一些小问题,要正确调整它可能很棘手。
我认为您错过了测试顶部的注释 class,请参阅此