sububClass中的模拟rettemplate
这里有一个问题!
在我的应用程序中,我有一个消费者(kafka),它使用一个外墙,该立面使用我想要(拥有)测试RESTTEMPLATE的REST API的客户端
来测试整个流量,但不包括其余呼叫。因此,我已经尝试模拟rettemplate,但是如果没有成功-_-
在这里我的课程示例
@Service
public class KafkaConsumer{
@Autowired MyFacade facade;
@KafkaListener
public void onMessage(){
facade.awesomeMethod();
}
}
@Service
public class MyFacade{
@Autowired MyClient client;
public void awesomeMethod(){
client.callApi();
}
}
@Service
public class MyClient{
@Autowired RestTemplate restTemplate;
public void callApi(){
restTemplate.exchange(.....);
}
}
现在我正在构建一个模拟测试,但是出了问题,restTemplate调用真实服务器
在这里测试课程,
@ActiveProfile("test")
@RunWith(SpringRunner.class)
@SpringbootTest
@EmbeddedKafka
public class MyFailTest{
@Mock RestTemplate restTemplate;
@InjectMocks MyFacade facade;
@InjectMocks MyClient client;
@InjectMocks KafkaConsumer consumer;
@Autowired KafkaProducer producer;
@Test
public void test(){
Mokito.when(restTemplate.exchange(....)).thenReturn(new ResponseEntity<Void>(HttpStatus.OK);)
producer.send("My Kafka Message");
}
}
这里我的测试依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.11.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
eseciesjunit4和springboot 2.6.7
我认为我在@mock和@injecmock逻辑中失败了,我尝试了其中的一些组合。 )但是没有成功
there's a problem here!
In my application I have a Consumer (kafka) that uses a Facade that uses a Client that calls a rest Api with RestTemplate
I want (have) to test the whole flow excluded the rest call. so I've tried to mock RestTemplate but without success -_-
here an example of my classes
@Service
public class KafkaConsumer{
@Autowired MyFacade facade;
@KafkaListener
public void onMessage(){
facade.awesomeMethod();
}
}
@Service
public class MyFacade{
@Autowired MyClient client;
public void awesomeMethod(){
client.callApi();
}
}
@Service
public class MyClient{
@Autowired RestTemplate restTemplate;
public void callApi(){
restTemplate.exchange(.....);
}
}
now I'm tring to build a test with mock but something goes wrong and RestTemplate call real server
here test class
@ActiveProfile("test")
@RunWith(SpringRunner.class)
@SpringbootTest
@EmbeddedKafka
public class MyFailTest{
@Mock RestTemplate restTemplate;
@InjectMocks MyFacade facade;
@InjectMocks MyClient client;
@InjectMocks KafkaConsumer consumer;
@Autowired KafkaProducer producer;
@Test
public void test(){
Mokito.when(restTemplate.exchange(....)).thenReturn(new ResponseEntity<Void>(HttpStatus.OK);)
producer.send("My Kafka Message");
}
}
here my test dependecies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.11.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
Junit4 and Springboot 2.6.7
I think I'm failing in @Mock and @InjecMock logic, i've tried some combination of them ; ) but without success
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
多亏了@geobreze的评论,我将其深入到@mockbean。
这是我数千次尝试尝试的一种方法,但是我复制了一些代码,而我却没有真正理解我
使用@spybean的应对方法,因为@mockbean嘲笑所有方法类。要进行SEMPLIFY实现我嘲笑了客户端对象,请交换RESTTEMPLATE,这有点复杂,我不想遇到其他愚蠢的错误。
这不是有用的声明所有类链,所以我卸下了立面,并且我使用@Autowired注释,而不是@InjectMock
在这里我的测试
thanks to @geobreze comment I drilled down into @MockBean.
It was a way tried on my thousands tries but I copied some code without really understand what it was coping
At the end I've used @SpyBean because @MockBean mocks all methods class. To semplify implementation I've mocked Client object, exchenge of RestTemplate it is a little bit complex, I didn't want fall in other stupid error.
It is not useful declare all classes chain, so I've removed facade and I've used @autowired annotation rather than @InjectMock
here my test