Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring、Feign和TestNG的策略_Java_Spring_Testng_Netflix Feign_Spring Cloud Feign - Fatal编程技术网

Java Spring、Feign和TestNG的策略

Java Spring、Feign和TestNG的策略,java,spring,testng,netflix-feign,spring-cloud-feign,Java,Spring,Testng,Netflix Feign,Spring Cloud Feign,我目前有一个项目使用TestNG对我的Spring项目执行测试。在我的项目中,我有一组外部接口,用于处理Eureka配置上的外部调用。我很难理解如何在执行期间逐个测试地模拟/拦截这些调用 下面是我的一个外部接口的示例: @FeignClient ("http://my-service") public interface MyServiceFeign { @RequestMapping (value = "/endpoint/{key}", method = RequestMethod

我目前有一个项目使用TestNG对我的Spring项目执行测试。在我的项目中,我有一组外部接口,用于处理Eureka配置上的外部调用。我很难理解如何在执行期间逐个测试地模拟/拦截这些调用

下面是我的一个外部接口的示例:

@FeignClient ("http://my-service")
public interface MyServiceFeign {

    @RequestMapping (value = "/endpoint/{key}", method = RequestMethod.GET)
    SomePojo getByKey(@PathVariable ("key") String key);
}
我的服务依赖于客户:

@Service
public class MyService {

    @Autowired
    private MyServiceFeign theFeign;

    public SomePojo doStuff() {
      return theFeign.getByKey("SomeKey");
    }
}
我的测试仅通过以下方式启动:

@SpringBootTest (
    classes = Service.class,
    webEnvironment= SpringBootTest.WebEnvironment.RANDOM_PORT
)
@TestExecutionListeners (
    inheritListeners = false,
    listeners = {
        DependencyInjectionTestExecutionListener.class,
        TransactionalTestExecutionListener.class
    }
)
@DirtiesContext
@ContextConfiguration (initializers = CustomYamlLoader.class)
@ActiveProfiles ("test")
publi class MyModuleTest extends AbstractTestNGSpringContextTests {
    // ....
}
我想在测试中执行如下操作:

@Test
public void doSomeTest() {
   SomePojo fakeReturn = new SomePojo();
   fakeReturn.setSomeStuff("some stuff");

   /*
     !!! do something with the injected feign for this test !!!
     setupFeignReturn(feignIntercept, fakeReturn);
   */

   SomePojo somePojo = injectedService.doStuff();
   Assert.assertNotNull(somePojo, "How did I get NULL in a fake test?");
}
所以,我的困境是:我想,我缺少一个关键的理解,无法做到这一点。我完全不知道该如何处理这个问题。我不认为在这里使用回退实现是有意义的,但我可能错了


救命啊

据我所知,您正在处理外国客户机(可能是那些具有安全性的客户机,如basic auth或OAuth2),并且希望进行测试。但实际上,注意的不是测试
MyServiceFeign
是否工作,而是
MyService
是否工作正常,前提是外国客户机检索到有效的结果

为此,您实际上并没有注入您的假客户机,而是模拟它

简而言之,这可以通过两个步骤实现:使用
@MockBean
而不是
@Autowired
,并在使用它之前描述您的客户机行为

@RunWith(SpringRunner.class)
@SpringBootTest(classes = YourApp.class)
public class MyServiceUnitTest {

    @MockBean
    private MyServiceFeign myFeignClient;

    @Autowiered
    private MyService myService;

    @Test
    public void testSync() {
        given(myFeignClient.getByKey("SomeKey")).willReturn(
            new SomePojo("SomeKey")
        );
        assertEquals("SomeKey", myService.doStuff().getKey());
    }
}

如前所述,spring使用的是这种测试组件的方法。我描述了两种测试oauth2拦截的外国客户机的方法。

例如Mockito。听说过吗?我想我应该更清楚-让我更新一下描述。我不是一个悲观的选民。我投票赞成平衡。我想你在找嘲弄。我用服务设置了这个。读完这篇文章后,我没有去嘲笑:——我明白他们在说什么,我觉得我应该能够在没有嘲笑的情况下以某种方式拦截呼叫。