英文:
ResponseEntity is always null in Mockito while calling function from a service class
问题
我已经创建了一个服务类
```java
@Service
public class SomeServiceImpl implements SomeService {
@Override
public ResponseEntity<?> getMethod() {
EntityClass entity = new EntityClass();
//一些操作来操作实体
return new ResponseEntity<>("entity", HttpStatus.OK);
}
}
我正在尝试在测试类中使用Mockito获取对象:
@AutoConfigureMockMvc
@RunWith(MockitoJUnitRunner.class)
@Slf4j
public class SomeServiceTest {
@Mock
SomeService someService;
@Test
public void getMethodSomeService(){
ResponseEntity<?> responseEntity = someService.getMethod();
log.debug("{}", responseEntity);
}
}
然而,responseEntity
总是 null
。我做错了什么?我有什么遗漏吗?为什么我得到的值总是 null?
<details>
<summary>英文:</summary>
I have created a service class
```java
@Service
public class SomeServiceImpl implements SomeService {
@Override
public ResponseEntity<?> getMethod() {
EntityClass entity = new EntityClass();
//Some operations to manipulate entity
return new ResponseEntity<>("entity", HttpStatus.OK);
}
}
I'm trying to get the object in a test class using Mockito:
@AutoConfigureMockMvc
@RunWith(MockitoJUnitRunner.class)
@Slf4j
public class SomeServiceTest {
@Mock
SomeService someService;
@Test
public void getMethodSomeService(){
ResponseEntity<?> responseEntity=someService.getMethod();
log.debug("{}", responseEntity);
}
}
However, responseEntity
is always null
. What am I doing wrong? Am I missing something? Why the value I got is always null?
答案1
得分: 1
通过编写
@Mock
SomeService someService;
您已经创建了一个模拟对象,对该模拟对象的所有方法调用将返回 null,除非您使用类似下面这样的代码告诉它进行其他操作:
when(someService.someMethod(any())).thenReturn("something");
因此,someService.getMethod()
将始终返回 null。
在这种情况下,您可能不希望使用模拟对象,模拟对象通常用于防止调用其他不应成为单元测试一部分的对象。
英文:
By writing
@Mock
SomeService someService;
You have created a mock, all method calls to that mock will return null unless you tell it to do otherwise with something like this
when(someService.someMethod(any())).thenReturn("something");
Because of this someService.getMethod()
will always return null.
You are probably looking to not use a mock in this case, mocks are usually used to prevent calls to other objects that aren't supposed to be part of the unit test.
答案2
得分: 0
Mocking意味着用外部可操作的内容替换具体的实现。如果你想测试你的 SomeServiceImpl
,你可以创建一个新的实例。
SomeService service = new SomeServiceImpl();
然后可以对这个实例进行测试。
如果你的 SomeService
有一个依赖,比如另一个Service,你可能需要对这个依赖进行模拟以控制其行为。
@Mock
OtherService dependency;
SomeService someService = new SomeServiceImpl(dependency);
英文:
Mocking means replacing a concrete implementation with something you can manipulate from the outside. If you want to test your SomeServiceImpl
, you can just create a new instance.
SomeService service = new SomeServiceImpl();
This instance can then be tested.
If your SomeService
had a dependency to let's say another Service, you might need to mock this dependency in order to control its behaviour.
@Mock
OtherService dependency;
SomeService someService = new SomeServiceImpl(dependency);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论