【问题标题】:Null pointer exception on @PostConstruct method?@PostConstruct 方法上的空指针异常?
【发布时间】:2016-10-27 07:07:45
【问题描述】:

我正在使用 Spring 用 Ja​​va 开发一个小型应用程序,所以我有这个服务:

public class AccountService implements UserDetailsService {

    @Autowired
    private AccountRepository accountRepository;

    @Autowired
    private BlogRepository blogRepository;

    @Autowired
    private ImageService imageService;

    @PostConstruct
    protected void initialize() throws IOException {
        Account user = new Account("user", "demo", "ROLE_USER");
        save(user);
        Blog userBlog = new Blog("userBlog", true, user);
        userBlog.setAvatar(imageService.createBlogAvatar(userBlog.getName()));
        blogRepository.save(userBlog);
        save(new Account("admin", "admin", "ROLE_ADMIN"));
    }

    // More methods

}

还有这个测试:

@RunWith(MockitoJUnitRunner.class)
public class AccountServiceTest {

    @InjectMocks
    private AccountService accountService = new AccountService();

    @Mock
    private AccountRepository accountRepositoryMock;

    @Test
    public void shouldInitializeWithTwoDemoUsers() throws IOException {
        // act
        accountService.initialize();
        // assert
        verify(accountRepositoryMock, times(2)).save(any(Account.class));
    }

}

为什么我在运行测试时得到这个异常?

shouldInitializeWithTwoDemoUsers(es.udc.fi.dc.fd.account.AccountServiceTest)  Time elapsed: 0.016 sec  <<< ERROR!
java.lang.NullPointerException: null
    at es.udc.fi.dc.fd.account.AccountService.initialize(AccountService.java:45)
    at es.udc.fi.dc.fd.account.AccountServiceTest.shouldInitializeWithTwoDemoUsers(AccountServiceTest.java:42)

使用@PostConstruct注解应该注入所有的bean吧?

【问题讨论】:

  • @Matt1776 如果我删除出现异常的行,我会在此行中得到空指针:blogRepository.save(userBlog);。我测试 userBlog 是否为 null 而不是,所以我现在很困惑...
  • 错误出现在第 45 行,这不是您放在这里的代码的一部分。你能粘贴那部分吗?
  • 嗨@AdityaK,错误来自shouldInitializeWithTwoDemoUsers 方法,这是我在调用AccountService 的initialize() 方法时输入的代码的一部分。
  • 我认为你应该在你的测试类中使用@Mock BlogRepository blogRepository?你试过了吗?
  • 那是错误!我没有模拟我的BlogRepository 存储库,所以现在测试通过了,非常感谢@AdityaK!

标签: java spring


【解决方案1】:

这里的东西很少。首先@InjectMocks 通常使事情变得更容易,但 Mockito 不是依赖注入框架,因此不能保证它可以正常工作。

其次,要使@InjectMocks 正常工作,您还需要@Mock 所有对象,而不是手动创建您尝试注入的类。我不再相信这种情况,但在 mockito 的顺序版本中,@Mocks 的顺序也很重要。

此代码可能对您有用

@RunWith(MockitoJUnitRunner.class)
public class AccountServiceTest {


    @Mock
    private AccountRepository accountRepositoryMock;

    @Mock
    private BlogRepository blogRepository;

    @Mock
    private ImageService imageService;

    @InjectMocks
    private AccountService accountService ;

    @Test
    public void shouldInitializeWithTwoDemoUsers() throws IOException {
        // act
        accountService.initialize();
        // assert
        verify(accountRepositoryMock, times(2)).save(any(Account.class));
    }

}

【讨论】:

    【解决方案2】:

    您需要模拟您的测试对象正在使用的所有依赖项。您可能希望在 AccountServiceTest 类中执行此操作:

    @Mock 
    private BlogRepository blogRepositoryMock;
    

    【讨论】:

      猜你喜欢
      • 2019-10-27
      • 2020-09-11
      • 2018-08-19
      • 2013-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多