【发布时间】:2020-04-06 14:52:31
【问题描述】:
我无权访问MyClass2 代码,也无法更改它。我如何模拟/实例化MyClass2 myClass2?
类和代码测试:
@RunWith(JUnit4.class)//can't change thisone
public class MyTest
{
@Autowired // or not, tried both ways
MyClass testedInstance= new MyClass();
@Test
public void boot() throws Exception{
testedInstance.boot();
assertTrue(true);
}
}
public class MyClass
{
@Autowired
private MyClass2 myClass2;
void boot()
{
myClass2.foo();//getting a null pointer here
}
}
【问题讨论】:
-
不,我的@autowired 变量没有在测试类中正确设置,嵌套的变量仍然为空
-
检查这个工作解决方案:stackoverflow.com/a/71591567/5108695
标签: java spring spring-boot junit autowired