我想使用Junit + Mockito.
如何使用我的ServiceTest
类调用实际的服务层方法,如果我模拟ServiceTest
类,那么它的对象不会执行实际的服务方法代码,因为它不会让对象调用它的方法如果我尝试与间谍仍然无法正常工作,I tried this example
我仍然无法执行测试用例。
MyService.java
@Service
public class MyService{
@Autowired
Utility utility;
public String showResult(){
String result = utility.getName();
return result;
}
}
MyServiceTest.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);
}
}
}
答案 0 :(得分:4)
您必须首先模拟Utility
类,然后必须在使用@Test
调用MockitoAnnotations.initMocks(this)
之前调用它,如下所示:
<强> MyServiceTest.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();
}
}
}
<强> MyService.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;
}
}
<强> Utility.java 强>
import org.springframework.stereotype.Component;
@Component
public class Utility {
public String getName() {
return "hello";
}
}
答案 1 :(得分:1)
如何使用@MockBean
?它适合Spring + JUnit,可能你需要实现模拟行为。
我猜Utility.getName()
返回&#34;测试&#34;在测试用例中。
以下是我试过的测试代码。
@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();
}
}
}
答案 2 :(得分:0)
利用@Spy
调用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";
}
}
}
仍有问题与您正在使用的弹簧版本共享