Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

springboot + mybatis-native unittest failed #66

Open
cqsuper opened this issue Nov 24, 2022 · 1 comment
Open

springboot + mybatis-native unittest failed #66

cqsuper opened this issue Nov 24, 2022 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@cqsuper
Copy link

cqsuper commented Nov 24, 2022

when run app is ok, but when run unittest will failed:

Caused by: java.io.FileNotFoundException: class path resource 
[file:/Users/xx/.../target/classes/mybatis/mapper/userMapper.xml] cannot be opened because it does not exist
	at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:199)
	at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:609)
	... 164 more

In class MyBatisSpringNativeAutoConfiguration

       resources = holders.stream()
           .flatMap(holder -> holder.getMapperLocations().stream().map(ClassPathResource::new)).toArray(
               Resource[]::new);

Because mapperLocations is full file path, the resource can be FileSystemResource can not be ClassPathResource!
When in the test project ,mapper files are in test resources,so the full file path can remove the baseUrl,but commons mapper files are in src/main/resource path, so in the method: org.mybatis.spring.nativex.MyBatisScannedResourcesHolder.Registrar#toPath, url.startsWith(baseUrl) is false.

Maybe can change code like this, just change code in org.mybatis.spring.nativex.MyBatisScannedResourcesHolder.Registrar#toPath, the url index "target/test-classes" or "target/classes", remove the prefix.

    private static final String TEST_CLASSPATH= "target/test-classes/";
    private static final String CLASSPATH= "target/classes/";
    private String toPath(Resource resource, String baseUrl) {
      try {
        String url = resource.getURL().toString();
        String path = url;
        int indexTestClassPath = url.indexOf(TEST_CLASSPATH);
        int indexClassPath = url.indexOf(CLASSPATH);
        if (url.startsWith(baseUrl)) {
          path = url.replace(baseUrl, "");
        } else if (indexTestClassPath > 0) {
          path = url.substring(indexTestClassPath+TEST_CLASSPATH.length());
        } else if (indexClassPath > 0) {
          path = url.substring(indexClassPath+CLASSPATH.length());
  ...
@kazuki43zoo kazuki43zoo self-assigned this Nov 24, 2022
@kazuki43zoo kazuki43zoo added the bug Something isn't working label Nov 24, 2022
@hazendaz
Copy link
Member

@cqsuper Can you raise PR for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants