mongodb的spring数据没有抛出duplicatekeyexception

ecfsfe2w  于 2021-07-16  发布在  Java
关注(0)|答案(1)|浏览(443)

我尝试在JUnit5测试中抛出duplicatekeyexception。单元测试使用相同的productid将两个对象保存到存储库中。productid在productentity类中声明为@index(unique=true)。我的期望是得到一个例外。
这是测试类:

package com.exercim.microservices.core.product;

import static org.junit.jupiter.api.Assertions.assertThrows;

import com.exercim.microservices.core.product.persistence.ProductEntity;
import com.exercim.microservices.core.product.persistence.ProductRepository;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest;
import org.springframework.dao.DuplicateKeyException;

@DataMongoTest
public class ProductRepositoryTest {

    @Autowired
    ProductRepository repository ;

    @Test
    public void testDuplicateKeyException() {
        ProductEntity e1 = new ProductEntity( 1, "name", 1 ) ;
        ProductEntity e2 = new ProductEntity( 1, "name", 1 ) ;

        repository.deleteAll() ;
        repository.save( e1 ) ;
        assertThrows(DuplicateKeyException.class, () -> repository.save( e2 ) ) ;
    }
}

这是Productentity类:

package com.exercim.microservices.core.product.persistence;

import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Version;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;

@Document( collection = "products" )
public class ProductEntity {

    @Id
    private String id ;

    @Version
    private Integer version ;

    @Indexed( unique = true )
    private int productId ;

    private String name ;
    private Integer weight ;

    public ProductEntity() {
    }

    public ProductEntity( int productId, String name, int weight) {
        this.productId = productId;
        this.name = name;
        this.weight = weight;
    }

    public String getId() {
        return this.id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public int getVersion() {
        return this.version;
    }

    public void setVersion(int version) {
        this.version = version;
    }

    public int getProductId() {
        return this.productId;
    }

    public void setProductId(int productId) {
        this.productId = productId;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getWeight() {
        return this.weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }
}

存储库是一个扩展 PagingAndSortingRepository 这是我生活的一部分 build.gradle file :

implementation group: 'org.springframework.boot', name: 'spring-boot-starter-data-mongodb'
   testImplementation 'de.flapdoodle.embed:de.flapdoodle.embed.mongo'

测试失败,并显示以下消息:

org.opentest4j.AssertionFailedError: Expected org.springframework.dao.DuplicateKeyException to be thrown, but nothing was thrown.
    at org.junit.jupiter.api.AssertThrows.assertThrows(AssertThrows.java:71)

有人有主意吗?提前谢谢!

tzxcd3kk

tzxcd3kk1#

谢谢乔!
问题是,没有启用自动索引创建。这是我的原创 application.yml 文件:

spring.data.mongodb:
    host: localhost
    port: 27017
    database: product-db

我添加了以下行:

auto-index-creation: true

解决了问题。

相关问题