Boost Your Java Test Coverage Instantly with Squaretest – A Step‑by‑Step Guide

This tutorial explains how to install and use the Squaretest IntelliJ plugin to automatically generate unit test methods for Java classes, covering setup, template selection, generated code examples, handling of getters/setters, and tips for achieving high test coverage quickly.

Programmer DD
Programmer DD
Programmer DD
Boost Your Java Test Coverage Instantly with Squaretest – A Step‑by‑Step Guide

Squaretest is an IntelliJ IDEA plugin that automatically generates unit test methods for Java classes, helping teams meet code‑quality metrics such as test‑coverage thresholds and Sonar issue checks.

Why Use Squaretest?

Many legacy projects and fast‑delivery tasks suffer from low test coverage (often below 5%). Manually writing tests for dozens of public methods is time‑consuming, so automating this step can dramatically improve productivity.

Installation

Open File → Settings → Plugins, search for Squaretest, click Install, and restart the IDE. After restart a new Squaretest menu appears.

Generating Tests

Select a class with public methods, right‑click in the editor and choose Generate…. In the dialog pick the desired test template (the first time you’ll be prompted to select one). The plugin creates a test class named <OriginalClassName>Test in the appropriate test folder, containing a test method for each public method.

Sample Generated Test

public class CrawlerScreenShotServiceImplTest {

    @Mock
    private CrawerScreenShotTaskMapper mockCrawerScreenShotTaskMapper;
    @Mock
    private CrawerScreenShotTaskLogMapper mockCrawerScreenShotTaskLogMapper;

    @InjectMocks
    private CrawlerScreenShotServiceImpl crawlerScreenShotServiceImplUnderTest;

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

    @Test
    public void testReceiveData() {
        // Setup
        final CrawlerScreenShotVO vo = new CrawlerScreenShotVO();
        vo.setUrl("url");
        // ... other setters ...
        when(mockCrawerScreenShotTaskLogMapper.saveSelective(any(CrawerScreenShotTaskLog.class))).thenReturn(0);
        // Run the test
        final Result<String> result = crawlerScreenShotServiceImplUnderTest.receiveData(vo);
        // Verify the results
    }
    // Additional test methods omitted for brevity
}

If you only need coverage and do not care about assertions, you can remove the assertEquals lines.

Changing the Test Template

Press Alt+M or use the second‑last menu item under Squaretest to switch templates.

Limitations and Work‑Around

Squaretest only generates tests for constructors; it does not create tests for getter/setter methods. To cover those, you can create a base test class that uses reflection to invoke all getters and setters automatically.

@SpringBootTest
@RunWith(MockitoJUnitRunner.class)
public abstract class BaseVoEntityTest<T> {
    protected abstract T getT();

    private void testGetAndSet() throws IllegalAccessException, InstantiationException, IntrospectionException, InvocationTargetException {
        T t = getT();
        Class<?> modelClass = t.getClass();
        Object obj = modelClass.newInstance();
        Field[] fields = modelClass.getDeclaredFields();
        for (Field f : fields) {
            if (f.getName().equals("serialVersionUID") || Modifier.isStatic(f.getModifiers()) || f.getGenericType().toString().equals("boolean") || f.isSynthetic()) {
                continue;
            }
            PropertyDescriptor pd = new PropertyDescriptor(f.getName(), modelClass);
            Method get = pd.getReadMethod();
            Method set = pd.getWriteMethod();
            set.invoke(obj, get.invoke(obj));
        }
    }

    @Test
    public void getAndSetTest() throws InvocationTargetException, IntrospectionException, InstantiationException, IllegalAccessException {
        this.testGetAndSet();
    }
}

Extend this base class in your VO/DTO test and run the generated Squaretest methods; the combined approach can push coverage above 90% in a single day.

Result

Using Squaretest together with the reflective base test class, you can quickly achieve high unit‑test coverage, saving roughly 70% of manual testing effort.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Javaunit testingIntelliJ IDEAMockitotest coverageSquaretest
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.