提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
提示:这里可以添加本文要记录的大概内容:
例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。
提示:以下是本篇文章正文内容,下面案例可供参考
User.class
package com.lanshang.common;
public class User {
}
UserDao.class
package com.lanshang.helloworld.dao;
import com.lanshang.common.User;
public class UserDao {
public void insertUser(User user){
throw new UnsupportedOperationException();
}
public int getCount(){
throw new UnsupportedOperationException();
}
}
UserService.class
package com.lanshang.helloworld.service;
import com.lanshang.common.User;
import com.lanshang.helloworld.dao.UserDao;
public class UserService {
private UserDao userDao;
public UserService(UserDao userDao) {
this.userDao = userDao;
}
public void insertUser(User user){
userDao.insertUser(user);
}
public int getCount(){
return userDao.getCount();
}
}
pom
<?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>
<groupId>org.example</groupId>
<artifactId>powerMock_study</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.powermock/powermock-module-junit4 -->
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.6.5</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.powermock/powermock-api-mockito -->
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.6.5</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
package com.lanshang.helloworld.service;
import com.lanshang.common.User;
import com.lanshang.helloworld.dao.UserDao;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import javax.annotation.processing.SupportedSourceVersion;
import static org.junit.Assert.*;
public class UserServiceTest {
UserDao userDao;
UserService userService;
@Before
public void init(){
userDao = PowerMockito.mock(UserDao.class);
userService = new UserService(userDao);
}
@Test
public void testInsertUser() {
PowerMockito.doNothing().when(userDao).insertUser(Mockito.anyObject());
User user = new User();
userService.insertUser(user);
Mockito.verify(userDao,Mockito.times(1)).insertUser(user);
}
@Test
public void testGetCount() {
PowerMockito.when(userService.getCount()).thenReturn(2);
int count = userService.getCount();
assertEquals(2,count);
}
}
UserDao.class
package com.lanshang.local.dao;
import com.lanshang.common.User;
public class UserDao {
public void insertUser(User user){
throw new UnsupportedOperationException();
}
public int getCount(){
throw new UnsupportedOperationException();
}
}
UserService.class
package com.lanshang.local.service;
import com.lanshang.common.User;
import com.lanshang.local.dao.UserDao;
public class UserService {
public UserService() {
}
public void insertUser(User user){
UserDao userDao = new UserDao();
userDao.insertUser(user);
}
public int getCount(){
UserDao userDao = new UserDao();
return userDao.getCount();
}
}
package com.lanshang.local.service;
import com.lanshang.common.User;
import com.lanshang.local.dao.UserDao;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.junit.Assert.assertEquals;
@RunWith(PowerMockRunner.class)
@PrepareForTest({UserService.class})
public class UserServiceTest {
private UserService userService;
@Before
public void init(){
userService = new UserService();
}
@Test
public void insertUser() {
User user = new User();
UserDao userDao = PowerMockito.mock(UserDao.class);
try {
PowerMockito.whenNew(UserDao.class).withAnyArguments().thenReturn(userDao);
PowerMockito.doNothing().when(userDao).insertUser(Mockito.anyObject());
userService.insertUser(user);
Mockito.verify(userDao,Mockito.times(1)).insertUser(user);
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void getCount() {
UserDao userDao = PowerMockito.mock(UserDao.class);
try {
PowerMockito.whenNew(UserDao.class).withAnyArguments().thenReturn(userDao);
PowerMockito.when(userService.getCount()).thenReturn(1);
int count = userService.getCount();
assertEquals(1,count);
} catch (Exception e) {
e.printStackTrace();
}
}
}
UserDao.class
package com.lanshang.mockstatic.dao;
import com.lanshang.common.User;
public class UserDao {
public static void insertUser(User user){
throw new UnsupportedOperationException();
}
public static int getCount(){
throw new UnsupportedOperationException();
}
}
UserService.class
package com.lanshang.mockstatic.service;
import com.lanshang.common.User;
import com.lanshang.mockstatic.dao.UserDao;
public class UserService {
public void insertUser(User user){
UserDao.insertUser(user);
}
public int getCount(){
return UserDao.getCount();
}
}
package com.lanshang.mockstatic.service;
import com.lanshang.common.User;
import com.lanshang.mockstatic.dao.UserDao;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.junit.Assert.*;
@RunWith(PowerMockRunner.class)
@PrepareForTest({UserService.class,UserDao.class})
public class UserServiceTest {
UserService userService;
@Before
public void init(){
userService = new UserService();
}
@Test
public void insertUser() {
User user = new User();
PowerMockito.mockStatic(UserDao.class);
PowerMockito.doNothing().when(UserDao.class,"insertUser",anyObject());
userService.insertUser(user);
//PowerMockito.verifyStatic();
PowerMockito.verifyStatic(Mockito.times(1));
}
@Test
public void getCount() {
PowerMockito.mockStatic(UserDao.class);
PowerMockito.when(UserDao.getCount()).thenReturn(1);
int count = UserDao.getCount();
assertEquals(1,count);
}
}
UserDao.class
package com.lanshang.mockfinal.dao;
import com.lanshang.common.User;
public final class UserDao {
public void insertUser(User user){
throw new UnsupportedOperationException();
}
public int getCount(){
throw new UnsupportedOperationException();
}
}
UserService.class
package com.lanshang.mockfinal.service;
import com.lanshang.common.User;
import com.lanshang.mockfinal.dao.UserDao;
public class UserService {
private UserDao userDao;
public UserService(UserDao userDao) {
this.userDao = userDao;
}
public void insertUser(User user){
userDao.insertUser(user);
}
public int getCount(){
return userDao.getCount();
}
}
package com.lanshang.mockfinal.service;
import com.lanshang.common.User;
import com.lanshang.mockfinal.dao.UserDao;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.junit.Assert.*;
@RunWith(PowerMockRunner.class)
@PrepareForTest({UserDao.class,UserService.class})
public class UserServiceTest {
@Test
public void insertUser() {
User user = new User();
UserDao userDao = PowerMockito.mock(UserDao.class);
UserService userService = new UserService(userDao);
PowerMockito.doNothing().when(userDao).insertUser(Mockito.anyObject());
userService.insertUser(user);
Mockito.verify(userDao,Mockito.times(1)).insertUser(user);
}
@Test
public void getCount() {
UserDao userDao = PowerMockito.mock(UserDao.class);
UserService userService = new UserService(userDao);
PowerMockito.when(userDao.getCount()).thenReturn(1);
int count = userService.getCount();
assertEquals(1,count);
}
}
UserDao.class
package com.lanshang.verify.dao;
import com.lanshang.common.User;
public class UserDao {
public void updateUser(User user){
throw new UnsupportedOperationException();
}
public void insertUser(User user){
throw new UnsupportedOperationException();
}
public int getUser(){
throw new UnsupportedOperationException();
}
}
UserService.class
package com.lanshang.verify.service;
import com.lanshang.common.User;
import com.lanshang.verify.dao.UserDao;
public class UserService {
public void saveOrUpdate(User user){
UserDao userDao = new UserDao();
if(userDao.getUser()==0){
userDao.insertUser(user);
}else{
userDao.updateUser(user);
}
}
}
package com.lanshang.verify.service;
import com.lanshang.common.User;
import com.lanshang.verify.dao.UserDao;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.junit.Assert.*;
@RunWith(PowerMockRunner.class)
@PrepareForTest({UserService.class})
public class UserServiceTest {
@Test
public void saveOrUpdate() {
UserService userService = new UserService();
UserDao userDao = PowerMockito.mock(UserDao.class);
try {
PowerMockito.whenNew(UserDao.class).withAnyArguments().thenReturn(userDao);
PowerMockito.when(userDao.getUser()).thenReturn(0);
userService.saveOrUpdate(new User());
Mockito.verify(userDao,Mockito.times(1)).insertUser(Mockito.anyObject());
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void saveOrUpdate1() {
UserService userService = new UserService();
UserDao userDao = PowerMockito.mock(UserDao.class);
try {
PowerMockito.whenNew(UserDao.class).withAnyArguments().thenReturn(userDao);
PowerMockito.when(userDao.getUser()).thenReturn(1);
userService.saveOrUpdate(new User());
Mockito.verify(userDao,Mockito.times(1)).updateUser(Mockito.anyObject());
} catch (Exception e) {
e.printStackTrace();
}
}
}
UserDao.class
package com.lanshang.construction.dao;
public class UserDao {
String username;
String password;
public UserDao(String username, String password) {
this.username = username;
this.password = password;
}
public void insertUser(){
throw new UnsupportedOperationException();
}
public int getCount(){
throw new UnsupportedOperationException();
}
}
UserService.class
package com.lanshang.construction.service;
import com.lanshang.construction.dao.UserDao;
public class UserService {
public void insertUser(String username,String password){
UserDao userDao = new UserDao(username,password);
userDao.insertUser();
}
}
package com.lanshang.construction.service;
import com.lanshang.construction.dao.UserDao;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.junit.Assert.*;
@RunWith(PowerMockRunner.class)
@PrepareForTest({UserDao.class,UserService.class})
public class UserServiceTest {
@Test
public void insertUser() {
String username="cat";
String password="111";
UserDao userDao = PowerMockito.mock(UserDao.class);
try {
PowerMockito.whenNew(UserDao.class).withArguments(username,password).thenReturn(userDao);
PowerMockito.doNothing().when(userDao).insertUser();
UserService userService = new UserService();
userService.insertUser(username,password);
Mockito.verify(userDao,Mockito.times(1)).insertUser();
} catch (Exception e) {
e.printStackTrace();
}
}
}
UserDao.class
package com.lanshang.matcher.dao;
public class UserDao {
public String query(String name){
throw new UnsupportedOperationException();
}
}
UserService.class
package com.lanshang.matcher.service;
import com.lanshang.matcher.dao.UserDao;
public class UserService {
UserDao userDao = new UserDao();
public String query(String name){
return userDao.query(name);
}
}
package com.lanshang.matcher.service;
import com.lanshang.matcher.dao.UserDao;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatcher;
import org.mockito.Matchers;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.junit.Assert.*;
@RunWith(PowerMockRunner.class)
@PrepareForTest({UserService.class,UserDao.class})
public class UserServiceTest {
@Test
public void query() {
UserDao userDao = PowerMockito.mock(UserDao.class);
PowerMockito.when(userDao.query(Matchers.argThat(new MyArgumentMatcher()))).thenReturn("yes");
UserService userService = new UserService();
userService.userDao=userDao;
assertEquals("yes",userService.query("aaa"));
assertEquals("yes",userService.query("bbb"));
assertEquals("yes",userService.query("ccc"));
}
static class MyArgumentMatcher extends ArgumentMatcher<String>{
@Override
public boolean matches(Object o) {
String name =(String)o;
switch (name){
case "aaa":
case "bbb":
case "ccc":
return true;
default:
return false;
}
}
}
}
UserDao.class
package com.lanshang.answer.dao;
public class UserDao {
public String query(String name){
throw new UnsupportedOperationException();
}
}
UserService.class
package com.lanshang.answer.service;
import com.lanshang.answer.dao.UserDao;
public class UserService {
UserDao userDao = new UserDao();
public String query(String name){
return userDao.query(name);
}
}
package com.lanshang.answer.service;
import com.lanshang.answer.dao.UserDao;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.junit.Assert.*;
@RunWith(PowerMockRunner.class)
@PrepareForTest({UserService.class, UserDao.class})
public class UserServiceTest {
@Test
public void query() {
UserDao userDao = PowerMockito.mock(UserDao.class);
PowerMockito.when(userDao.query(Mockito.anyString())).then(invocation ->{
String argument = (String) invocation.getArguments()[0];
switch (argument){
case "aaa":return "AAA";
case "bbb":return "BBB";
case "ccc":return "CCC";
default:
return new RuntimeException(argument);
}
});
UserService userService = new UserService();
userService.userDao=userDao;
assertEquals("AAA",userService.query("aaa"));
assertEquals("BBB",userService.query("bbb"));
assertEquals("CCC",userService.query("ccc"));
}
}
package com.lanshang.spy;
public class CheckService {
public Boolean check(String name){
return checkExist1(name);
}
public Boolean check1(){
return checkExist();
}
public void check3(String name){
checkExist2(name,"a");
}
private Boolean checkExist() {
throw new UnsupportedOperationException();
}
private Boolean checkExist1(String name) {
throw new UnsupportedOperationException();
}
private void checkExist2(String name,String passwoed) {
throw new UnsupportedOperationException();
}
}
package com.lanshang.spy;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.junit.Assert.*;
@RunWith(PowerMockRunner.class)
@PrepareForTest({CheckService.class})
public class CheckServiceTest {
//有返回值,带参数的私有方法
@Test
public void check() throws Exception {
CheckService checkService = PowerMockito.spy(new CheckService());
//下面这种写法不行,原因未明
//PowerMockito.when(checkService,"checkExist1","a").thenReturn(true);
PowerMockito.doReturn(true).when(checkService,"checkExist1","a");
assertTrue(checkService.check("a"));
}
//有返回值,不带参数的私有方法
@Test
public void check1() throws Exception {
CheckService checkService = PowerMockito.spy(new CheckService());
PowerMockito.doReturn(true).when(checkService,"checkExist");
assertTrue(checkService.check1());
}
//没有返回值的
@Test
public void check2() throws Exception {
CheckService checkService = PowerMockito.spy(new CheckService());
PowerMockito.doNothing().when(checkService).check3("name");
checkService.check3("name");
Mockito.verify(checkService,Mockito.times(1)).check3("name");
}
}