在webmvctest中加载依赖项

huangapple go评论77阅读模式
英文:

Load dependency in webmvctest

问题

以下是翻译好的部分:

我有一个Rest控制器,我正在尝试进行单元测试:

它有一些自动装配的依赖项

@RestController
@RequestMapping("/test")
public class TestController {

    private final Dep1 dep1;
    private final Dep2 dep2;
    private final Dep3 dep3;

    public TestController(final Dep1 dep1,
                          final Dep2 dep2,
                          final Dep3 dep3) {
        this.dep1 = dep1;
        this.dep2 = dep2;
        this.dep3 = dep3;
    }
   
}

我最近添加了dep3

@Service
public class Dep3 {

    private final IValidator validator;

    public Dep3(final IValidator validator) {
        this.validator= validator;
    }

    public void validate(final Request req) {
        validator.validate(req);
    }

}

Dep3有它自己的Autowired依赖IValidator

这是我的测试类:

@WebMvcTest(TestController.class)
public class TestControllerTest {

    @MockBean
    private Dep1 dep1;

    @MockBean
    private Dep2 dep2;

    @MockBean
    private Dep3 dep3;

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void someTest() throws Exception {

    }

    @TestConfiguration
    static class InnerConfiguration {

        @Bean
        IValidator validator() {
            return new SomeValidator();
        }
    }

}

对于我的测试,我需要实际运行dep3.validate(..),并使用实现SomeValidator()。我不确定如何实现这一点。也许我缺少一个注释?

英文:

I have a rest controller which I am trying to Unit Test:

It has a few dependencies autowired

@RestController
@RequestMapping("/test")
public class TestController {

    private final Dep1 dep1;
    private final Dep2 dep2;
    private final Dep3 dep3;

    public TestController(final Dep1 dep1,
                          final Dep2 dep2,
                          final Dep3 dep3) {
        this.dep1 = dep1;
        this.dep2 = dep2;
        this.dep3 = dep3;
    }
   
}

I recently added dep3

@Service
public class Dep3 {

    private final IValidator validator;

    public Dep3(final IValidator validator) {
        this.validator= validator;
    }

    public void validate(final Request req) {
        validator.validate(req);
    }

}

Dep3 has its own Autowired Dependency IValidator

Here is my test class:

@WebMvcTest(TestController.class)
public class TestControllerTest {

    @MockBean
    private Dep1 dep1;

    @MockBean
    private Dep2 dep2;

    @MockBean
    private Dep3 dep3;

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void someTest() throws Exception {

    }

    @TestConfiguration
    static class InnerConfiguration {

        @Bean
        IValidator validator() {
            return new SomeValidator();
        }
    }

}

For my test, I need the code to actually run the dep3.validate(..) with the implementation SomeValidator(). I am unsure how to achieve this. Perhaps I am missing an annotation?

答案1

得分: 1

不要模拟 Dep3。而是在 @TestConfiguration 中配置 Dep3 的 bean:

@TestConfiguration
static class InnerConfiguration {

    @Bean
    Dep3 dep3() {
        return new Dep3(validator());
    }

    @Bean
    IValidator validator() {
        return new SomeValidator();
    }
}

Spring 将会将完全配置好的 Dep3 注入到 TestController 中。

英文:

Don't mock Dep3. Instead configure Dep3 bean in @TestConfiguration:

@TestConfiguration
static class InnerConfiguration {

        @Bean
        Dep3 dep3() {
            return new Dep3(validator());
        }

        @Bean
        IValidator validator() {
            return new SomeValidator();
        }
    }

Spring will inject fully configured Dep3 into TestController

答案2

得分: 0

你必须正确配置你的模拟。
类似这样:

Mockito.when(dep3.validate(Mockito.any(Request.class))).thenReturn(new SomeValidator());

你可以在测试方法中或在实际调用之前的设置方法中完成这个步骤。

英文:

You have to configure your Mock correctly.
Something like this:

Mockito.when(dep3.validate(Mockito.any(Request.class))).thenReturn(new SomeValidator());

You can do this in your test method or in the setup method before the actual call.

huangapple
  • 本文由 发表于 2020年9月4日 18:57:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63739827.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定