Однако при написании JUnits вызов происходит внутри служебного класса, даже если я выполняю статические насмехаться. Если я удалю реализацию Completable Future, JUnits будут нормально работать с макетом.
Есть ли что-то, что мне не хватает или нужно имитировать Completable Future по-другому?
Мой код:
Код CompletableFuture:
Код: Выделить всё
package com.demo.futures;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
public class CompletableFutureSample {
public List myShortTaskWithoutCompletableFuture() {
System.out.println("Inside myShortTaskWithoutCompletableFuture");
List res = new ArrayList();
res.add(MyUtils1.getSampleValue1("Ranesh"));
return res;
}
public List myShortTask() {
System.out.println("Inside myShortTask");
List futures = new ArrayList();
List res = new ArrayList();
CompletableFuture future1 = CompletableFuture
.supplyAsync(() -> MyUtils1.getSampleValue1("Ranesh"));
futures.add(future1);
try {
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
for (CompletableFuture future : futures) {
res.add(future.get());
}
} catch (InterruptedException | ExecutionException e) {
System.out.println(e.getMessage());
}
return res;
}
public List myTask() {
List futures = new ArrayList();
List res = new ArrayList();
CompletableFuture future1 = CompletableFuture
.supplyAsync(() -> MyUtils1.getSampleValue1("Ranesh"));
CompletableFuture future2 = CompletableFuture
.supplyAsync(() -> MyUtils2.getSampleValue2("Ranesh"));
CompletableFuture future3 = CompletableFuture
.supplyAsync(() -> MyUtils3.getSampleValue3("Ranesh"));
futures.add(future1);
futures.add(future2);
futures.add(future3);
try {
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
for (CompletableFuture future : futures) {
res.add(future.get());
}
} catch (InterruptedException | ExecutionException e) {
System.out.println(e.getMessage());
}
return res;
}
}
Код: Выделить всё
package com.demo.futures;
public class MyUtils1 {
public static String getSampleValue1(String str) {
System.out.println("Inside My Utils 1");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return "Hello : " + str;
}
}
Код: Выделить всё
package com.demo.futures;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.List;
import static org.mockito.ArgumentMatchers.anyString;
@ExtendWith(MockitoExtension.class)
class CompletableFutureSampleTest {
@InjectMocks
private CompletableFutureSample completableFutureSample;
private MockedStatic myUtils1MockedStatic;
@BeforeEach
void setup() {
completableFutureSample = new CompletableFutureSample();
myUtils1MockedStatic = Mockito.mockStatic(MyUtils1.class);
}
@Test
void testMyShortTaskWithoutCompletableFuture() {
myUtils1MockedStatic.when(() -> MyUtils1.getSampleValue1(anyString())).thenReturn("Test String Result");
List res = completableFutureSample.myShortTaskWithoutCompletableFuture();
Assertions.assertEquals(1, res.size());
Assertions.assertEquals("Test String Result", res.get(0));
}
@Test
void testMyShortTask() {
myUtils1MockedStatic.when(() -> MyUtils1.getSampleValue1(anyString())).thenReturn("Test String Result");
List res = completableFutureSample.myShortTask();
Assertions.assertEquals(1, res.size());
Assertions.assertEquals("Test String Result", res.get(0));
}
@Test
void testMyTask() {
}
@AfterEach
void cleanUp() {
myUtils1MockedStatic.close();
}
}
Подробнее здесь: https://stackoverflow.com/questions/791 ... upplyasync