2016-10-25 19 views
0

私は春のブートWebアプリケーションを構築するためにspring boot v1.4.1.RELEASE + mybatis-spring-boot-starter v1.1.1 + h2database v1.4.192 + flywaydb v4.0.3を使用しています。ここh2データベースのMybatisはデータを挿入できません

は私のYMLの設定一部のファイルやデータソースの設定です:

datasource: km: 

driverClassName: org.h2.Driver

url: jdbc:h2:mem:km;MODE=MySQL;

そして、私は私のユニットテストを実行するためにMySQLを使用していたときに、すべてのユニットテストを合格しました。しかし、h2databaseに切り替えると、同じテストがパスされませんでした。

UTコードは以下の通りです:

@RunWith(SpringJUnit4ClassRunner.class) 
@SpringBootTest(classes = App.class) 
@ActiveProfiles("unit-test") 
@Transactional 
@Rollback 
public class FollowServiceTest { 
    private int HOST_ID = 1; 
    private int UID = 100; 

    @Autowired 
    private FollowService followService; 

    @Autowired 
    private UserFollowDAO followDAO; 

    @Test 
    public void testFans() throws Exception { 

     UserFollow userFollow = new UserFollow(); 
     userFollow.setHostId(HOST_ID); 
     userFollow.setFollowerId(UID); 
     followDAO.insert(userFollow); 

     List<UserFollow> fans = followDAO.findByIndexHostId(HOST_ID, 0, 10); 

     assertThat("fans is empty", fans, not(empty())); 
    } 
} 

UserFollowDAO:

public interface UserFollowDAO { 
    public static final String COL_ALL = " id, host_id, follower_id, create_time, last_update_time "; 
    public static final String TABLE = " user_follow "; 


    @Select(" select " + 
      COL_ALL + 
      " from " + 
      TABLE + 
      " where " + 
      "`host_id` = #{hostId} " + 
      " and id > #{lastId} " + 
      " limit #{count} " 
    ) 
    public List<UserFollow> findByIndexHostId(
      @Param("hostId") int hostId, 
      @Param("lastId") int lastId, 
      @Param("count") int count 
    ); 

    @Insert(" insert into " + TABLE + " set " 
     + " id = #{id}, " 
     + " host_id = #{hostId}, " 
     + " follower_id = #{followerId}, " 
     + " create_time = now(), " 
     + " last_update_time = now()") 
    public int insert(UserFollow bean); 
} 

エラー情報:

java.lang.AssertionError: fans is empty 
Expected: not an empty collection 
    but: was <[]> 

    at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20) 
    at org.junit.Assert.assertThat(Assert.java:956) 
    at com.hi.hk.api.service.FollowServiceTest.testFans(FollowServiceTest.java:52) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 

ここでは、私が試してみる私のソリューションです:

  1. ymlファイルのデータソース設定をurl: jdbc:h2:mem:km;MODE=MySQLからurl: jdbc:h2:mem:km;MODE=MySQL;LOCK_MODE=1に変更します。うまくいかない。
  2. 削除@Transactional@Rollback utコードからの注釈。うまくいかない。

この問題を解決するには、次に何ができますか?

@pau:ここ

は私application-unit-test.yml設定ファイルの一部です:

datasource: ut: true km: 
 driverClassName: org.h2.Driver
 url: jdbc:h2:mem:km;MODE=MySQL

そして、あなたが言ったように、私は、変更データソースの設定があります。ここでは

@Value("${datasource.ut}") 
private boolean isUt; 

public static final String SQL_SESSION_FACTORY_NAME = "sessionFactoryKm"; 
public static final String TX_MANAGER = "txManagerKm"; 
private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); 

@Bean(name = "datasourceKm") 
@Primary 
@ConfigurationProperties(prefix = "datasource.km") 
public DataSource dataSourceKm() { 
    if (isUt){ 
     EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder(); 
     return builder.setType(EmbeddedDatabaseType.H2).build(); 
     // Because I am use migration db to execute init sql script, so I haven't set script. 
    } 
    return DataSourceBuilder.create().build(); 
} 

があるflyway豆:

mycodeでいくつかのバグがあり

答えて

0

:Bean内のIDの値を使用します

@Insert(" insert into " + TABLE + " set " 
     + " id = #{id}, "// this line 
     + " host_id = #{hostId}, " 
     + " follower_id = #{followerId}, " 
     + " create_time = now(), " 
     + " last_update_time = now()") 
    public int insert(UserFollow bean); 

この挿入方法は、それはとても0ですが、私は右の値を取得することはできません。この行を削除すると、この単体テストが渡されます。

関連する問題