实例化CacheManager-NullPointerException

时间:2019-05-07 08:53:29

标签: java unit-testing spring-boot junit nullpointerexception

当前,要测试的类如下所示

@Service
public class ClassToBeTested {

private WebTarget target;

@Autowired
private AnotherClass anotherClass;

@Autowired
public ClassToBeTested(String Uri) {
        target = ClientBuilder.newClient().target("some uri");
}

public String getInfo(String A, String B) {
String someValue = anotherClass.getSomeValue(A, B);
...
}

AnotherClass看起来像下面的那个

import javax.cache.CacheManager;

@Service
public class AnotherClass {

private WebTarget target;

private WebTarget satTarget = null;

private HazelcastServerCacheManager ms;

private HazelcastInstance instance;

@Autowired
public AnotherClass(String Url, CacheManager manager) {
target = ClientBuilder.newClient().target("some url");
this.ms = (HazelcastServerCacheManager) manager;
this.instance = ms.getHazelcastInstance();  
}

public String getSomeValue(String A, String B) {
...

在这里,我正在尝试为ClassToBeTested编写单元测试用例,并且不想模拟AnotherClass。为此,我必须自动连线AnotherClass

@RunWith(SpringRunner.class)
public class TestClass {

@Autowired
private ClassToBeTested classTobeTested;

@Autowired
private AnotherClass anotherClass;

@TestConfiguration
static class ClassToBeTestedTestConfiguration {
  private CacheManager manager;

  @Bean
  public AnotherClass anotherClass(){
  return new anotherClass("some url", manager);

  @Bean
  public ClassToBeTested classTobeTested(){
  return new classTobeTested("some url");
  }
}

@Test
...

在这里,当我编写一个测试用例并运行它时,我得到一个空指针异常。未实例化CacheManager的原因。在这种情况下,我该怎么办?

0 个答案:

没有答案