【发布时间】:2021-12-07 22:13:15
【问题描述】:
我能否通过在测试类上设置@ActiveProfiles 来访问我尝试测试的类中的 enviorment.getProperty("spring.profiles.active"),
@RunWith(SpringJUnit4ClassRunner.class)
@WebMvcTest(value = TradeController.class)
@ActiveProfiles("test)
class TradeControllerTest{
@Autowired
private MockMvc mockMvc;
@MockBean
private TradeServiceImpl tradeServiceImpl;
@Mock
private ConfigurableEnvironment enviroment;
@Before
public void setup(){
MockitoAnnotations.initMocks(this);
}
@Test
public void test() throws Exception{
Mockito.doNothing().when(tradeServiceImpl).getAllTrades(any());
RequestBuilder requestBuilder = MockMvcRequestBuilder.get(new
URI("/getTrades")).accept(MediaType.APPLICATION_JSON);
MvcResult result = mockMvc.perform(requestBuilder).andReturn(); // calling the
actual class under test
assertEquals(result.getResponse().getStatus(), 400);
}
}
下面是课程,我正在阅读 spring.profiles.active。然而,它即将为空
@RestController
public class TradeController{
@Autowired
private TradeServiceImpl tradeServiceImpl;
@Autowired
private Environment environment;
@GetMapping("/getTrade")
public ResponseEntity<String> getTrade(@PathVariable final String
tradeId){
String profile = environment.getProperty("spring.profiles.active");
}
}
【问题讨论】:
标签: spring-boot spring-mvc spring-test