forked from rbmonster/learning-note
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBDDMockitoTestDemo.java
More file actions
38 lines (32 loc) · 1.43 KB
/
Copy pathBDDMockitoTestDemo.java
File metadata and controls
38 lines (32 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import com.four.unittest.UnitTestApplication;
import com.four.unittest.service.UnitInterface;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.math.BigDecimal;
import static org.mockito.BDDMockito.*;
@Slf4j
@ActiveProfiles(profiles = "test")
@SpringBootTest(classes = UnitTestApplication.class,
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestExecutionListeners(MockitoTestExecutionListener.class)
public class BDDMockitoTestDemo extends AbstractTestNGSpringContextTests {
@MockBean
UnitInterface unitInterface;
@BeforeClass
public void setup() {
given(unitInterface.register(anyString())).willReturn("register success");
given(unitInterface.calculate(anyDouble())).willThrow(new IllegalArgumentException());
}
@Test(expectedExceptions = IllegalArgumentException.class)
public void test(){
BigDecimal calculate = unitInterface.calculate(123.112d);
log.info(String.valueOf(calculate));
}
}