Quarkus - 如何测试 onFailure() 的反应流
Quarkus - How to test reactive flow for onFailure()
我有反应式 quarkus 资源,我正在使用 Mongo 反应式 Panache。我有一个逻辑,如果 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();
.....
}
我有反应式 quarkus 资源,我正在使用 Mongo 反应式 Panache。我有一个逻辑,如果 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();
.....
}