我熟悉SpringBoot,我写了一个小应用程序。它有一个ProductRepository
延伸CrudRepository
。我的ProductService
是这样的:
public interface ProductService {
Iterable<Product> listAllProducts();
Product getProductById(Integer id);
Product saveProduct(Product product);
void deleteProduct(Integer id);
}
ProductServiceImpl
中的ProductRepository
类自动连线,并使用ProductRepository
提供实施。
我的应用程序按预期运行,这就是我测试存储库的方式。
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {RepositoryConfiguration.class})
public class ProductRepositoryTest {
private ProductRepository productRepository;
@Autowired
public void setProductRepository(ProductRepository productRepository) {
this.productRepository = productRepository;
}
@Test
public void testSaveProduct(){
//setup product
Product product = new Product();
product.setDescription("Shirt");
product.setPrice(new BigDecimal("18.95"));
product.setProductId("1234");
assertNull(product.getId()); //null before save
productRepository.save(product);
assertNotNull(product.getId()); //not null after save
//fetch from DB
Product fetchedProduct = productRepository.findOne(product.getId());
//should not be null
assertNotNull(fetchedProduct);
}
}
我想知道如何在没有外部依赖性的情况下对ProductRepository
进行单元测试(上面的测试具有这一点,因此不具备单元测试资格。我相信它更像是一个整合测试)。
另外,我如何对ProductService
进行单元测试?我尝试用Mockito模仿ProductRepository
和Product
,就像这样:
public class ProductServiceImplTest {
private ProductServiceImpl productServiceImpl;
private ProductRepository productRepository;
private Product product;
@Before
public void setupMock() {
MockitoAnnotations.initMocks(this);
productServiceImpl=new ProductServiceImpl();
productRepository = mock(ProductRepository.class);
product = mock(Product.class);
}
@Test
public void testRetrieveById() throws Exception {
when(productRepository.findOne(5)).thenReturn(product);
assertEquals(product, productServiceImpl.getProductById(5));
}
}
但这就是我得到的:
java.lang.NullPointerException
at
ProductServiceImpl.getProductById(ProductServiceImpl.java:24)
at ProductServiceImplTest.testRetrieveById(ProductServiceImplTest.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke
(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke
(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall
(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run
(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
我正确使用Mockito.mock()
吗?如果我使用Mockito.spy()
代替什么会有什么不同?任何帮助将受到高度赞赏。
答案 0 :(得分:2)
如果您想编写单元测试,请删除所有用于集成测试的不必要的注释。
您还需要使用@Before
方法启动模拟:
public class ProductServiceImplTest {
@InjectMocks
private ProductServiceImpl productServiceImpl;
@Mock
private ProductRepository productRepository;
@Mock
private Product product;
@Before
public void setupMock() {
MockitoAnnotations.initMocks(this);
}
我认为在你的情况下,使用mock
就足够了。
您应该使用spy
的唯一时间是在您正在测试的类中调用它,在您的情况下:
productServiceImpl=new ProductServiceImpl();
prodServiceSpy = spy(productServiceImpl);
并模拟一些您不希望调用的实现方法,或使用测试方案所需的自定义实现调用。