java单元测试void控制器和服务方法

1bqhqjot  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(283)

我已经构建了一个restapi,它在产品数据库上执行基本的crud操作,现在我正在对控制器和服务层中的所有方法进行单元测试。我已经意识到addproduct、updateproduct或deleteproduct等方法在这两个层上都返回void。
在测试服务层时如何模拟存储库,在测试这些方法的控制器层时如何模拟服务?
控制器方法:

@RequestMapping(method= RequestMethod.POST,value ="/products")
public void addProduct(@RequestBody Product product){
    productService.addProduct(product);
}

服务方式:

public void addProduct(Product product){
    productRepository.save(product);
}

我已经在需要addproduct()方法帮助的字段中进行了注解。
控制器测试:

package com.product.controllers;

import com.product.entities.Product;
import com.product.services.ProductService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class ProductControllerTest {

@InjectMocks
ProductController productController;

@Mock
ProductService productService;

@Test
public void getHomePage(){
    String homePageText = "Welcome to the home page!";
    assertEquals(homePageText,productController.getHomePage());
}

@Test
public void getAllProducts(){

    Product product1 = new Product();
    product1.setId("001");
    product1.setName("TV");
    product1.setPrice(999.99);

    Product product2 = new Product();
    product2.setId("002");
    product2.setName("Mobile");
    product2.setPrice(300D);

    List<Product> listOfProducts = new ArrayList<>();
    listOfProducts.add(product1);
    listOfProducts.add(product2);

    when(productService.getAllProducts()).thenReturn(listOfProducts);
    assertEquals(listOfProducts,productController.getAllProducts());
}

@Test
public void getProductById(){

    Product product = new Product();
    product.setId("001");
    product.setName("TV");
    product.setPrice(999.99);

    when(productService.getProductById("001")).thenReturn(product);
    assertEquals(new ResponseEntity<Product> (product, HttpStatus.OK),productController.getProductById("001"));
}

@Test
public void addProduct() {

    Product product = new Product();
    product.setId("001");
    product.setName("TV");
    product.setPrice(999.99);

    // need help with this
}
}

服务测试:

package com.product.services;

import com.product.entities.Product;
import com.product.repositories.ProductRepository;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
public class ProductServiceTest {

@InjectMocks
ProductService productService;

@Mock
ProductRepository productRepository;

@Test
public void getAllProducts() {

    Product product1 = new Product();
    product1.setId("001");
    product1.setName("TV");
    product1.setPrice(999.99);

    Product product2 = new Product();
    product2.setId("002");
    product2.setName("Mobile");
    product2.setPrice(300D);

    List<Product> listOfProducts = new ArrayList<>();
    listOfProducts.add(product1);
    listOfProducts.add(product2);

    when(productRepository.findAll()).thenReturn(listOfProducts);
    assertEquals(listOfProducts, productService.getAllProducts());
}

@Test
public void getProductById() {

    Product product = new Product();
    product.setId("001");
    product.setName("TV");
    product.setPrice(999.99);

    when(productRepository.findById("001")).thenReturn(java.util.Optional.of(product));
    assertEquals(product, productService.getProductById("001"));

}

@Test
public void addProduct() {

    Product product = new Product();
    product.setId("001");
    product.setName("TV");
    product.setPrice(999.99);

    // need help with this
}
}
jtoj6r0c

jtoj6r0c1#

这段代码非常简单,所以我不确定您是否需要对它进行单元测试。但是如果你想,你可以利用你的mockito's mocks's verify 方法。例如测试时 addProduct :

@InjectMocks
ProductService productService;

@Mock
ProductRepository productRepository;

// ...

@Test
public void testAddProduct() { 
   Product dummyProduct = new Product();

   productService.addProduct(dummyProduct);

   // eq() is from the Mockito matchers package
   Mockito.verify(productRepository, Mockito.times(1)).save(eq(dummyProduct))
}

这验证了 save 在模拟存储库上按时调用,参数为 dummyProduct .
你可能还想看看mockito的“参数捕捉器”。

相关问题