需求说明:需要对一个service接口进行单元测试
1.在pom.xml中加入依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>org.example</groupId> <artifactId>springboot-interview-demo</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!--引入springboot内置的tomact作为启动容器,不然启动后会自动关闭--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- MySQL驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.22</version> </dependency> <!-- 集成MyBatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.2</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.19</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.28</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-mockito2</artifactId> <version>2.0.2</version> </dependency> <!-- <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4</artifactId> <version>2.0.2</version> </dependency>--> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4</artifactId> <version>2.0.2</version> <scope>test</scope> </dependency> </dependencies> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> </project>
2.编写controller类
@RestController @RequestMapping("/tx") public class TxClassController { // @Autowired // private TxClass txClass; // @GetMapping("tx_required") public String testTx() throws Exception { TxTable txTable=TxTable.getInstance(); System.out.print("AAAAAAAAAAAAAAAAAA"); return "success"; } }
3.编写实体类
@AllArgsConstructor @NoArgsConstructor @Data @Entity(name = "oct_tx") public class TxTable { private static TxTable txTable= new TxTable(); @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id") private Integer id; @Column(name="name") private String name; public static TxTable getInstance(){ return txTable; } }
4.编写测试类
@RunWith(PowerMockRunner.class) @PrepareForTest({TxClassController.class,TxTable.class}) public class TxClassControllerTest { @InjectMocks private TxClassController txClassController; @Mock private TxClass txClass; @Test public void test() throws Exception { PowerMockito.mockStatic(TxTable.class); TxTable txTableMock=PowerMockito.mock(TxTable.class); PowerMockito.when(TxTable.getInstance()).thenReturn(txTableMock); txClassController.testTx(); } }
测试结果:
出现这个说明已经模拟对象成功