繁体   English   中英

Quarkus - 如何测试 onFailure() 的反应流

[英]Quarkus - How to test reactive flow for onFailure()

我在使用 Mongo Reactive Panache 的地方有反应性 quarkus 资源。 我有一个逻辑,如果 person 的 emailId 存在,那么代码应该抛出 BusinessException。

我能够测试快乐的路径,但我如何触发负面测试。 这是我的代码和测试 class。

PersonResource.java

@POST
    public Uni<Response> create(Person person){
        return repository.persist(person). map(r ->
                Response.ok(r).build())
                .onFailure()
                .recoverWithItem(f-> {
                    AStatus status =  createErrorStatus(f.getMessage());
                    return Response.serverError().entity(status).build();
                }) ;
    }

PersonRepositoryTest.java

package com.eventu.resource;

import com.eventu.exception.BusinessException;
import com.eventu.repository.PersonRepository;
import com.eventu.vo.Address;
import com.eventu.vo.Person;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.mockito.InjectMock;
import io.smallrye.mutiny.Uni;
import io.smallrye.mutiny.helpers.test.UniAssertSubscriber;
import org.bson.types.ObjectId;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import javax.inject.Inject;
import javax.ws.rs.core.Response;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;


@QuarkusTest
class PersonResourceTest {

     @InjectMock
    PersonRepository repository;
    @Inject
    PersonResource resource;
    Uni<Person> uniPerson;
    Person person;
    @BeforeEach
    public  void setUp(){
        person =  Person.builder().address(Address.builder()
                        .addressLine_1("1000 Test dr")
                        .addressLine_2("opposite crescent dr").suiteNumber("Ste. 200").city("testCity")
                        .countryCode("US").state("CA")
                        .build()).emailAddress("test@test.com").firstName("John").lastName("Barista")
                .mobileNumber("70444444444")

                .id(new ObjectId())
                .build();
       uniPerson = Uni.createFrom().item(person);

        when(repository.persist(any(Person.class))).thenReturn(uniPerson);
        
    }
   // THIS TEST IS WORKING 
    @Test
    @DisplayName("When a new person object is passed then person object with Id should be returned")
    void create_success() {
        final Uni<Response> p = resource.create(person);
        Person respPerson = (Person) p.subscribe().withSubscriber(UniAssertSubscriber.create()).assertCompleted().getItem().getEntity();
        Assertions.assertNotNull(p);
        Assertions.assertEquals(person, respPerson);
    }

   

    /**
     * THIS ONE FAILS WITH at line 63 with the message
     * org.mockito.exceptions.base.MockitoException: 
     * Checked exception is invalid for this method!
     * Invalid: com.eventu.exception.BusinessException
     */
    @Test
    @DisplayName("When a new person object is passed but email exists then AStatus object with error description")
    void create_duplicate_emailId() {
        when(repository.persist(any(Person.class))).thenThrow(BusinessException.class); /// **FAILS HERE. Tried replacing with RunTimeException.class as well** 
        final Uni<Response> p = resource.create(person);
        Person respPerson = (Person) p.subscribe().withSubscriber(UniAssertSubscriber.create()).assertCompleted().getItem().getEntity();
        //**SHOULD FAIL HERE as I EXPECT AStatus.java object**
        Assertions.assertEquals(person, respPerson);
    }


}

我在尝试错误后找到了解决方案。 关于 Mutiny/Quarkus 的文档还有很多不足之处。 这是需要的-

@Test
    @DisplayName("When a new person object is passed but email exists then AStatus object with error description")
    void create_duplicate_emailId() {
        when(repository.persist(any(Person.class)))
.thenReturn(Uni.createFrom().failure(new MongoException(11000, "Email does not exist")));
        final Uni<Response> p = resource.create(person);
        Person respPerson = (Person) p.subscribe().withSubscriber(UniAssertSubscriber.create()).assertCompleted().getItem().getEntity();
        
       .....
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM