【发布时间】:2021-03-29 22:46:32
【问题描述】:
我正在使用嵌套类在 Spring Boot 中编写单元测试用例。我的测试用例文件是:
@RunWith(NestedRunner.class)
@SpringBootTest
@TestInstance
@AutoConfigureMockMvc
public class OuterTestCase {
public static class InnerTestCases {
@Autowired
private RestTemplate restTemplate;
@Autowired
private MockMvc mockMvc;
@Test
public void testcase(){
//do Something
}
}
}
但是当我尝试在 testCase 方法中使用 RestTemplate 时,我得到了 NPE。
我无法在内部类中初始化 bean。
【问题讨论】:
-
这能回答你的问题吗? @Autowired not working in inner class
-
基本上必须将内部静态类注释为某种 bean - 也许是@Component。否则你必须自己管理依赖。
-
添加组件 bean 也不起作用。 @RandyCasburn 我也检查了上面的链接,但这也没有回答我的问题。
标签: java spring-boot junit junit-runner