创建一个测试类,并在其中引用已定义的 service 类:
@Slf4j
public class TestUtil {
@Autowired
private INoSqlChargeDbService dbService;
@Test
public void testDao() {
TblUpspSysUserEntity byOpenId = dbService.selectUpspSysUserByOpenId("openIdxxxxx");
System.out.println(byOpenId);
}
}
运行时会在 service 对象上报 NullPointerException:
解决方法:
在SpringBoot项目中使用测试类,测试类需要使用两个注解做配置解析:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringApplication.class)`
SpringBootTest 通过设置classes指定入口配置文件地址;
Junit4中必须要加入@RunWith注解才可以注入bean、service等,但是junit5则不需要