【问题标题】:JUnit testing of spring boot @value variablespring boot @value变量的JUnit测试
【发布时间】:2020-05-22 16:13:04
【问题描述】:

我正在尝试为在“if”中测试 2 个值的方法实现单元测试。我怎样才能做到这一点 ?

对于 valService.validate 部分,我调用了 when.thenReturn 并且它工作正常,但对于 equals 部分,我的测试无法变为绿色,当我调试时我看到它没有通过第一部分如果并且那个 userTypeSpecific 有一个 null 值。

编辑:我从我的 app.properties 中获得了带有 @value 的 userTypeSpecific 变量

我的控制器

@Value("${app.usertype.specific}")
String userTypeSpecific;

@PostMapping(value = "decline")
@ResponseBody
public ResponseEntity<Boolean> declineUser(@RequestParam final String idUser, @RequestHeader("userType") String userType) {
    HttpStatus httpStatus = HttpStatus.FORBIDDEN;
    Boolean result = false;

    if (userTypeSpecific.equals(userType) && valService.validate(idUser, userType)) {
        result = this.service.declineUser(idUser);
        if (result){
            httpStatus = HttpStatus.OK;
        }
    }
    return ResponseEntity.status(httpStatus).body(result);
}

这是我的测试代码:

@SpringBootTest
@ExtendWith(SpringExtension.class)
@RunWith(SpringJUnit4ClassRunner.class)
@TestPropertySource(properties = {"app.usertype.specific=spec",})
public class UserControllerTest {
    @Mock
    IUserService service;

    @Value("${app.usertype.specific}")
    String userType;

    @Mock
    IValService valservice;

    @InjectMocks
    UserController controller;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
    }

    @Configuration
    static class Config {
        @Bean
        public static PropertySourcesPlaceholderConfigurer propertiesResolver() {
            return new PropertySourcesPlaceholderConfigurer();
        }
    }

    @Test
    public void testDeclineUser() {
        String idUser= "123";

        Assert.assertEquals("TEST userType", "gae", userType);
        when(valservice.validate(any(),any())).thenReturn(Boolean.TRUE);
        when(service.declineUser(idUser)).thenReturn(true);

        ResponseEntity<Boolean> resultStatus = controller.declineUser(idUser, userType);

        verify(this.service,times(1)).declinePieceJustif(numeroDemande);
        Assert.assertEquals("HTTP status test, HttpStatus.OK, resultStatus.getStatusCode());
        Assert.assertEquals ("Boolean status test", true, resultStatus.getBody());
    }



}

【问题讨论】:

  • 能否提供您的代码来展示测试实现?
  • 能否请您补充一下等于实现?
  • 我提供了我的控制器和我的测试实现

标签: java spring spring-boot junit mockito


【解决方案1】:

我在 @Before 方法中找到了一种使用 ReflectionTestUtils.setField 的方法:

 @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        String userTypeSpecific ="spec";
        ReflectionTestUtils.setField(controller, "userTypeSpecific", userTypeSpecific);
    }

【讨论】:

    【解决方案2】:

    我假设,您已经成功地模拟了“userType”,因此为了模拟“userTypeSpecific”,您可以使用 Spring 的 ReflectionTestUtils.setField 方法来模拟 @value 变量。 您可以在此处找到详细信息: https://www.briandupreez.net/2011/04/little-spring-gem-reflectiontestutils.html

    【讨论】:

    • 我为此使用了@TestPropertySource。 ReflectionTestUtils.setField 更容易吗?
    • 谢谢,它成功了。我在@Before 方法中添加了它;)
    • 我很高兴能提供帮助。 :)
    猜你喜欢
    • 1970-01-01
    • 2023-04-02
    • 2019-10-26
    • 2017-06-06
    • 1970-01-01
    • 2019-10-19
    • 2020-07-27
    • 2014-01-19
    • 2018-02-23
    相关资源
    最近更新 更多