Skip to content

Commit

Permalink
完善测试代码
Browse files Browse the repository at this point in the history
  • Loading branch information
abel533 committed Dec 15, 2023
1 parent 17e6eef commit b014ae5
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 2 deletions.
6 changes: 6 additions & 0 deletions jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@
</dependency>

<!-- test -->
<dependency>
<groupId>io.mybatis</groupId>
<artifactId>mybatis-mapper</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
Expand Down
1 change: 0 additions & 1 deletion jpa/src/test/java/io/mybatis/provider/jpa/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
@Table(name = "user")
public class User {
@Id
@Column
private Long id;
@Column(name = "name")
private String username;
Expand Down
25 changes: 25 additions & 0 deletions jpa/src/test/java/io/mybatis/provider/jpa/UserBaseMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2020-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.mybatis.provider.jpa;

import io.mybatis.mapper.BaseMapper;

import java.io.Serializable;

public interface UserBaseMapper extends BaseMapper<User, Serializable> {

}
47 changes: 47 additions & 0 deletions jpa/src/test/java/io/mybatis/provider/jpa/UserBaseMapperTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2020-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.mybatis.provider.jpa;

import io.mybatis.provider.BaseTest;
import org.apache.ibatis.session.SqlSession;
import org.junit.Assert;
import org.junit.Test;

public class UserBaseMapperTest extends BaseTest {

@Test
public void testSelectById() {
try (SqlSession sqlSession = getSqlSession()) {
UserBaseMapper userMapper = sqlSession.getMapper(UserBaseMapper.class);

User user = userMapper.selectByPrimaryKey(1L).get();
Assert.assertNotNull(user);
Assert.assertEquals("张无忌", user.getUsername());
Assert.assertNull(user.getSex());

user.setId(999L);
Assert.assertEquals(1, userMapper.insert(user));

int count = userMapper.deleteByPrimaryKey(user.getId());
Assert.assertEquals(1, count);
count = userMapper.deleteByPrimaryKey(user.getId());
Assert.assertEquals(0, count);
sqlSession.rollback();
}
}

}
5 changes: 5 additions & 0 deletions jpa/src/test/java/io/mybatis/provider/jpa/UserMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.mybatis.provider.jpa;

import io.mybatis.provider.Caching;
import org.apache.ibatis.annotations.DeleteProvider;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Lang;
import org.apache.ibatis.annotations.SelectProvider;
Expand All @@ -30,4 +31,8 @@ public interface UserMapper {
@Lang(Caching.class)
@InsertProvider(type = BaseProvider.class, method = "insertSelective")
int insert(User user);

@Lang(Caching.class)
@DeleteProvider(type = BaseProvider.class, method = "deleteById")
int deleteById(User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ public void testSelectById() {
Assert.assertNull(user.getSex());

user.setId(999L);
userMapper.insert(user);
Assert.assertEquals(1, userMapper.insert(user));

int count = userMapper.deleteById(user);
Assert.assertEquals(1, count);
sqlSession.rollback();
}
}
Expand Down

0 comments on commit b014ae5

Please sign in to comment.