1

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.

1 Answer 1

1

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
2
  • 1
    That works. But why mvn does not read pom.xml since the three properties are in utf-8 now. And why the pom.xml utf-8 configuration does not work? Are they used for different purpose, why pom.xml utf-8 configuration does not work?
    – Tiina
    Commented Mar 24, 2017 at 3:29
  • 1
    Yes those POM properties are different from file.encoding. Just as extra information that may help you - there is a way to make mvn test do what you need, refer to this example (the <systemProperties> part): github.com/intuit/karate#test-reports Commented Mar 24, 2017 at 4:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.