Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.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 如何在producer for spring cloud contract上模拟controller中的服务_Java_Mockito_Junit5_Powermockito_Spring Cloud Contract - Fatal编程技术网

Java 如何在producer for spring cloud contract上模拟controller中的服务

Java 如何在producer for spring cloud contract上模拟controller中的服务,java,mockito,junit5,powermockito,spring-cloud-contract,Java,Mockito,Junit5,Powermockito,Spring Cloud Contract,我一直试图在制作人端的控制器内模拟服务 @Autowired private Service service; @PostMapping(path = "/getone", consumes = "application/json", produces = "application/json") public ResponseEntity<Response> getone(@RequestBody Test[] t

我一直试图在制作人端的控制器内模拟服务

@Autowired
private Service service;

    @PostMapping(path = "/getone", consumes = "application/json", produces = "application/json")
public ResponseEntity<Response> getone(@RequestBody Test[] test) {
    String appId = Utils.getId();
    return new ResponseEntity<>(service.pub(test,id), HttpStatus.OK);
}
  • 实际的服务类在此处运行,而不是导致问题的模拟类?
    2.如何在@Before方法中测试失败场景

  • 有一个重要的区别。对于您的测试,您需要
    @MockBean
    ,因为您正在为您的Spring测试上下文用mock替换springbean

    除此之外,您还混合了JUnit4(
    @RunWith
    )和JUnit5(
    @ExtendWith
    @beforeach

    如果您只想使用
    MockMvc
    为控制器编写一个测试,那么您不必使用
    @springbootest
    启动整个Spring上下文。Spring Boot Test仅为MVC组件提供注释:
    @WebMvcTest

    对于
    Utils
    类的静态模拟,您可以使用来减少额外依赖项的数量,并坚持Spring Boot Starter测试提供的内容

    可能的测试如下所示:

    // @ExtendWith(SpringExtension.class) not needed with recent Spring Boot versions
    @ActiveProfiles("test")
    @WebMvcTest
    public class ContractTest {
        
        @Autowired
        private MockMvc mockMvc;
    
        @MockBean
        private Service service;
    
        @Test
        void test() {
          
         try(MockedStatic<Utils> mockedUtils = Mockito.mockStatic(Utils.class)) {
            mockedUtils.when(Utils::getId).thenReturn("194");
    
            when(service.pub(Mockito.any(Test[].class),eq("194"))).thenReturn(TestUtils.publish_success_response());
            // ... your test
         }
          
        }
    }
    
    最近的Spring启动版本不需要
    /@ExtendWith(SpringExtension.class)
    @ActiveProfiles(“测试”)
    @WebMvcTest
    公共类契约测试{
    @自动连线
    私有MockMvc-MockMvc;
    @蚕豆
    私人服务;
    @试验
    无效测试(){
    try(MockedStatic mockedUtils=Mockito.mockStatic(Utils.class)){
    mockedUtils.when(Utils::getId).thenReturn(“194”);
    when(service.pub(Mockito.any(Test[].class),eq(“194”))。然后return(TestUtils.publish_success_response());
    //…你的测试
    }
    }
    }
    
    @RunWith
    是一个JUnit4注释,您确定没有被来自不同版本的类弄糊涂吗
    SpringExtension
    通常用于创建Spring上下文,因此默认情况下它将自动连线,而不是插入模拟。试试
    @MockBean
    也许吧?
    // @ExtendWith(SpringExtension.class) not needed with recent Spring Boot versions
    @ActiveProfiles("test")
    @WebMvcTest
    public class ContractTest {
        
        @Autowired
        private MockMvc mockMvc;
    
        @MockBean
        private Service service;
    
        @Test
        void test() {
          
         try(MockedStatic<Utils> mockedUtils = Mockito.mockStatic(Utils.class)) {
            mockedUtils.when(Utils::getId).thenReturn("194");
    
            when(service.pub(Mockito.any(Test[].class),eq("194"))).thenReturn(TestUtils.publish_success_response());
            // ... your test
         }
          
        }
    }