0

我用 mockito 尝试了 junit,并为编码练习编写了一些测试用例。

这是我写的测试用例:

@RunWith(SpringRunner.class)
public class TransactionControllerTest {

    @Mock
    TransactionService transactionServiceMock;

    @InjectMocks
    TransactionController transactionController;

    TransactionRequest txn = new TransactionRequest("123.34", "2018-11-28T23:32:36.312Z");

    @Test
    public void testSaveTxn() throws Exception {
        Mockito.when(transactionServiceMock.saveTxn(Mockito.any(TransactionRequest.class))).thenReturn(true);
        ResponseEntity<?> responseEntity = transactionController.saveTxn(null, txn);
        assertTrue(responseEntity.getStatusCode().equals(HttpStatus.CREATED));        
    }

    @Test
    public void testGetStats() throws Exception {
        StatsResponse sr = new StatsResponse("0.00", "0.00", "0.00", "0.00", 0L);
        Mockito.when(transactionServiceMock.getStats()).thenReturn(sr);
        ResponseEntity<StatsResponse> responseEntity = (ResponseEntity<StatsResponse>) transactionController.getStats(null);
        System.out.println("sr response = "+responseEntity.getBody());
        assertTrue(responseEntity.getBody().equals(sr));        
    }

    @Test
    public void testDelete() throws Exception {
        Mockito.doNothing().when(transactionServiceMock).delete();
        ResponseEntity<HttpStatus> responseEntity = (ResponseEntity<HttpStatus>) transactionController.deleteTxn(null);
        System.out.println("sr response = "+responseEntity.getBody());
        assertTrue(responseEntity.getStatusCode().equals(HttpStatus.NO_CONTENT));        
    }

}

测试用例运行良好。

但我的申请被拒绝,具体原因如下:

即使您没有在测试中使用 SpringContext 并模拟所有内容,您也正在使用 SpringRunner。

现在,以下是我的担忧:

  1. 测试用例有什么问题?

  2. 上述拒绝原因是什么意思?

  3. 我该如何纠正?

4

2 回答 2

1

What's wrong with the test cases?

I think what they want you to do is to write a spring web layer test. This is not a spring MVC test/spring-boot test. Because you don't test the controller as a spring loaded resource. You test it as a simple java class. That won't prove whether it behaves as a Spring controller correctly. You won't be able to test features such as;

  • spring resource injection
  • request dispatching and validation

How can i correct that?

You have to write a spring MVC test and use MockMvc or RestTemplate to verify your controller. For example;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = YourContext.class)
@WebAppConfiguration
public class MyWebTests {

    @Autowired
    private WebApplicationContext wac;

    private MockMvc mockMvc;

    @Before
    public void setup() {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
    }

    @Test
    public void foo() throws Exception {
         mockMvc.perform(get("/status"));
       //and verification
    }

}

Usage of mockito mocks is not the worst idea, but you could have used auto wired @MockBeans.

If this is spring-boot, you will have more flexibility. Have a look at following resources.

https://docs.spring.io/spring/docs/current/spring-framework-reference/testing.html https://spring.io/guides/gs/testing-web/

于 2019-02-06T10:31:48.383 回答
0

您之所以抱怨,是因为您的测试中不需要 spring 的测试功能。您的测试是纯单元测试。

因此,如果您删除@RunWith(SpringRunner.class)任何内容,您的测试将不会发生任何变化。就放在那里@ExtendWith(MockitoExtension.class)

SpringRunner将为您初始化 spring 上下文以测试您可以使用以下注释注入或模拟应用程序的切片:

  • @MockBean
  • @Autowired
  • ETC..
于 2019-02-06T10:18:59.623 回答