![](/img/trans.png)
[英]How to run all test classes but only start embedded Tomcat once when using Spring Boot?
[英]How to setup standalone Controller only once before all test cases in Spring Boot?
我正在為控制器編寫單元測試。 我在嘲笑服務層,並為休息控制器使用獨立的安裝程序。
ProductSupplierControllerUnitTest.java
public class ProductSupplierControllerUnitTest {
@Mock
private ProductSupplierService productSupplierService;
@InjectMocks
private ProductSupplierRestController productSupplierRestController;
private MockMvc mockMvc;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders.standaloneSetup(productSupplierRestController)
.setControllerAdvice(new ServiceExceptionHandler()).build();
}
@Test
public void productNotFound() throws Exception {
Long incorrectProductId = 2L;
Mockito.when(productSupplierService.getProductSuppliers(incorrectProductId, tenantId))
.thenThrow(new EntityNotFoundException(Product.class, String.valueOf(incorrectProductId)));
RequestBuilder requestBuilder = MockMvcRequestBuilders.get(prepareRequestUrl(incorrectProductId))
.requestAttr(TENANT_ID, tenantId).contentType(MediaType.APPLICATION_JSON_UTF8);
mockMvc.perform(requestBuilder).andExpect(status().isNotFound())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)).andExpect(jsonPath("$.message",
is(String.format("Product was not found for parameter(s) %s", incorrectProductId))));
Mockito.verify(productSupplierService, times(1)).getProductSuppliers(incorrectProductId, tenantId);
Mockito.verifyNoMoreInteractions(productSupplierService);
}
@Test
public void getProductSuppliersSuccess() throws Exception {
Map<String, List<? extends BaseDTO>> result = new HashMap<>(0);
ProductSupplierDTO productSupplierDTO = new ProductSupplierDTO();
productSupplierDTO.setSupplierId(correctSupplierId);
productSupplierDTO.setBuyPrice(validBuyPrice);
productSupplierDTO.setDefaultSupplier(isDefaultSupplier);
result.put("product_suppliers", Collections.singletonList(productSupplierDTO));
Mockito.when(productSupplierService.getProductSuppliers(productId, tenantId)).thenReturn(result);
RequestBuilder requestBuilder = MockMvcRequestBuilders.get(prepareRequestUrl(productId))
.requestAttr(TENANT_ID, tenantId).contentType(MediaType.APPLICATION_JSON_UTF8);
mockMvc.perform(requestBuilder).andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$.product_suppliers", hasSize(1)))
.andExpect(jsonPath("$.product_suppliers[0].supplier_id", is(correctSupplierId.intValue())))
.andExpect(jsonPath("$.product_suppliers[0].buy_price", is(validBuyPrice)))
.andExpect(jsonPath("$.product_suppliers[0].default_supplier", is(isDefaultSupplier)));
Mockito.verify(productSupplierService, times(1)).getProductSuppliers(productId, tenantId);
Mockito.verifyNoMoreInteractions(productSupplierService);
}
// some more tests
}
測試工作正常,但問題是,在每個測試用例之前都要完成此獨立設置,因為每個測試用例的控制器方法都映射到url並初始化了Spring FrameworkServlet。
是否可以對所有測試用例僅執行一次初始化? 因為我想減少測試時間。
編輯 :我已將代碼更改為以下代碼,並且按預期工作。 問題是測試是否正確?
@RunWith(MockitoJUnitRunner.class)
public class ProductSupplierControllerUnitTest {
@Mock
private ProductSupplierService productSupplierService;
@InjectMocks
private static ProductSupplierRestController productSupplierRestController = new ProductSupplierRestController();
private static MockMvc mockMvc = MockMvcBuilders.standaloneSetup(productSupplierRestController)
.setControllerAdvice(new ServiceExceptionHandler()).build();
// tests
}
使控制器和嘲笑靜態化是一種好習慣嗎?
如果使用JUnit,則使用注釋@BeforeClass而不是@Before。 具有該注釋的方法將在類中的所有測試之前被調用一次。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.