Tiina Tiina -4 years ago 103
Java Question

maven test fails at a eastern language character while IDEA success

This is the test class:

@Sql(value = {"classpath:/ut_user_init.sql"})
public class UserControllerTest extends MockMvcTest {
@WithUserDetails(value = MockData.ADMIN_USERNAME, userDetailsServiceBeanName = "mockUserDetailsService")
public void testGetUser() throws Exception {}

And the

public String list(Model model) {
userService.findByName("[An Eastern character]")

And in

User findByName(String name) {
return userRepository.findByName(name); // name is in Eastern language.

This is related data in

INSERT INTO `user` VALUES ('32', '[The Eastern character]');

This is effective


Now the odd thing is: if I use IDEA the Service class returns a finding result, but if I use
mvn test -Dtest=path.to.TestClass
, it would fail because the return is null. But if I print the result of
I can see that it prints a correct number of records listed in sql file. And I also tried printing result of
, in both MinGW (windows uses GDB for Eastern languages while the source files are in UTF-8) and Linux prints random code for the
. While IDEA does not have this problem, it returns the finding and it prints the Eastern Character as it is. Why mvn and IDEA behaves differently, and what caused mvn fail, and how to solve this? Thanks.

Answer Source

I am guessing that when you run from the command line it is taking your system default encoding, which may not be UTF-8.

Try this:

mvn test -DargLine="-Dfile.encoding=UTF-8" -Dtest=MyTestClassName
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download