1

I'm currently trying to implement a Test where I create a User and then checking the Database if the new User is there. For that, I want to access my service class, but for some Reason the Service is always Null. I tried using Autowired, but that also didn't work.

My Testclass :

public class UserTest {
    
    @Autowired
    private UserdataService userdataService;
 
    @ParameterizedTest
    @MethodSource("Selenium.WebDriverFactory#getAll")
    public void createUser(final WebDriver driver) throws InterruptedException{

        List<UserdataDto> users = getAllUsers();
        UserdataDto user = users.get(users.size() - 1);
        assertEquals(user.getEmail(), "testUser@email.com");

    }

    @ResponseBody
    public List<UserdataDto> getAllUsers() {
        return userdataService.getAllUsers();
    }

}

Error:

java.lang.NullPointerException: Cannot invoke "service.UserdataService.getAllUsers()" because "this.userdataService" is null
    at Selenium.UserTest.getAllUsers(UserTest.java:78)
    at Selenium.UserTest.createAndDeleteUser(UserTest.java:57)
    
Bastian
  • 13
  • 3

1 Answers1

0

You should use the @Autowired tag above private UserdataService userdataService;

Stefan
  • 125
  • 1
  • 7
  • Thanks for the Answer I tried it but it didn't work. I think it has more something to to with how I use my WebDriverFactory. – Bastian Mar 15 '22 at 09:58
  • That can't be, because you do a getAllUsers, that gives a null-pointer, which means that or GetAllUsers() returns null, so there is a problem in the userdataService. Or userdataService is null. You can find out where the problem is if you do a System.out.println(userdataService); if that's null you've found the problem. Otherwise it's because there are no users. – Stefan Mar 15 '22 at 10:06
  • Dit you use @Autowired with capital A – Stefan Mar 15 '22 at 13:48
  • Yes I used Autowired with Capital A. And the userdataService is null, that was the problem I was asking help for I have edited the question to better shwo that. – Bastian Mar 16 '22 at 08:05
  • What annotation do you use above the test class? – Stefan Mar 16 '22 at 08:06
  • Im using nothing above the test class. – Bastian Mar 16 '22 at 08:26
  • You should use `@Test` or `@SpringBootTest` – Stefan Mar 16 '22 at 09:24