Spring Mongo 数据库的数据不会抛出 DuplicateKeyException

Spring Data for Mongo DB does not throw DuplicateKeyException

我尝试在 jUnit 5 测试中抛出 DuplicateKeyException。单元测试将两个对象保存到具有相同 productId 的存储库。 productID 在 ProductEntity class 中声明为 @Indexed(unique = true)。我的期望是收到 DuplicateKeyException。

这是测试class:

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 ) ) ;
    }
}

这是 ProductEntitiy class:

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)

有人有想法吗?提前致谢!

谢谢乔!

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

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

我添加了以下行:

    auto-index-creation: true

问题解决了。