mockmvc时,spring-@autowire环境为空

hyrbngr7  于 2021-07-23  发布在  Java
关注(0)|答案(0)|浏览(332)

我使用springboot和mockmvc来测试springapi。当我试图通过isomnia或postman测试api时,api按照我的预期正常工作,@autowire注入的环境变量有一个值。但是当我使用mockmvc进行测试时,环境变量返回空值。
这是我的控制器代码:

  1. @Autowired
  2. private Environment env;
  3. @GetMapping(path = { "/", "" })
  4. @ResponseBody
  5. @ResponseStatus(code = HttpStatus.OK)
  6. public ReturnListDto<AttributeTypeReturnDto> getAttributeTypes(HttpServletRequest request) {
  7. LoggerUtilities.logInfor(logger, "Get AttributeType: " + env);
  8. System.out.println("HERER_0__: " + env); //Return null value when using mockmvc to test
  9. ELKLogTCP logELK = new ELKLogTCP(env, request);
  10. try {
  11. //Some code call service
  12. logELK.setSuccess();
  13. } catch (Exception e) {
  14. logELK.setFail(e, ErrorCode.UNKNOWN_ERROR);
  15. }
  16. return data;
  17. }

这是我的测试课:

  1. public class AttributeTypeControllerTest {
  2. @Mock
  3. private AttributeTypeService attributeTypeService;
  4. @Spy
  5. private Environment env;
  6. @Autowired
  7. private MockMvc mockMvc;
  8. @InjectMocks
  9. private AttributeTypeController attributeTypeController;
  10. private AttributeType attributeType;
  11. private static MockedStatic mocked;
  12. @BeforeEach
  13. public void setup(){
  14. MockitoAnnotations.initMocks(this);
  15. mockMvc = MockMvcBuilders.standaloneSetup(attributeTypeController).build();
  16. attributeType = new AttributeType();
  17. attributeType.setId(1L);
  18. }
  19. @BeforeAll
  20. static void install() {
  21. mocked = mockStatic(AttributeTypeMappingDto.class);
  22. }
  23. @Test
  24. void getAttributeTypes() throws Exception {
  25. //DataTest
  26. ReturnListDto<AttributeTypeReturnDto> returnListDto = new ReturnListDto<>();
  27. AttributeTypeReturnDto attributeTypeReturnDto = Common.convertObject(attributeType, AttributeTypeReturnDto.class);
  28. attributeTypeReturnDto.setName("2");
  29. attributeTypeReturnDto.setId(1L);
  30. List<AttributeTypeReturnDto> returnDtos = new ArrayList<>();
  31. returnDtos.add(attributeTypeReturnDto);
  32. returnListDto.setReturnData(returnDtos);
  33. returnListDto.setState(true);
  34. List<AttributeType> attributeTypes = new ArrayList<>();
  35. attributeTypes.add(attributeType);
  36. //DataTest
  37. //It notworking - function call inside class
  38. ELKLogTCPMockito.when(this.env.getProperty("spring.application.name")).thenReturn("iva-product-service");
  39. //It notworking
  40. Mockito.doReturn("iva-product-service").when(env).getProperty("spring.application.name");
  41. //Donothing not working at all
  42. ELKLogTCP logELK = new ELKLogTCP();
  43. ELKLogTCP spyUtils = Mockito.spy(logELK);
  44. Mockito.doNothing().when(spyUtils).setSuccess();
  45. Mockito.doNothing().when(spyUtils).setFail(new RuntimeException(), ErrorCode.UNKNOWN_ERROR);
  46. //Donothing
  47. mocked.when(()->AttributeTypeMappingDto.convertAttributeTypeToDto(attributeType)).thenReturn(attributeTypeReturnDto);
  48. mockMvc.perform(get("/api/attrType/"))
  49. .andExpect(status().isOk())
  50. .andExpect(jsonPath("$.state", org.hamcrest.Matchers.equalTo(returnListDto.isState())))
  51. .andDo(MockMvcResultHandlers.print());
  52. }
  53. }

类elklogtcp不是一个bean,我在这个类中获得了environment属性,但是在我将它传递到elklogtcp之前,它在控制器中获得null。
顺便说一句,我尝试在测试api时使用donothing()跳过日志记录,但它不起作用。
我的问题是:
测试时如何获取环境变量
顺便说一句,如何和donothing一起工作。
任何人都可以暗示我做错了什么。谢谢您!!
[编辑]
当我将环境变量解析到控制器的构造函数时,它返回mockito环境(当使用test mockito时,但当使用test mockito时,它工作正常)

  1. public AttributeTypeController(AttributeTypeService attrTypeService, Environment env) {
  2. this.attrTypeService = attrTypeService;
  3. this.env = env;
  4. }
  5. HERER_0__: org.springframework.core.env.Environment$MockitoMock$1203144333@344f4dea

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题