newline after Mockito verify call fails the test

hi, im trying to verify a logger method call with this code line:
verify(loggerSpy).debug("some text {}", someArg);
verify(loggerSpy).debug("some text {}", someArg);
when i execute the test it fails due to a "Comparison" error comparing the expected
log4jLogger.debug(
"some text {}",
someArg
);
log4jLogger.debug(
"some text {}",
someArg
);
to the actual:
log4jLogger.debug(
"some text {}",
someArg
);
\n
log4jLogger.debug(
"some text {}",
someArg
);
\n
shows the difference is after the codeline has ended (notice the newline after the "Actual" value)(i put a \n to help visualize it since three backtick syntax would remove trailing newlines) How can i include that newline into the "expected"? Or tell Mockito to ignore the trailing newline?
10 Replies
JavaBot
JavaBot14mo ago
This post has been reserved for your question.
Hey @SterniXPutzig! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Kyo-chan
Kyo-chan14mo ago
That's probably not what's happening.
SterniXPutzig
SterniXPutzigOP14mo ago
when i click on <Click to see difference> (difference between expected and actual) it shows me this (the only difference is the newline at the end of the actual value)
No description
Kyo-chan
Kyo-chan14mo ago
Erm. Kinda make me want to ask see difference between what and and what So you're testing that some data sent is some Java code of logging something? Your original example says that you're testing for some text {}
SterniXPutzig
SterniXPutzigOP14mo ago
the screenshot just shows how mockito displays the difference between the 2 in the code i do this:
void verifyData(data) {
/*parse args*/
Stringbuilder someArg;
/*build String with data values*/

//this next line is where mockito puts a newline character after the log call
logger.debug("some text {}", someArg);// <--- this is what im trying to test for (to see if the args are correctly collected)
/*error handling if necessary*/
}
void verifyData(data) {
/*parse args*/
Stringbuilder someArg;
/*build String with data values*/

//this next line is where mockito puts a newline character after the log call
logger.debug("some text {}", someArg);// <--- this is what im trying to test for (to see if the args are correctly collected)
/*error handling if necessary*/
}
and in the test Method i have this configuration:
@Test
void testVerifyData() {
/*prepare data and expected*/
verifyData(data);
//loggerSpy is created in @beforeAll and reset in @BeforeEach
verify(loggerSpy).debug("some text {}", expected);//this is where fails
}
@Test
void testVerifyData() {
/*prepare data and expected*/
verifyData(data);
//loggerSpy is created in @beforeAll and reset in @BeforeEach
verify(loggerSpy).debug("some text {}", expected);//this is where fails
}
basically the arguments are identical but mockito adds a newline after the code line when it is being logged by mockito
Kyo-chan
Kyo-chan14mo ago
Mockito doesn't log things There is a newline when you do the real code. If you don't expect it, then either you should expect it, or your actual code adds a newline that you want that it doesn't add What may happen and not be made obvious by the comparison though, is that the new line is not in expected format. I'd encourage you to run debug and to check the exact content of the String
SterniXPutzig
SterniXPutzigOP14mo ago
My Problem is How do I change this line:
verify(loggerSpy).debug("some text {}", someArg);
verify(loggerSpy).debug("some text {}", someArg);
To expect a newline after the semicolon? Because the arguments themselves are correct
Kyo-chan
Kyo-chan14mo ago
No, you need someArg to contain the newline too
SterniXPutzig
SterniXPutzigOP14mo ago
nope then it has 2 mismatched positions: -after someArg because the newline is expected but not present -after the semicolon because no newline is expected but is present im creating a minimal working example atm
JavaBot
JavaBot14mo ago
Post Closed
This post has been closed by <@378591387341291520>.

Did you find this page helpful?