Tiina Tiina -4 years ago 103
Java Question

maven test fails at a eastern language character while IDEA success

This is the test class:

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


And the
UserController
class´╝Ü

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


And in
UserService
class:

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


This is related data in
ut_user_init.sql


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


This is effective
pom.xml
:

<maven.compiler.encoding>utf-8</maven.compiler.encoding>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>


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
userRepository.findAll().size()
I can see that it prints a correct number of records listed in sql file. And I also tried printing result of
userRepository.findOne(32L)
, in both MinGW (windows uses GDB for Eastern languages while the source files are in UTF-8) and Linux prints random code for the
name
. 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