test: transaction test
This commit is contained in:
parent
8e7ec76774
commit
49030da9ca
9
src/main/java/io/spring/MyBatisConfig.java
Normal file
9
src/main/java/io/spring/MyBatisConfig.java
Normal file
@ -0,0 +1,9 @@
|
||||
package io.spring;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
@Configuration
|
||||
@EnableTransactionManagement
|
||||
public class MyBatisConfig {
|
||||
}
|
@ -5,6 +5,7 @@ import io.spring.core.article.ArticleRepository;
|
||||
import io.spring.core.article.Tag;
|
||||
import io.spring.infrastructure.mybatis.mapper.ArticleMapper;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@ -17,6 +18,7 @@ public class MyBatisArticleRepository implements ArticleRepository {
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void save(Article article) {
|
||||
if (articleMapper.findById(article.getId()) == null) {
|
||||
createNew(article);
|
||||
@ -26,13 +28,13 @@ public class MyBatisArticleRepository implements ArticleRepository {
|
||||
}
|
||||
|
||||
private void createNew(Article article) {
|
||||
articleMapper.insert(article);
|
||||
for (Tag tag : article.getTags()) {
|
||||
if (!articleMapper.findTag(tag.getName())) {
|
||||
articleMapper.insertTag(tag);
|
||||
}
|
||||
articleMapper.insertArticleTagRelation(article.getId(), tag.getId());
|
||||
}
|
||||
articleMapper.insert(article);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -0,0 +1,46 @@
|
||||
package io.spring.infrastructure.article;
|
||||
|
||||
import io.spring.core.article.Article;
|
||||
import io.spring.core.article.ArticleRepository;
|
||||
import io.spring.core.user.User;
|
||||
import io.spring.core.user.UserRepository;
|
||||
import io.spring.infrastructure.mybatis.mapper.ArticleMapper;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
@AutoConfigureTestDatabase
|
||||
public class ArticleRepositoryTransactionTest {
|
||||
@Autowired
|
||||
private ArticleRepository articleRepository;
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Autowired
|
||||
private ArticleMapper articleMapper;
|
||||
|
||||
|
||||
@Test
|
||||
public void transactional_test() throws Exception {
|
||||
User user = new User("aisensiy@gmail.com", "aisensiy", "123", "bio", "default");
|
||||
userRepository.save(user);
|
||||
Article article = new Article("test", "desc", "body", new String[]{"java", "spring"}, user.getId());
|
||||
articleRepository.save(article);
|
||||
Article anotherArticle = new Article("test", "desc", "body", new String[]{"java", "spring", "other"}, user.getId());
|
||||
try {
|
||||
articleRepository.save(anotherArticle);
|
||||
} catch (Exception e) {
|
||||
assertThat(articleMapper.findTag("other"), is(false));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -30,6 +30,7 @@ public class MyBatisArticleRepositoryTest {
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
private User user;
|
||||
private Article article;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user