【发布时间】:2020-05-28 09:46:32
【问题描述】:
我的服务
@Service
public class StripeServiceImpl implements StripeService {
@Override
public int getCustomerId() {
return 2;
}
}
我的测试
public class StripeServiceTests {
@Autowired
StripeService stripeService;
@TestConfiguration
static class TestConfig {
@Bean
public StripeService employeeService() {
return new StripeServiceImpl();
}
}
@Test
public void findCustomerByEmail_customerExists_returnCustomer() {
assertThat(stripeService.getCustomerId()).isEqualTo(2);
}
}
错误:java.lang.NullPointerException。我查过了,stripeService 实际上是空的。
【问题讨论】:
-
检查StripeServiceImpl类中是否有@Service注解
-
是的,当然。我的应用程序仍然可以正常运行。我已经编辑了我的问题。
-
尝试删除 TestConfig 类并检查。如果您已经自动装配,则无需再次定义 bean。
-
可能在测试类中缺少
@RunWith(SpringRunner.class)(JUnit 4)或@ExtendWith(SpringExtension.class)(JUnit 5)。
标签: spring spring-boot junit5 spring-test