簡體   English   中英

Quarkus Panache Mockito 失敗

[英]Quarkus Panache Mockito fails

我與 mocking 一個 Panache 存儲庫作斗爭。 這是實體:

    import javax.persistence.*;
    
    @Entity
    public class Thing {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "id", nullable = false)
        private Long id;
    
        public Long getId() {
            return id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    }

簡單的存儲庫:

import io.quarkus.hibernate.orm.panache.PanacheRepository;

import javax.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class ThingRepository implements PanacheRepository<Thing> {
}

這是資源:

import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.List;

@Path("/things")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class ThingResource {

    @Inject
    ThingRepository thingRepository;

    @GET
    public List<Thing> list() {
        return thingRepository.listAll();
    }

}

和一個我嘗試模擬存儲庫的簡單測試:

import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.mockito.InjectMock;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;

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

@QuarkusTest
class ThingResourceTest {

    private Thing thing;

    @Inject ThingResource thingResource;
    @InjectMock ThingRepository thingRepository;

    @BeforeEach
    void setUp() {
        Thing thing = new Thing();
        thing.setId(1L);
    }

    @Test
    void getAll() {
        List<Thing> things = new ArrayList<Thing>();
        things.add(thing);
        Mockito.when(thingRepository.listAll()).thenReturn(things);
        List<Thing> response = thingResource.list();
        assertNotNull(response);
        assertNotNull(response.get(0));
    }
}

測試失敗,因為響應列表是<null>

調試器告訴我thingRepository實際上是模擬的。 但是由於某種原因Mockito.when().thenReturns()沒有返回我設置的列表。

在此處輸入圖像描述

我錯過了什么? 感謝您的任何幫助。

我有雙重聲明的thing 一次作為 class 變量,另一次在setUp()中。 無賴。 我為噪音道歉。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM