I am having this test, junit
assestion works well, while the same assertj
assertion does not.
<code>@Test
void registerWithDuplicatedEmail() throws EmailAlreadyRegistered {
Mockito.when(customerCommand.createCustomer("Duplicated", "[email protected]", "pass"))
.thenThrow(new EmailAlreadyRegistered());
assertThrows(EmailAlreadyRegistered.class, () -> registerCustomerUseCase.execute("Duplicated", "[email protected]", "pass"));
// (1) this works well
assertThatCode(() -> registerCustomerUseCase.execute("Duplicated", "[email protected]", "pass")).hasCause(new EmailAlreadyRegistered());
// (2) This fails with the error:
// Expecting a cause with type: "...exception.EmailAlreadyRegistered" and message: null
// but actualCause had no cause.
assertThatCode(() -> registerCustomerUseCase.execute("Duplicated", "[email protected]", "pass")).doesNotThrowAnyException();
// (3) Although it contradicts the previous one, this also fails with this error:
// Expecting code not to raise a throwable but caught "...exception.EmailAlreadyRegistered"
}
</code>
<code>@Test
void registerWithDuplicatedEmail() throws EmailAlreadyRegistered {
Mockito.when(customerCommand.createCustomer("Duplicated", "[email protected]", "pass"))
.thenThrow(new EmailAlreadyRegistered());
assertThrows(EmailAlreadyRegistered.class, () -> registerCustomerUseCase.execute("Duplicated", "[email protected]", "pass"));
// (1) this works well
assertThatCode(() -> registerCustomerUseCase.execute("Duplicated", "[email protected]", "pass")).hasCause(new EmailAlreadyRegistered());
// (2) This fails with the error:
// Expecting a cause with type: "...exception.EmailAlreadyRegistered" and message: null
// but actualCause had no cause.
assertThatCode(() -> registerCustomerUseCase.execute("Duplicated", "[email protected]", "pass")).doesNotThrowAnyException();
// (3) Although it contradicts the previous one, this also fails with this error:
// Expecting code not to raise a throwable but caught "...exception.EmailAlreadyRegistered"
}
</code>
@Test
void registerWithDuplicatedEmail() throws EmailAlreadyRegistered {
Mockito.when(customerCommand.createCustomer("Duplicated", "[email protected]", "pass"))
.thenThrow(new EmailAlreadyRegistered());
assertThrows(EmailAlreadyRegistered.class, () -> registerCustomerUseCase.execute("Duplicated", "[email protected]", "pass"));
// (1) this works well
assertThatCode(() -> registerCustomerUseCase.execute("Duplicated", "[email protected]", "pass")).hasCause(new EmailAlreadyRegistered());
// (2) This fails with the error:
// Expecting a cause with type: "...exception.EmailAlreadyRegistered" and message: null
// but actualCause had no cause.
assertThatCode(() -> registerCustomerUseCase.execute("Duplicated", "[email protected]", "pass")).doesNotThrowAnyException();
// (3) Although it contradicts the previous one, this also fails with this error:
// Expecting code not to raise a throwable but caught "...exception.EmailAlreadyRegistered"
}
RegisterCustomerUseCase is pretty simple:
<code>@Component
@RequiredArgsConstructor
public class RegisterCustomerUseCase {
private final CustomerCommand customerCommand;
public Customer execute(String name, String email, String password) throws EmailAlreadyRegistered {
return customerCommand.createCustomer(name, email, password);
}
}
</code>
<code>@Component
@RequiredArgsConstructor
public class RegisterCustomerUseCase {
private final CustomerCommand customerCommand;
public Customer execute(String name, String email, String password) throws EmailAlreadyRegistered {
return customerCommand.createCustomer(name, email, password);
}
}
</code>
@Component
@RequiredArgsConstructor
public class RegisterCustomerUseCase {
private final CustomerCommand customerCommand;
public Customer execute(String name, String email, String password) throws EmailAlreadyRegistered {
return customerCommand.createCustomer(name, email, password);
}
}