Java 在SpringBoot应用程序中使用Mockito
我对SpringBoot很熟悉,我已经编写了一个小应用程序。它有一个Java 在SpringBoot应用程序中使用Mockito,java,unit-testing,spring-boot,mockito,junit4,Java,Unit Testing,Spring Boot,Mockito,Junit4,我对SpringBoot很熟悉,我已经编写了一个小应用程序。它有一个ProductRepository扩展crudepository。我的ProductService是: public interface ProductService { Iterable<Product> listAllProducts(); Product getProductById(Integer id); Product saveProduct(Product product); void
ProductRepository
扩展crudepository
。我的ProductService
是:
public interface ProductService {
Iterable<Product> listAllProducts();
Product getProductById(Integer id);
Product saveProduct(Product product);
void deleteProduct(Integer id);
}
我想知道如何在没有外部依赖的情况下进行单元测试ProductRepository
(上面的测试有外部依赖,因此不符合单元测试的条件。我相信这更像是一个集成测试)
另外,如何对我的产品服务进行单元测试?我试着用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()
,又有什么区别呢?非常感谢您的帮助。如果您想编写单元测试,请删除所有用于集成测试的不必要注释
您还需要在@Before
方法中启动模拟:
public class ProductServiceImplTest {
@InjectMocks
private ProductServiceImpl productServiceImpl;
@Mock
private ProductRepository productRepository;
@Mock
private Product product;
@Before
public void setupMock() {
MockitoAnnotations.initMocks(this);
}
我认为在您的情况下,使用mock
就足够了
您应该使用间谍的唯一时间是在您正在测试的类上调用它,在您的情况下,该类将是:
productServiceImpl=new ProductServiceImpl();
prodServiceSpy = spy(productServiceImpl);
并模拟一些您不希望调用的实现,或使用测试场景所需的自定义实现来调用的方法。感谢您明确的回答,这正是我所要的。我编辑了这个问题,但仍然无法理解抛出NullPointerException的原因。您需要将存储库或任何其他依赖项注入服务。我已经使用mockito注释更新了示例。。让生活更轻松
productServiceImpl=new ProductServiceImpl();
prodServiceSpy = spy(productServiceImpl);