我想使用 Junit + Mockito.
为 spring 框架的服务层编写测试用例
如何使用我的 ServiceTest
类调用实际的服务层方法,如果我模拟ServiceTest
类,那么它的对象将不会执行实际的服务方法代码,因为它不会让对象调用它的方法,如果我尝试使用 Spy 它仍然不起作用,我尝试了这个例子我仍然无法执行测试用例。
我的服务.java
@Service
public class MyService{
@Autowired
Utility utility;
public String showResult(){
String result = utility.getName();
return result;
}
}
我的服务测试.java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader=AnnotationConfigWebContextLoader.class)
@WebAppConfiguration
public class MyServiceTest {
@Autowired
MyService myService;
@Autowired
Utility utility;
@Test
public void testShowResult() throws Exception {
assertEquals("Test",myService.showResult());
}
@Configuration
static class MykServiceTestContextConfiguration {
@Bean
public MyService myService() {
return new MyService();
}
@Bean
public Utility utility() {
return Mockito.mock(Utility.class);
}
}
}
您必须首先模拟Utility
类,然后在使用 MockitoAnnotations.initMocks(this)
调用@Test
之前调用它,如下所示:
我的服务测试.java
import static org.mockito.Mockito.when;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.AnnotationConfigWebContextLoader;
import org.springframework.test.context.web.WebAppConfiguration;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigWebContextLoader.class)
@WebAppConfiguration
public class MyServiceTest {
@InjectMocks
private MyService myService;
@Mock
private Utility utility;
@Before
public void setupMock() {
MockitoAnnotations.initMocks(this);
}
@Test
public void testShowResult() throws Exception {
when(utility.getName()).thenReturn("Test");
Assert.assertEquals("Test", myService.showResult());
}
@Configuration
static class MykServiceTestContextConfiguration {
@Bean
public MyService myService() {
return new MyService();
}
@Bean
public Utility utility() {
return new Utility();
}
}
}
我的服务.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class MyService {
@Autowired
private Utility utility;
public String showResult() {
String result = utility.getName();
return result;
}
}
效用.java
import org.springframework.stereotype.Component;
@Component
public class Utility {
public String getName() {
return "hello";
}
}
利用@Spy
当调用间谍时,则调用真实对象的实际方法。
https://www.tutorialspoint.com/mockito/mockito_spying.htm
请仔细阅读教程
这对我有用
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@WebAppConfiguration
public class MyServiceTest {
@Spy
MyService myService;
@Test
public void testShowResult() throws Exception {
assertEquals("Test",myService.showResult());
}
@Service
public class MyService{
public String showResult(){
return "Test";
}
}
}
仍然有问题共享您正在使用的春季版本
使用@MockBean
怎么样?它适合Spring + JUnit,并且可能需要实现模拟行为。
我想Utility.getName()
测试用例中返回"测试"。
以下是我尝试过的测试代码。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigWebContextLoader.class)
@WebAppConfiguration
public class MyServiceTest {
@Autowired
MyService myService;
@MockBean
Utility utility;
@Test
public void testShowResult() throws Exception {
Mockito.when(utility.getName()).thenReturn("Test");
assertEquals("Test", myService.showResult());
}
@Configuration
static class MykServiceTestContextConfiguration {
@Bean
public MyService myService() {
return new MyService();
}
}
}