maven-surefire-plugin:2.22.1测试失败,出现NullPointerException

时间:2019-02-21 01:06:20

标签: spring easymock maven-surefire-plugin

我在使用Maven成功编译测试时遇到问题。我正在使用EasyMock v 4.0.2模拟服务。我应该提到在使用intellij IDE的情况下运行测试没有问题,下面是附带的代码。当我尝试将模拟注入模拟测试时,似乎会出现问题。我已经看到了几篇有关此问题的文章,但没有任何文章可以解决我的问题。

我所看到的最接近的是这个 Spring Boot Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.1:test

public class EasyMockExtension implements TestInstancePostProcessor {
    @Override
    public void postProcessTestInstance(Object testInstance, ExtensionContext extensionContext) throws Exception {
        EasyMockSupport.injectMocks(testInstance);
    }
}

@ExtendWith(EasyMockExtension.class)
public class TestUserService extends EasyMockSupport {

    @TestSubject
    private UserService userService = new UserService();

    @Mock
    private UserRepository userRepository;

    @Mock
    private PasswordEncoder passwordEncoder;

    @Mock
    private MessengerRoleRepository roleRepository;

    @Mock
    private AuthenticationManager authenticationManager;

    @Mock
    private JwtTokenProvider jwtTokenProvider;


    @Test
    public void testAuthenticateUser(){
        String username = "username";
        String password = "password";
        String token = UUID.randomUUID().toString();
        User user = TestUserHelper.createUserWithRoles(username, password);
        Collection<String> roles = user.getRoles().stream().map(MessengerRole::getRoleName).collect(Collectors.toList());
        EasyMock.expect(authenticationManager.authenticate(EasyMock.anyObject(UsernamePasswordAuthenticationToken.class)))
                .andReturn(EasyMock.anyObject(Authentication.class));
        EasyMock.expect(userRepository.findByUsername(username)).andReturn(Optional.of(user));
        EasyMock.expect(jwtTokenProvider.createToken(EasyMock.anyString(), EasyMock.anyObject(Collection.class))).andReturn(token);
        replayAll();
        Map<String, Object> authenticatedUser =  userService.authenticateUser(username, password);
        Assertions.assertTrue(MapUtils.isNotEmpty(authenticatedUser));
        Assertions.assertSame(authenticatedUser.get("token"), token);
        Assertions.assertTrue(((Collection<String>)authenticatedUser.get("roles")).containsAll(roles));
        Assertions.assertTrue(roles.containsAll((Collection<String>)authenticatedUser.get("roles")));
        verifyAll();
    }

    @Test
    public void testAddFriendUsernameSuccess(){
        long userId = 10L;
        String username = "testUser2";
        User user = TestUserHelper.createUser();
        User friend = EasyMock.createMock(User.class);

        Optional<User> optionalUser = Optional.of(user);
        Optional<User>  optionalFriend = Optional.of(friend);

        EasyMock.expect(userRepository.findById(userId)).andReturn(optionalUser);
        EasyMock.expect(userRepository.findByUsername(username)).andReturn(optionalFriend);
        EasyMock.replay(userRepository, friend);

        User returnedUser = userService.addFriend(userId, username);
        Assertions.assertNotNull(returnedUser);
        Assertions.assertSame(returnedUser, user);
        Assertions.assertTrue(returnedUser.getFriends().contains(friend));
        EasyMock.verify(userRepository, friend);
    }
}

这就是我从中得到的。我会对此有所帮助,因为我不确定为什么surefire无法注入我的模拟物

java.lang.NullPointerException
    at com.messenger.service.TestUserService.testAuthenticateUser(TestUserService.java)

[ERROR] com.messenger.service.TestUserService.testAddFriendUserNotFound()  Time elapsed: 0 s  <<< FAILURE!
java.lang.NullPointerException
    at com.messenger.service.TestUserService.testAddFriendUserNotFound(TestUserService.java)

[ERROR] com.messenger.service.TestUserService.testAddFriendUsernameNotFound()  Time elapsed: 0 s  <<< FAILURE!
java.lang.NullPointerException
    at com.messenger.service.TestUserService.testAddFriendUsernameNotFound(TestUserService.java)

[ERROR] com.messenger.service.TestUserService.testAddFriendUsernameSuccess()  Time elapsed: 0.191 s  <<< FAILURE!
java.lang.NullPointerException
    at com.messenger.service.TestUserService.testAddFriendUsernameSuccess(TestUserService.java)

[ERROR] com.messenger.service.TestUserService.testAuthenticateUserUnableToFindUser()  Time elapsed: 0.001 s  <<< FAILURE!
java.lang.NullPointerException

1 个答案:

答案 0 :(得分:0)

如果模拟为空,则表示它们未正确注入。

EasyMockExtension来自哪里?它不是来自EasyMock本身,因为还不支持JUnit 5。但很乐意接受捐助。