自己使用的简化的单测模板(部分代码)

阅读更多
import static org.junit.Assert.*;
import static org.powermock.api.mockito.PowerMockito.*;
import static org.powermock.reflect.Whitebox.*;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import static java.util.stream.Collectors.*;
import static java.util.Comparator.*;
import static com.google.common.base.Preconditions.*;



    @Before
    public void before() {

        this.employeeBasicService = mock(EmployeeBasicService.class);
        setInternalState(impl, "employeeBasicService", employeeBasicService);

    }



private EntryServiceImpl impl;
private EnhancedUserQueryReadService enhancedUserQueryService;
private void init() {
    impl = new EntryServiceImpl();
    this.enhancedUserQueryService = mock(EnhancedUserQueryReadService.class);
    setInternalState(impl, "enhancedUserQueryService", enhancedUserQueryService);
}

private EntryServiceImpl impl;
private void init() {
    impl = new EntryServiceImpl();
}

try {
    init();
    fail();
} catch (Exception e) {
    assertTrue(true);
}



try {
    init();

} catch (Exception e) {
    e.printStackTrace();
    fail(e.getMessage());
}



解决问题: Could not reconfigure JMX java.lang.LinkageError: loader constraint violation: loader (instance of org/powermock/core/classloader/MockClassLoader) previously initiated loading for a different type with name "javax/management/MBeanServer"
@PowerMockIgnore({"javax.management.*"})
解决问题: java.security.NoSuchAlgorithmException: class configured for SSLContext: sun.security.ssl.SSLContextImpl$TLSContext not a SSLContext
@PowerMockIgnore({"javax.net.ssl.*"})

@RunWith(PowerMockRunner.class) // mock静态方法,必须有
@PrepareForTest({ DateTime.class }) // mock静态方法,必须有
@PowerMockIgnore({"javax.net.ssl.*","javax.management.*"})
public class CommonUtilsTest {

    mockStatic(DateTime.class);

    mockStatic(OpLogClient.class);
    doNothing().when(OpLogClient.class);
    OpLogClient.log(anyObject());
}



actual
expected



return new com.google.gson.GsonBuilder().setPrettyPrinting().create().toJson(this);





 

你可能感兴趣的:(自己使用的简化的单测模板(部分代码))