wrdv / testme-idea

TestMe IntelliJ IDEA Plugin - Auto generates Unit Test code
http://weirddev.com/testme
Other
157 stars 62 forks source link

for classes with only private constructor exclude mocks #22

Closed huangliang992 closed 4 months ago

huangliang992 commented 4 months ago

for classes with only private constructor exclude mocks,

for example tested class like

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.unicom.tdt.admin.model.exception.ResponseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Base64Utils;

import javax.crypto.Cipher;

import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Map;

public class AesUtil {

    private AesUtil(){
        throw new IllegalStateException("Utility class");
    }
    private static Logger logger = LoggerFactory.getLogger(AesUtil.class);

    private static final String KEY_ALGORITHM = "AES";

    private static final String DEFAULT_CIPHER_ALGORITHM = "AES/GCM/NoPadding";

    public static String encrypt(String content, String password) {
    }

    public static String encrypt(Object object,String field, String password) {
    }

    public static String decrypt(String content, String password) {
    }

    public static void decrypt(Object object,String field,String privateKey) throws Exception {
     }
}

the generated test class is , the mock should be removed

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.slf4j.Logger;

import static org.mockito.Mockito.*;

public class AesUtilTest44 {
    @Mock
    Logger logger;
    @InjectMocks
    AesUtil aesUtil;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void testEncrypt() throws Exception {
        String result = AesUtil.encrypt("content", "password");
        Assert.assertEquals("replaceMeWithExpectedResult", result);
    }

    @Test
    public void testEncrypt2() throws Exception {
        String result = AesUtil.encrypt("object", "field", "password");
        Assert.assertEquals("replaceMeWithExpectedResult", result);
    }

    @Test
    public void testDecrypt() throws Exception {
        String result = AesUtil.decrypt("content", "password");
        Assert.assertEquals("replaceMeWithExpectedResult", result);
    }

    @Test
    public void testDecrypt2() throws Exception {
        AesUtil.decrypt("object", "field", "privateKey");
    }
}

// Generated with love by TestMe :) Please report issues and submit feature requests at:
// http://weirddev.com/forum#!/testme
CLAassistant commented 4 months ago

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

:white_check_mark: huangliang992
:x: 南京-黄亮


南京-黄亮 seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

yaronyam commented 4 months ago

Good use case. BTW for integration test case, I would prefer a clean example with field that is not static and not initialized inline - in both cases the field should not be mocked for other reasons. Thanks

yaronyam commented 4 months ago

@huangliang992 - Published this feature as an EAP version for now - https://plugins.jetbrains.com/plugin/9471-testme/versions/eap/497405. Will be included in the next stable version. Thanks for contributing! EAP versions installation instructions