Java 问:Mockito-使用@Mock和@Autowired

Java 问:Mockito-使用@Mock和@Autowired,java,junit,mockito,Java,Junit,Mockito,我想使用Mockito测试一个服务类,它还有两个其他服务类,如下所示 @Service public class GreetingService { private final Hello1Service hello1Service; private final Hello2Service hello2Service; @Autowired public GreetingService(Hello1Service hello1Service, Hello2Se

我想使用
Mockito
测试一个服务类,它还有两个其他服务类,如下所示

@Service
public class GreetingService {

    private final Hello1Service hello1Service;
    private final Hello2Service hello2Service;

    @Autowired
    public GreetingService(Hello1Service hello1Service, Hello2Service hello2Service) {
        this.hello1Service = hello1Service;
        this.hello2Service = hello2Service;
    }

    public String greet(int i) {
        return hello1Service.hello(i) + " " + hello2Service.hello(i);
    }
}

@Service
public class Hello1Service {

    public String hello(int i) {

        if (i == 0) {
            return "Hello1.";
        }

        return "Hello1 Hello1.";
    }
}

@Service
public class Hello2Service {

    public String hello(int i) {

        if (i == 0) {
            return "Hello2.";
        }

    return "Hello2 Hello2.";
    }
}    
我知道如何用
Mockito
模拟
Hello1Service.class
Hello2Service.class
,如下所示

@RunWith(MockitoJUnitRunner.class)
public class GreetingServiceTest {

    @InjectMocks
    private GreetingService greetingService;

    @Mock
    private Hello1Service hello1Service;

    @Mock
    private Hello2Service hello2Service;

    @Test
    public void test() {

        when(hello1Service.hello(anyInt())).thenReturn("Mock Hello1.");
        when(hello2Service.hello(anyInt())).thenReturn("Mock Hello2.");

        assertThat(greetingService.greet(0), is("Mock Hello1. Mock Hello2."));
    }
}
我想模拟
Hello1Service.class
并使用
@Autowired
注入
Hello2Service.class
,如下所示。 我尝试使用
@SpringBootTest
,但它不起作用。 有更好的办法吗

@RunWith(MockitoJUnitRunner.class)
public class GreetingServiceTest {

    @InjectMocks
    private GreetingService greetingService;

    @Mock
    private Hello1Service hello1Service;

    @Autowired
    private Hello2Service hello2Service;

    @Test
    public void test() {

        when(hello1Service.hello(anyInt())).thenReturn("Mock Hello1.");
        assertThat(greetingService.greet(0), is("Mock Hello1. Hello2."));
    }
}

您希望使用一些要形成的功能注入依赖项,然后使用
@Spy
。 您不需要加载Spring容器并使用
@Autowired

@Spy
private Hello2Service hello2Service=new Hello2Service();
您可以阅读有关模拟与间谍的更多详细信息


您可以使用Spy更改真实对象而不是模拟对象。 测试代码将是这样的

@RunWith(MockitoJUnitRunner.class)
public class GreetingServiceTest {

    @InjectMocks
    private GreetingService greetingService;

    @Mock
    private Hello1Service hello1Service;

    @Spy
    private Hello2Service hello2Service;

    @Test
    public void test() {

        when(hello1Service.hello(anyInt())).thenReturn("Mock Hello1.");
        assertThat(greetingService.greet(0), is("Mock Hello1. Hello2."));
    }
}

谢谢你的回答。它起作用了。如果Hello2Service有一个jpa存储库,我应该如何实例化Hello2Service?像
public class public class Hello2Service{@Autowired HelloRepository HelloRepository;..
@Repository接口HelloRepository扩展了JpaRepository…