SpringBoot Mockito:when..thenReturn 给出异常
SpringBoot Mockito: when..thenReturn giving an exception
所以,目前,我正在测试服务 class
这是我的ConvertService.java
@Service
public class ConvertService {
private final NetworkClient networkClient; //NetworkClient is a Service too
private final ConvertUtility convertUtility;
public ConvertService(Network networkClient) {
convertUtility = ConvertFactory.of("dev", "F");
this.networkClient = networkClient
}
public Response convert(Request request) {
User user = networkClient.getData(request.getId()); //User is POJO class
Context context = convertUtility.transform(request.getToken()) //getToken returns a String
//Context is a normal Java
}
}
这是我的ConvertServiceTest.java
@SpringBootTest
@RunWith(MockitoJunitRunner.class)
class ConvertServiceTest {
@MockBean
private NetworkClient networkClient;
@Mock
ConvertUtility convertUtility;
private ConvertService convertService;
@BeforeEach
void setUp() {
convertService = new ConvertService(networkClient);
}
private mockMethod() {
Request request = Request(1000);
User user = new User("user1");
Context context = new Context();
when(networkClient.getData(anyLong())).thenReturn(user);
when(convertUtility.transform(any(String.class)).thenReturn(context);
Response response = convertService.convert(request); //it throws me an exception here
}
}
convertService.convert(request);
抛出异常
指向内部convertUtility.transform(request.getToken())
我不确定为什么它会处理来自 transform
方法的所有内容,当我写
when(convertUtility.transform(any(String.class)).thenReturn(context);
有人可以帮忙吗?
编辑:ConvertUtility
是只读库
在您的 public 构造函数中,您使用静态工厂方法获取 ConvertUtility
的实例。您必须模拟静态 ConvertUtility.of()
方法才能在测试期间使用模拟。
虽然 Mockito is able to mock static methods,但我建议重构(如果可能)您的 class 设计并接受 ConvertUtility
的实例作为 public 构造函数的一部分:
@Service
public class ConvertService {
private final NetworkClient networkClient; //NetworkClient is a Service too
private final ConvertUtility convertUtility;
public ConvertService(Network networkClient, ConvertUtility convertUtility) {
this.convertUtility = convertUtility
this.networkClient = networkClient
}
}
通过此更改,您可以在编写单元测试时轻松模拟 ConvertService
的协作者:
@ExtendWith(MockitoExtension.class)
class ConvertServiceTest {
@Mock
private NetworkClient networkClient;
@Mock
private ConvertUtility convertUtility;
@InjectMocks
private ConvertService convertService;
@Test // make sure it's from org.junit.jupiter.api
void yourTest() {
}
}
所以,目前,我正在测试服务 class
这是我的ConvertService.java
@Service
public class ConvertService {
private final NetworkClient networkClient; //NetworkClient is a Service too
private final ConvertUtility convertUtility;
public ConvertService(Network networkClient) {
convertUtility = ConvertFactory.of("dev", "F");
this.networkClient = networkClient
}
public Response convert(Request request) {
User user = networkClient.getData(request.getId()); //User is POJO class
Context context = convertUtility.transform(request.getToken()) //getToken returns a String
//Context is a normal Java
}
}
这是我的ConvertServiceTest.java
@SpringBootTest
@RunWith(MockitoJunitRunner.class)
class ConvertServiceTest {
@MockBean
private NetworkClient networkClient;
@Mock
ConvertUtility convertUtility;
private ConvertService convertService;
@BeforeEach
void setUp() {
convertService = new ConvertService(networkClient);
}
private mockMethod() {
Request request = Request(1000);
User user = new User("user1");
Context context = new Context();
when(networkClient.getData(anyLong())).thenReturn(user);
when(convertUtility.transform(any(String.class)).thenReturn(context);
Response response = convertService.convert(request); //it throws me an exception here
}
}
convertService.convert(request);
抛出异常
指向内部convertUtility.transform(request.getToken())
我不确定为什么它会处理来自 transform
方法的所有内容,当我写
when(convertUtility.transform(any(String.class)).thenReturn(context);
有人可以帮忙吗?
编辑:ConvertUtility
是只读库
在您的 public 构造函数中,您使用静态工厂方法获取 ConvertUtility
的实例。您必须模拟静态 ConvertUtility.of()
方法才能在测试期间使用模拟。
虽然 Mockito is able to mock static methods,但我建议重构(如果可能)您的 class 设计并接受 ConvertUtility
的实例作为 public 构造函数的一部分:
@Service
public class ConvertService {
private final NetworkClient networkClient; //NetworkClient is a Service too
private final ConvertUtility convertUtility;
public ConvertService(Network networkClient, ConvertUtility convertUtility) {
this.convertUtility = convertUtility
this.networkClient = networkClient
}
}
通过此更改,您可以在编写单元测试时轻松模拟 ConvertService
的协作者:
@ExtendWith(MockitoExtension.class)
class ConvertServiceTest {
@Mock
private NetworkClient networkClient;
@Mock
private ConvertUtility convertUtility;
@InjectMocks
private ConvertService convertService;
@Test // make sure it's from org.junit.jupiter.api
void yourTest() {
}
}