代码之家  ›  专栏  ›  技术社区  ›  KayV

在编写JUnit测试用例时,在没有SpringContext的情况下使用SpringRunner是否是一种良好的做法?

  •  0
  • KayV  · 技术社区  · 7 年前

    我用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. 我该怎么纠正呢?

    2 回复  |  直到 7 年前
        1
  •  1
  •   Laksitha Ranasingha    7 年前

    测试用例有什么问题?

    • 春季资源注入
    • 请求分派和验证

    我该怎么纠正呢?

    您必须编写一个SpringMVC测试并使用 MockMvc 或 RestTemplate

    @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
        }
    
    }
    

    使用mockito mocks并不是最糟糕的主意,但您可以使用自动连线 @MockBean

    如果这是spring引导,您将拥有更大的灵活性。请查看以下资源。

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

        2
  •  0
  •   Igor Konoplyanko    7 年前

    您有投诉,因为您的测试中不需要spring的测试功能。 您的测试是纯单元测试。

    @RunWith(SpringRunner.class) @ExtendWith(MockitoExtension.class)

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

    • @MockBean
    • @Autowired
    • 等