{"id":26776,"date":"2020-02-15T07:14:09","date_gmt":"2020-02-15T07:14:09","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=26776"},"modified":"2025-05-17T07:12:36","modified_gmt":"2025-05-17T07:12:36","slug":"how-to-fix-junit-invalid-test-class-error","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/how-to-fix-junit-invalid-test-class-error\/","title":{"rendered":"How to Fix JUnit Invalid test class Error"},"content":{"rendered":"<h1>How to Fix JUnit Invalid test class Error<\/h1>\n<h2>Exception Trace<\/h2>\n<p>org.junit.runners.model.InvalidTestClassError: Invalid test class &#8216;com.testingdocs.tutorials.TestCaseOne&#8217;:<br \/>\n1. The class com.testingdocs.tutorials.TestCaseOne is not public.<br \/>\n2. Test class should have exactly one public constructor<br \/>\nat org.junit.runners.ParentRunner.validate(ParentRunner.java:525)<br \/>\nat org.junit.runners.ParentRunner.&lt;init&gt;(ParentRunner.java:102)<br \/>\nat org.junit.runners.BlockJUnit4ClassRunner.&lt;init&gt;(BlockJUnit4ClassRunner.java:84)<br \/>\nat org.junit.runners.JUnit4.&lt;init&gt;(JUnit4.java:23)<\/p>\n<p>&nbsp;<\/p>\n<p>The error message suggests that your JUnit test class has incorrect visibility or constructor issues.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-26781\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/JUnit-Invalid-Test-Class.png\" alt=\"JUnit Invalid Test Class\" width=\"1914\" height=\"955\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/JUnit-Invalid-Test-Class.png 1914w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/JUnit-Invalid-Test-Class-300x150.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/JUnit-Invalid-Test-Class-1024x511.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/JUnit-Invalid-Test-Class-768x383.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/JUnit-Invalid-Test-Class-1536x766.png 1536w\" sizes=\"auto, (max-width: 1914px) 100vw, 1914px\" \/><\/p>\n<h2>Possible Fixes<\/h2>\n<div class=\"solution\">\n<h3>Ensure the Test Class is Public<\/h3>\n<p>Your test class must be declared as <code class=\"\" data-line=\"\">public<\/code>:<\/p>\n<pre><code class=\"\" data-line=\"\">\npackage com.testingdocs.tutorials;\n\nimport org.junit.Test;\nimport static org.junit.Assert.assertTrue;\n\npublic class TestCaseOne {  \/\/ Ensure the class is public\n\n    @Test\n    public void testExample() {\n        assertTrue(true);\n    }\n}\n            <\/code><\/pre>\n<\/div>\n<div class=\"solution\">\n<h3>Ensure There is Only One Public Constructor<\/h3>\n<p>JUnit test classes should have only <strong>one public constructor<\/strong>. If you have multiple constructors, remove the extra ones.<\/p>\n<p><strong>Incorrect:<\/strong><\/p>\n<pre><code class=\"\" data-line=\"\">\npublic class TestCaseOne {\n    public TestCaseOne() {}  \/\/ Default constructor (OK)\n    public TestCaseOne(String param) {}  \/\/ Extra constructor (INVALID)\n}\n            <\/code><\/pre>\n<p><strong>Correct:<\/strong><\/p>\n<pre><code class=\"\" data-line=\"\">\npublic class TestCaseOne {\n    public TestCaseOne() {}  \/\/ OK\n}\n            <\/code><\/pre>\n<\/div>\n<div class=\"solution\">\n<h3>Ensure JUnit is Using the Correct Runner<\/h3>\n<p>If you are using JUnit 4, add the correct runner annotation:<\/p>\n<pre><code class=\"\" data-line=\"\">\nimport org.junit.runner.RunWith;\nimport org.junit.runners.JUnit4;\n\n@RunWith(JUnit4.class)\npublic class TestCaseOne {\n    @Test\n    public void testExample() {\n        assertTrue(true);\n    }\n}\n            <\/code><\/pre>\n<p>For <strong>JUnit 5 (Jupiter)<\/strong>, no runner annotation is needed:<\/p>\n<pre><code class=\"\" data-line=\"\">\nimport org.junit.jupiter.api.Test;\nimport static org.junit.jupiter.api.Assertions.assertTrue;\n\npublic class TestCaseOne {\n    @Test\n    void testExample() {\n        assertTrue(true);\n    }\n}\n            <\/code><\/pre>\n<\/div>\n<div class=\"solution\">\n<h3>Ensure the Test Class is Not a Nested (Inner) Class<\/h3>\n<p>If your test class is an inner class, it must be <code class=\"\" data-line=\"\">static<\/code>:<\/p>\n<pre><code class=\"\" data-line=\"\">\npublic class OuterClass {\n    public static class TestCaseOne {  \/\/ Must be static\n        @Test\n        public void testExample() {\n            assertTrue(true);\n        }\n    }\n}\n            <\/code><\/pre>\n<\/div>\n<h2>Final Checklist<\/h2>\n<ul>\n<li>\u2705 Class is <code class=\"\" data-line=\"\">public<\/code><\/li>\n<li>\u2705 Only <strong>one<\/strong> (or no) constructor<\/li>\n<li>\u2705 No extra constructor parameters<\/li>\n<li>\u2705 Proper <code class=\"\" data-line=\"\">@Test<\/code> annotations<\/li>\n<li>\u2705 Using correct JUnit version<\/li>\n<\/ul>\n<p>After applying these fixes, try running the test again.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to Fix JUnit Invalid test class Error Exception Trace org.junit.runners.model.InvalidTestClassError: Invalid test class &#8216;com.testingdocs.tutorials.TestCaseOne&#8217;: 1. The class com.testingdocs.tutorials.TestCaseOne is not public. 2. Test class should have exactly one public constructor at org.junit.runners.ParentRunner.validate(ParentRunner.java:525) at org.junit.runners.ParentRunner.&lt;init&gt;(ParentRunner.java:102) at org.junit.runners.BlockJUnit4ClassRunner.&lt;init&gt;(BlockJUnit4ClassRunner.java:84) at org.junit.runners.JUnit4.&lt;init&gt;(JUnit4.java:23) &nbsp; The error message suggests that your JUnit test class has incorrect visibility or constructor issues. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[],"class_list":["post-26776","post","type-post","status-publish","format-standard","hentry","category-java","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/26776","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=26776"}],"version-history":[{"count":7,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/26776\/revisions"}],"predecessor-version":[{"id":27414,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/26776\/revisions\/27414"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=26776"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=26776"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=26776"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}