尝试使用 junit 5 测试包含自动装配的 SprngBoot 的功能时遇到问题
Having problems trying to test functionality that contains autowired SprngBoot with junit 5
我想测试一些功能,一个带有自动装配的 repo 的服务。我不想模拟自动装配,这更像是一个用于调试的集成测试。
我的测试如下
@SpringBootConfiguration
@ExtendWith(SpringExtension.class)
@SpringBootTest
public class ThrottleRateServiceTest {
ThrottleRateService service = null;
@BeforeEach
public void setUp() {
service = new ThrottleRateServiceImpl();
}
@Test
public void testThrottoling() {
service.isAllowed("test");
}
}
代码很简单
@Service
public class ThrottleRateServiceImpl implements ThrottleRateService {
private final Logger logger = LogManager.getLogger(ThrottleRateServiceImpl.class);
@Autowired
ThrottleRateRepository throttleRateRepository;
问题是 throttleRateRepository 总是空的。
我以前测试过这种代码。使用 Junit 4
@RunWith(SpringJUnit4ClassRunner.class)
它自动装配了所有 bean。
自从我进行这种集成测试以来已经有一段时间了,并且 Junit 5 改变了一切。
谢谢或任何帮助。
该代码中的问题:
@BeforeEach
public void setUp() {
service = new ThrottleRateServiceImpl();
}
您不应该手动创建 bean。在这种情况下 Spring 无法管理它和自动装配存储库。
If you need to recreate your service before each method call, you can use class annotation @DirtiesContext(methodMode = MethodMode.AFTER_METHOD)
. You can read about it more here.
取而代之的是像存储库一样自动装配ThrottleRateServiceImpl
。此外,对于正确的自动装配,您可能需要进行测试配置。它可以是内部静态 class,也可以是单独的 class.
@SpringBootConfiguration
@ExtendWith(SpringExtension.class)
@SpringBootTest
public class ThrottleRateServiceTest {
@TestConfiguration
static class TestConfig {
@Bean
public ThrottleRateService throttleRateService() {
return new ThrottleRateServiceImpl();
}
}
@Autowired
ThrottleRateService service;
@Test
public void testThrottoling() {
service.isAllowed("test");
}
}
您可以在 this tutorial.
中阅读更多有关为测试和测试配置初始化 bean 的信息
另外,很有帮助an official documentation:
If you are using JUnit 4, do not forget to also add
@RunWith(SpringRunner.class) to your test, otherwise the annotations
will be ignored. If you are using JUnit 5, there is no need to add the
equivalent @ExtendWith(SpringExtension.class) as @SpringBootTest and
the other @…Test annotations are already annotated with it.
已修复,我做的是替换
@SpringBootTest 和 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
并自动连接 ThrottleRateService
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ThrottleRateServiceTest {
@Autowired
ThrottleRateService service;
@Test
public void testThrottoling() {
service.isAllowed("test");
}
}
我想测试一些功能,一个带有自动装配的 repo 的服务。我不想模拟自动装配,这更像是一个用于调试的集成测试。
我的测试如下
@SpringBootConfiguration
@ExtendWith(SpringExtension.class)
@SpringBootTest
public class ThrottleRateServiceTest {
ThrottleRateService service = null;
@BeforeEach
public void setUp() {
service = new ThrottleRateServiceImpl();
}
@Test
public void testThrottoling() {
service.isAllowed("test");
}
}
代码很简单
@Service
public class ThrottleRateServiceImpl implements ThrottleRateService {
private final Logger logger = LogManager.getLogger(ThrottleRateServiceImpl.class);
@Autowired
ThrottleRateRepository throttleRateRepository;
问题是 throttleRateRepository 总是空的。
我以前测试过这种代码。使用 Junit 4
@RunWith(SpringJUnit4ClassRunner.class)
它自动装配了所有 bean。 自从我进行这种集成测试以来已经有一段时间了,并且 Junit 5 改变了一切。 谢谢或任何帮助。
该代码中的问题:
@BeforeEach
public void setUp() {
service = new ThrottleRateServiceImpl();
}
您不应该手动创建 bean。在这种情况下 Spring 无法管理它和自动装配存储库。
If you need to recreate your service before each method call, you can use class annotation
@DirtiesContext(methodMode = MethodMode.AFTER_METHOD)
. You can read about it more here.
取而代之的是像存储库一样自动装配ThrottleRateServiceImpl
。此外,对于正确的自动装配,您可能需要进行测试配置。它可以是内部静态 class,也可以是单独的 class.
@SpringBootConfiguration
@ExtendWith(SpringExtension.class)
@SpringBootTest
public class ThrottleRateServiceTest {
@TestConfiguration
static class TestConfig {
@Bean
public ThrottleRateService throttleRateService() {
return new ThrottleRateServiceImpl();
}
}
@Autowired
ThrottleRateService service;
@Test
public void testThrottoling() {
service.isAllowed("test");
}
}
您可以在 this tutorial.
中阅读更多有关为测试和测试配置初始化 bean 的信息另外,很有帮助an official documentation:
If you are using JUnit 4, do not forget to also add @RunWith(SpringRunner.class) to your test, otherwise the annotations will be ignored. If you are using JUnit 5, there is no need to add the equivalent @ExtendWith(SpringExtension.class) as @SpringBootTest and the other @…Test annotations are already annotated with it.
已修复,我做的是替换 @SpringBootTest 和 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
并自动连接 ThrottleRateService
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ThrottleRateServiceTest {
@Autowired
ThrottleRateService service;
@Test
public void testThrottoling() {
service.isAllowed("test");
}
}