//指定springboot应用启动类
@SpringBootTest(classes = IncBdpServerApplication.class,webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@RunWith(SpringRunner.class)
@ContextConfiguration
//指定启动哪个环境的配置文件,一般的主文件是:applicaton.properties,不同的环境对应不同的文件,
//例如:applicaton-dev.properties,applicaton-sit.properties,applicaton-prod.properties
@ActiveProfiles("ci")

webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,这个配置是websocket用的,如果项目中用到了websocket,单元测试需要加上这个,否则可能会报错:

javax.websocket.server.ServerContainer not available

示例如下:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = IncBdpServerApplication.class)
@ContextConfiguration
@Slf4j
@ActiveProfiles("ci")
public class OpsSlaServiceTest {
    @Autowired
    private OpsSlaService opsSlaService;

    @Test
    public void testBasicProfile() {
        BasicProfile basicProfile = opsSlaService.basicProfile(84, "2022-06-27");
        log.info("result:{}", JSON.toJSONString(basicProfile));
        Assert.assertTrue(basicProfile.getFailTaskNo() > 0);
    }

}

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐