#Java testing with scanner input

1 messages · Page 1 of 1 (latest)

glad hollow
#

I have a createUser() method which i am trying to write a test for. It starts like this

        Scanner input = new Scanner(System.in);
        System.out.println("—Welcome to the Task Management System—");
        if (currentUser == null) {
            System.out.println("If you are an existing user press 1 to login, if you are a new user press 2 to register. By typing -1 when entered into any menu you can go back to main menu");
            String choice = input.nextLine();
            if (choice.equals("2")) {```

and i am trying to pass some inputs as if they are user inputs with mockito like this 
```public void testUserRegistration() throws SQLException {

        // Create a mock DatabaseService
        DatabaseService dbService = mock(DatabaseService.class);
        // Create a new Service with the mocked DatabaseService
        Service service = new Service(dbService,true);

        // Create a mock Scanner with predefined user inputs
        Scanner scanner = mock(Scanner.class);
        Person registeredUser = service.createUser(scanner);

        when(scanner.next()).thenReturn("2");
        when(scanner.nextLine()).thenReturn("testUser");
        when(scanner.nextLine()).thenReturn("testPassword");


        // Test the registration process
        // Verify that the createUser() method returns the expected output
        assertEquals("testUser", registeredUser.getName());
    }```
But still i think there is a problem cause test is not failing or anything. Can anyone help?
subtle reefBOT
# glad hollow I have a createUser() method which i am trying to write a test for. It starts li...

Detected code, here are some useful tools:

Formatted code
public Person createUser() {
  Scanner input = new Scanner(System.in);
  System.out.println("—Welcome to the Task Management System—");
  if (currentUser == null ) {
    System.out.println("If you are an existing user press 1 to login, if you are a new user press 2 to register. By typing -1 when entered into any menu you can go back to main menu");
    String choice = input.nextLine();
    if (choice.equals("2")) {
#

<@&987246399047479336> please have a look, thanks.

subtle reefBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

bronze sphinx
#

u really shouldn't go about it like that

#

instead, the method should take as input an InputStream and then it can create the scanner on that

#

and i ur test code u pass a StringReader instead

#

that way u can literally give the method essentially a plain string in ur tests

#

instead of having to mock anything

#

that said, it's really hard to understand what ur question is, since u said it works. so what did u expect, why did u expect it to fail etc

glad hollow
#

i think i fixed it like this String userInput = "2\nJohn\npassword"; InputStream in = new ByteArrayInputStream(userInput.getBytes()); System.setIn(in);

subtle reefBOT
glad hollow
#

i didnt know about InputStream

bronze sphinx
#

ur welcome. but u really shouldn't manipulate System. in itself. the method should take an InputStream as parameter instead. and ur getBytes() is technically a bug that can cause issues with encoding

#

just use a StringReader instead

glad hollow
#

How should i change it 🤔

#
            Reader in = new StringReader(userInput);
            InputStream inputStream = new InputStream() {
                @Override
                public int read() throws IOException, IOException {
                    return in.read();
                }
            };
            System.setIn(inputStream);``` this came up to my mind
subtle reefBOT
glad hollow
#

Does this solve for cause issues with encoding and is it better this way?

bronze sphinx
#

nah use:

#
InputStream stream = new ByteArrayInputStream(exampleString.getBytes(StandardCharsets.UTF_8));
#

or let the method take a scanner instead

#

scanner accepts strings directly

#

so in real code u give it new Scanner(System.in)

#

and in test code new Scanner("foo")

#

that's probably the easiest approach

glad hollow
#

ok thanks man