How to set when "The constructor Empty () is not visible" occurs in junit

Summary of this article

I had a little trouble when using junit with eclipse, so I will describe the setting method as a memorandum. Specifically, the following error may occur when the test target class has a private constructor, so the workaround is described.

The constructor Empty() is not visible

reference

How to test private methods in Junit

[Get method, execute method with Java reflection API] (http://pppurple.hatenablog.com/entry/2016/07/23/205446)

environment

OS:Windows8.1 32bit eclipse:4.5.2

Tested class

TestTarget.java



packege com.web.test

public class TestTarget {

  private TestTarget() {
    //No description
  }

  private int minus(int x, int y) {
        return (x - y);
  }

}

junit test class

Create a junit test class for the class under test

SampleTest.java



import static org.junit.Assert.*;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.junit.Test;

public class SampleTest {

    @Test
    public void test()
        throws NoSuchMethodException,
        SecurityException,
        IllegalAccessException,
        IllegalArgumentException,
        InvocationTargetException
    {
        TestTarget testTarget = new TestTarget();

        Method method = Sample.class.getDeclaredMethod("minus", int.class, int.class);
        method.setAccessible(true);

        int actual = (int)method.invoke(testTarget, 8, 2);

        assertEquals(6, actual);
    }

If you do this  The constructor Empty() is not visible Has occurred, so change it as follows

SampleTest.java



import static org.junit.Assert.*;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.junit.Test;

public class SampleTest {

    @Test
    public void test()
        throws NoSuchMethodException,
        SecurityException,
        IllegalAccessException,
        IllegalArgumentException,
        InvocationTargetException
    {
        //Before correction
        //TestTarget testTarget = new TestTarget();
                //Revised
                //Get an instance of the class under test using the reflection API
        TestTarget testTarget = Class.forName("com.web.test.TestTarget").newInstance();

        Method method = testTarget.class.getDeclaredMethod("minus", int.class, int.class);
        method.setAccessible(true);

        int actual = (int)method.invoke(testTarget, 8, 2);

        assertEquals(6, actual);
    }

with this  The constructor Empty() is not visible Has been resolved

Recommended Posts

How to set when "The constructor Empty () is not visible" occurs in junit
How to set chrony when the time shifts in CentOS7
How to output the value when there is an array in the array
How to solve the problem when the value is not sent when the form is disabled in rails and sent
How to write the view when Vue is introduced in Rails?
How to constrain the action of the transition destination when not logged in
[Ruby] How to prevent errors when nil is included in the operation
When the project is not displayed in eclipse
How to solve the problem that it is not processed normally when nesting beans in Spring Batch
How to set the display time to Japan time in Rails
[Java] How to omit the private constructor in Lombok
How to resolve the error'ActionView :: Template :: Error (The asset "application.css" is not present in the asset pipeline.'" When precompiling Rails assets
How to identify the cause when GC occurs frequently and CPU usage is high
Summary of how to use the proxy set in IE when connecting with Java
When an error occurs even though validation is not set
How to perform a specific process when the back button is pressed in Android Fragment
How to set Lombok in Eclipse
What to do when the changes in the Servlet are not reflected
[jOOQ] How to CASE WHEN in the WHERE / AND / OR clause
The milliseconds to set in /lib/calendars.properties of Java jre is UTC
How to solve the unknown error when using slf4j in Java
Check the behavior when the exception specified by expected in JUnit 4 @Test annotation occurs / does not occur
Monkey patch to return the current time when an empty string is specified in Ruby's Time.parse
How to set the indent to 2 single-byte spaces in the JAXB implementation of the JDK
Spring Autowired is written in the constructor
What to do if the prefix c is not bound in JSP
How to filter JUnit Test in Gradle
[RSpec] When you want to use the instance variable of the controller in the test [assigns is not recommended]
What to do and how to install when an error occurs in DXRuby 1.4.7
[Java] How to set the Date time to 00:00:00
How to fix system date in JUnit
[Docker] How to build when the source code is bind-mounted on the container
How to find the total number of pages when paging in Java
How to get the date in java
How to not start Flyway when running unit tests in Spring Boot
What to do if Operation not permitted is displayed when you execute a command in the terminal
[Swift] How to set an image in the background without using UIImageView.
How to reference a column when overriding the column name method in ActiveRecord
When the project is not displayed in eclipse
Processing when an ID that does not exist in the database is entered in the URL
How to set when "The constructor Empty () is not visible" occurs in junit
When I run the source command in the Ubuntu Makefile, it says "source: command not found"
How to set tabs and spaces to be visible by using the tab key to insert spaces in Java files in Eclipse
Possibility when deploying to EC2 but nothing is displayed in the error log
How to redirect to http-> https when SSL is enabled in Rails × Heroku environment
If the parameter is an array, how to include it in Stopara's params.permit
What to do when rails db: seed does not reflect in the database
[Ruby] Meaning of &. How to avoid the error when the receiver (object) is nil
[Swift] What to do if the app icon is set but not reflected
How to set environment variables in the properties file of Spring boot application
Is it mainstream not to write the closing tag of <P> tag in Javadoc?
Cause of is not visible when calling a method of another class in java
How to check the logs in the Docker container
When the server fails to start in Eclipse
JUnit 5: How to write test cases in enum
How to add sound in the app (swift)
How to delete the database when recreating the application
[Java] (for MacOS) How to set the classpath
What to do when IllegalStateException occurs in PlayFramework
How to build the simplest blockchain in Ruby
How to log in automatically when Ubuntu restarts
How to check Rails commands in the terminal
Ebean.update () is not executed in the inherited model.
How to automatically generate a constructor in Eclipse