“ResponseEntity在使用Mockito调用服务类中的函数时始终为null。”

huangapple go评论78阅读模式
英文:

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&lt;?&gt; getMethod() {
		EntityClass entity = new EntityClass();
		//Some operations to manipulate entity
		return new ResponseEntity&lt;&gt;(&quot;entity&quot;, 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&lt;?&gt; responseEntity=someService.getMethod();
		log.debug(&quot;{}&quot;, 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(&quot;something&quot;);

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);

huangapple
  • 本文由 发表于 2020年10月9日 15:42:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/64275889.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定