java - 如何使用Spring模拟从同一个类的方法调用的方法?

标签 java spring-boot mocking automated-tests mockito

我有一个具有这种架构的 spring-boot 应用程序:

@Controller > @Service > @Component

这是我的组件:

@Component
public class myComponent {

    @Autowired
    private ObjectMapper mapper;
    //com.fasterxml.jackson.databind.ObjectMapper

    @Autowired
    private Component2 component2;
    // some 3rd part REST service

    @Autowired
    private Component3 component3;
    //database access

    @Transactional(propagation = Propagation.REQUIRED)
    public List<SomePOJO> method1(String otherString) {
        String newOne = myString + otherString; //more logic here, but without components
        return this.method2(newOne);
    }

    public List<SomePojo> method2(String newOne){
        String one = component3.methodX(newOne); //access database with component3
        return component2.methodY(one); //more logic here, including component2 and mapper!
    }

}

使用Mockito我在我的测试类中实现了这个:

@MockBean
public myComponent component;

@Before
public void before() throws Exception {
    MockitoAnnotations.initMocks(this);
    List<POJO> someList = new ArrayList<>(); //list populated with some specific POJO's
    Mockito.when(component.method1("specificString")).
        thenReturn(someList);
}

@Test
public void recuperaDados() throws Exception  {
    String response = given().authentication().preemptive().oauth2("loginInfo")
            .get("myComponent_method1_path/" + "specificString1").asString();
    //this specificString1 leads to myComponent.method1("specificString"), satisfying Mockito 
}

这样我就成功地完成了我的测试。但是当我使用

Mockito.when(component.method2("specificString2")).thenReturn(someList);

然后

String response1 = given().authentication().preemptive().oauth2("loginInfo")
            .get("myComponent_method1_path/" + "specificString1").asString();
// this specificString1 leads to myComponent.method2("specificString2"), also satisfying Mockito
String response2 = component.method2("specificString2");
String response3 = component.method2("randomString");

response1 是 "",response2 是正确字符串化的 someList,response3 是 null。预计会有 response2response3。但我预计response1 与response2 相同或至少为null。如何正确模拟同一个类中其他人调用的方法并测试调用另一个方法?

编辑

我的测试类扩展了这个类:

@ActiveProfiles("test")
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
public abstract class AbstractRestTest {

    @Autowired
    private WebApplicationContext wac;

    protected Dsl dsl = Dsl.getInstance();

    @Before
    public void globalSetup() {
        RestAssuredMockMvc.webAppContextSetup(wac);
    }

}

最佳答案

Mockito 通过代理您希望 stub 的对象来工作,因此,在调用模拟对象上的方法时,您的实现代码永远不会被执行,除非专门定义这样做。因此,通过在模拟对象上调用 method1,method2 将永远不会被调用。

但是,有一种方法可以通过部分 mock 来实现您想要的效果。在您希望测试 method2 的测试中,您可以指示应该执行真正的 method1:

when(component.method1("specificString")).thenCallRealMethod();

需要注意的是,mockito documentation表示使用部分模拟可能表明存在代码味道。因此问题是,method2 真的必须公开吗?如果它只是公开的,因为您希望模拟它的实现,那么这是一个错误。使用模拟,您只想 stub 对象的公共(public) api。任何私有(private)或辅助方法都应被视为内部方法并被忽略。如果您测试该对象,则同样如此,只应测试公共(public)方法,并间接测试辅助方法。

你是对的,第一个结果是空字符串而不是 null 是奇怪的。我认为这可能与尚未提供的代码中的某些内容有关,例如 Controller 或服务。

关于java - 如何使用Spring模拟从同一个类的方法调用的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50457616/

相关文章:

java - Guice:辅助注入(inject)和提供者

java - 为什么我看不到 ID token 或身份验证 token ?

java - 仅从Eclipse JEE Neon运行/调试Spring Boot应用程序?

c# - Moq - 无法遍历隐藏的 IEnumerable

go - 如何为从 stdin 读取的内容编写 go Test 函数?

java - 返回流而不是列表

java - StandardWrapperValve [Faces Servlet] : PWC1406 Service. service() 空指针异常

java - Delphi XE5 中的谷歌云消息传递?

spring - 如何删除Spring Security 6和Spring Boot 3中的ROLE_前缀?

python-3.x - 如何模拟 googleapiclient.discovery.build 以单元测试从谷歌表格读取