{"id":44481,"date":"2017-04-12T11:00:02","date_gmt":"2017-04-12T08:00:02","guid":{"rendered":"http:\/\/examples.javacodegeeks.com\/?p=44481"},"modified":"2019-03-20T13:59:24","modified_gmt":"2019-03-20T11:59:24","slug":"junit-test-constructor-example","status":"publish","type":"post","link":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-test-constructor-example\/","title":{"rendered":"JUnit Test Constructor Example"},"content":{"rendered":"<p>In JUnit Test Constructor Example, we shall show you the process of testing the constructor of a class. It is as simple as we are testing other methods. Since the constructor is called before any other methods of the class we need to see the relevance of testing it.<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<\/p>\n<p>&nbsp;<br \/>\nConstructors are used for initializing the class and then do some processing on it. We will examine the different ways of testing the constructor of a class.<\/p>\n<h2>1. Introduction<\/h2>\n<p><a href=\"http:\/\/junit.org\/junit4\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">JUnit<\/a> provides lot of methods to test our cases. We can test the constructor of a class using the same techniques we have used in our previous examples. Sometimes we need to initialize the objects and we do them in a constructor. In JUnit we can also do same using the <code>@Before<\/code> method. But what will be the difference between them?<\/p>\n<p>They are following the same scenario as both all called before any test case is executed.<\/p>\n<div class=\"tip\"><strong>Tip<\/strong><br \/>\nConstructor and @Before both are same. But constructor has a drawback that it will not give proper error message, whereas @Before method will give proper error message in case of failing.<\/div>\n<p>We will see in the below example the way to test our constructor.<\/p>\n<h2>2. Technologies Used<\/h2>\n<p>We will be using following technologies in out below example.<\/p>\n<ul>\n<li><strong>Java 1.8<\/strong> &#8211; language to code<\/li>\n<li><strong>JUnit 4.12<\/strong> &#8211; testing framework<\/li>\n<li><strong>Maven<\/strong> &#8211; build tool<\/li>\n<li><strong>Eclipse<\/strong> &#8211; IDE for code<\/li>\n<\/ul>\n<h2>3. Project Setup<\/h2>\n<p>Let&#8217;s start creating our project.<\/p>\n<div class=\"tip\"><strong>Tip<\/strong><br \/>\nYou may skip project creation and jump directly to the <a href=\"#code\"><strong>beginning of the example<\/strong><\/a> below.<\/div>\n<p>Open Eclipse. Click <strong>File -&gt; New -&gt; Maven Project<\/strong>. You will be taken to below screen. Fill in the details and click on <strong>Next<\/strong> button.<\/p>\n<p><figure id=\"attachment_44483\" aria-describedby=\"caption-attachment-44483\" style=\"width: 725px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/04\/junit-test-constructor-example-setup-1.jpg\"><img decoding=\"async\" class=\"size-full wp-image-44483\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/04\/junit-test-constructor-example-setup-1.jpg\" alt=\"JUnit Test Constructor Example Setup 1\" width=\"725\" height=\"503\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/04\/junit-test-constructor-example-setup-1.jpg 725w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/04\/junit-test-constructor-example-setup-1-300x208.jpg 300w\" sizes=\"(max-width: 725px) 100vw, 725px\" \/><\/a><figcaption id=\"caption-attachment-44483\" class=\"wp-caption-text\">Figure 1: JUnit Test Constructor Example Setup 1<\/figcaption><\/figure><div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p>On this screen, fill in all the details and click on the <strong>Finish<\/strong> button.<\/p>\n<p><figure id=\"attachment_44484\" aria-describedby=\"caption-attachment-44484\" style=\"width: 718px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/04\/junit-test-constructor-example-setup-2.jpg\"><img decoding=\"async\" class=\"size-full wp-image-44484\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/04\/junit-test-constructor-example-setup-2.jpg\" alt=\"JUnit Test Constructor Example Setup 2\" width=\"718\" height=\"662\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/04\/junit-test-constructor-example-setup-2.jpg 718w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/04\/junit-test-constructor-example-setup-2-300x277.jpg 300w\" sizes=\"(max-width: 718px) 100vw, 718px\" \/><\/a><figcaption id=\"caption-attachment-44484\" class=\"wp-caption-text\">Figure 2: JUnit Test Constructor Example Setup 2<\/figcaption><\/figure><\/p>\n<p>This will create a blank Maven project. You are now ready to start coding the example.<br \/>\n<span id=\"code\"><\/span><\/p>\n<h2>4. JUnit Test Constructor Example<\/h2>\n<p>Let&#8217;s start creating classes our example.<\/p>\n<p>First of all, open <code>pom.xml<\/code> file and copy and paste below line into it.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>pom.xml<\/em><\/span><\/p>\n<pre class=\"brush:xml;wrap-lines:false\">    &lt;dependencies&gt;\n        &lt;dependency&gt;\n            &lt;groupId&gt;junit&lt;\/groupId&gt;\n            &lt;artifactId&gt;junit&lt;\/artifactId&gt;\n            &lt;version&gt;4.12&lt;\/version&gt;\n        &lt;\/dependency&gt;\n    &lt;\/dependencies&gt;\n\n    &lt;properties&gt;\n        &lt;maven.compiler.source&gt;1.8&lt;\/maven.compiler.source&gt;\n        &lt;maven.compiler.target&gt;1.8&lt;\/maven.compiler.target&gt;\n    &lt;\/properties&gt;\n<\/pre>\n<p>Here, we have ask Maven to pull <strong>JUnit 4.12<\/strong> dependency and use <strong>Java 1.8<\/strong> for compiling our example.<\/p>\n<h3>4.1 Java Classes<\/h3>\n<p>Start by create a model class that will help in testing our example. Here we are creating a simple class called <code>Calculator<\/code>.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Calculator.java<\/em><\/span><\/p>\n<pre class=\"brush:java;wrap-lines:false\">package junittestconstructor;\n\npublic class Calculator {\n\n    private int numA;\n    private int numB;\n    \n    Calculator(int numA,int numB){\n        this.numA = numA;\n        this.numB = numB;\n        if(numA&lt;0 || numB&lt;0){\n            throw new IllegalArgumentException();\n        }\n    }\n    \n    public int sum(){\n        return this.numA + this.numB;\n    }\n}\n<\/pre>\n<p>As we can see, this class 2 variables <code>numA<\/code> and <code>numB<\/code>. One constructor which is also throwing <code>IllegalArgumentException()<\/code>.[ulp id=&#8217;ODQaBEw1BIbHApZq&#8217;]<\/p>\n<p>We will test both scenarios where we will pass positive as well as negative values. We will also see <strong>how we can handle the exceptions<\/strong> in test class so that test cases don&#8217;t fail.<\/p>\n<p>Now, we will create a test class that will test our constructor.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>CalculatorTest.java<\/em><\/span><\/p>\n<pre class=\"brush:java;wrap-lines:false;highlight:[14,18,20]\">package junittestconstructor;\n\nimport static org.hamcrest.CoreMatchers.is;\nimport static org.junit.Assert.assertThat;\n\nimport org.junit.Test;\n\npublic class CalculatorTest {\n\n    private Calculator c;\n    \n    @Test\n    public void constructorTest(){\n        c = new Calculator(4, 5);\n        assertThat(9, is(c.sum()));\n    }\n    \n    @Test(expected = IllegalArgumentException.class)\n    public void constructorExceptionTest(){\n        c = new Calculator(-4, 5);\n    }\n    \n}\n<\/pre>\n<p>Here we can see that at <strong>line no 14<\/strong> we are testing with the positive values and at <strong>line no 20<\/strong> we are testing negative scenario.<\/p>\n<p>See how we have handled the exception. If we do not handle it, this method will fail. To comply standards and to test our case to pass, we need to catch the exception. We have done by passing the arguments to the <code>@Test<\/code> annotation. See <strong>line no 18<\/strong>.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Output<\/em><\/span><br \/>\nHere is the output seen in Eclipse JUnit window.<\/p>\n<p><figure id=\"attachment_44486\" aria-describedby=\"caption-attachment-44486\" style=\"width: 534px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/04\/junit-test-constructor-example-output.jpg\"><img decoding=\"async\" class=\"size-full wp-image-44486\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/04\/junit-test-constructor-example-output.jpg\" alt=\"JUnit Test Constructor Example Output\" width=\"534\" height=\"234\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/04\/junit-test-constructor-example-output.jpg 534w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/04\/junit-test-constructor-example-output-300x131.jpg 300w\" sizes=\"(max-width: 534px) 100vw, 534px\" \/><\/a><figcaption id=\"caption-attachment-44486\" class=\"wp-caption-text\">Figure 3: JUnit Test Constructor Example Output<\/figcaption><\/figure><\/p>\n<h2>5. Conclusion<\/h2>\n<p>In this example, we have learned that constructor testing is way similar to the other method testing. We can use <code>@Before<\/code> method also for initializing other than the constructor. Constructor will not give details about the exceptions clearly, but <code>@Before<\/code> does.<\/p>\n<h2>6. Download the Eclipse Project<\/h2>\n<p>This is JUnit Test Constructor Example.<\/p>\n<div class=\"download\"><strong>Download<\/strong><br \/>\nYou can download the full source code of this example here: <a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/04\/junittestconstructor.zip\"><strong>JUnitTestConstructorExample.zip<\/strong><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In JUnit Test Constructor Example, we shall show you the process of testing the constructor of a class. It is as simple as we are testing other methods. Since the constructor is called before any other methods of the class we need to see the relevance of testing it. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &hellip;<\/p>\n","protected":false},"author":112,"featured_media":6678,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[],"class_list":["post-44481","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-junit"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>JUnit Test Constructor Example - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"In JUnit Test Constructor Example, we shall show you the process of testing the constructor of a class. It is as simple as we are testing other methods.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-test-constructor-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JUnit Test Constructor Example - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"In JUnit Test Constructor Example, we shall show you the process of testing the constructor of a class. It is as simple as we are testing other methods.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-test-constructor-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Examples Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2017-04-12T08:00:02+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-03-20T11:59:24+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/junit-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Vinod Kumar Kashyap\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vinodkashyap\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Vinod Kumar Kashyap\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-test-constructor-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-test-constructor-example\/\"},\"author\":{\"name\":\"Vinod Kumar Kashyap\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/1d6281e8235e49b5b8614621c2445d2c\"},\"headline\":\"JUnit Test Constructor Example\",\"datePublished\":\"2017-04-12T08:00:02+00:00\",\"dateModified\":\"2019-03-20T11:59:24+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-test-constructor-example\/\"},\"wordCount\":640,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-test-constructor-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/junit-logo.jpg\",\"articleSection\":[\"junit\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-test-constructor-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-test-constructor-example\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-test-constructor-example\/\",\"name\":\"JUnit Test Constructor Example - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-test-constructor-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-test-constructor-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/junit-logo.jpg\",\"datePublished\":\"2017-04-12T08:00:02+00:00\",\"dateModified\":\"2019-03-20T11:59:24+00:00\",\"description\":\"In JUnit Test Constructor Example, we shall show you the process of testing the constructor of a class. It is as simple as we are testing other methods.\",\"breadcrumb\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-test-constructor-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-test-constructor-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-test-constructor-example\/#primaryimage\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/junit-logo.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/junit-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-test-constructor-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/examples.javacodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java Development\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Core Java\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"junit\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/junit\/\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"JUnit Test Constructor Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Examples and Code Snippets\",\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/javacodegeeks\",\"https:\/\/x.com\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/1d6281e8235e49b5b8614621c2445d2c\",\"name\":\"Vinod Kumar Kashyap\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/a11634ec460c849e8631b740af1b75cb68c9c58d5c2339c21bcd95f55b9af4f5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/a11634ec460c849e8631b740af1b75cb68c9c58d5c2339c21bcd95f55b9af4f5?s=96&d=mm&r=g\",\"caption\":\"Vinod Kumar Kashyap\"},\"description\":\"Vinod is Sun Certified and love to work in Java and related technologies. Having more than 13 years of experience, he had developed software's including technologies like Java, Hibernate, Struts, Spring, HTML 5, jQuery, CSS, Web Services, MongoDB, AngularJS, AWS. He is also a JUG Leader of Chandigarh Java User Group.\",\"sameAs\":[\"http:\/\/www.vinodkashyap.com\/\",\"https:\/\/www.instagram.com\/vinodkashyap\/\",\"https:\/\/in.linkedin.com\/in\/vinodkashyap\",\"https:\/\/in.pinterest.com\/vinodkashyap\/\",\"https:\/\/x.com\/vinodkashyap\"],\"url\":\"https:\/\/examples.javacodegeeks.com\/author\/vinod-kashyap\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JUnit Test Constructor Example - Java Code Geeks","description":"In JUnit Test Constructor Example, we shall show you the process of testing the constructor of a class. It is as simple as we are testing other methods.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-test-constructor-example\/","og_locale":"en_US","og_type":"article","og_title":"JUnit Test Constructor Example - Java Code Geeks","og_description":"In JUnit Test Constructor Example, we shall show you the process of testing the constructor of a class. It is as simple as we are testing other methods.","og_url":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-test-constructor-example\/","og_site_name":"Examples Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2017-04-12T08:00:02+00:00","article_modified_time":"2019-03-20T11:59:24+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/junit-logo.jpg","type":"image\/jpeg"}],"author":"Vinod Kumar Kashyap","twitter_card":"summary_large_image","twitter_creator":"@vinodkashyap","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Vinod Kumar Kashyap","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-test-constructor-example\/#article","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-test-constructor-example\/"},"author":{"name":"Vinod Kumar Kashyap","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/1d6281e8235e49b5b8614621c2445d2c"},"headline":"JUnit Test Constructor Example","datePublished":"2017-04-12T08:00:02+00:00","dateModified":"2019-03-20T11:59:24+00:00","mainEntityOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-test-constructor-example\/"},"wordCount":640,"commentCount":1,"publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-test-constructor-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/junit-logo.jpg","articleSection":["junit"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-test-constructor-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-test-constructor-example\/","url":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-test-constructor-example\/","name":"JUnit Test Constructor Example - Java Code Geeks","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-test-constructor-example\/#primaryimage"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-test-constructor-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/junit-logo.jpg","datePublished":"2017-04-12T08:00:02+00:00","dateModified":"2019-03-20T11:59:24+00:00","description":"In JUnit Test Constructor Example, we shall show you the process of testing the constructor of a class. It is as simple as we are testing other methods.","breadcrumb":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-test-constructor-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-test-constructor-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-test-constructor-example\/#primaryimage","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/junit-logo.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/junit-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-test-constructor-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/examples.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java Development","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/"},{"@type":"ListItem","position":3,"name":"Core Java","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/"},{"@type":"ListItem","position":4,"name":"junit","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/junit\/"},{"@type":"ListItem","position":5,"name":"JUnit Test Constructor Example"}]},{"@type":"WebSite","@id":"https:\/\/examples.javacodegeeks.com\/#website","url":"https:\/\/examples.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/examples.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/examples.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/1d6281e8235e49b5b8614621c2445d2c","name":"Vinod Kumar Kashyap","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/a11634ec460c849e8631b740af1b75cb68c9c58d5c2339c21bcd95f55b9af4f5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/a11634ec460c849e8631b740af1b75cb68c9c58d5c2339c21bcd95f55b9af4f5?s=96&d=mm&r=g","caption":"Vinod Kumar Kashyap"},"description":"Vinod is Sun Certified and love to work in Java and related technologies. Having more than 13 years of experience, he had developed software's including technologies like Java, Hibernate, Struts, Spring, HTML 5, jQuery, CSS, Web Services, MongoDB, AngularJS, AWS. He is also a JUG Leader of Chandigarh Java User Group.","sameAs":["http:\/\/www.vinodkashyap.com\/","https:\/\/www.instagram.com\/vinodkashyap\/","https:\/\/in.linkedin.com\/in\/vinodkashyap","https:\/\/in.pinterest.com\/vinodkashyap\/","https:\/\/x.com\/vinodkashyap"],"url":"https:\/\/examples.javacodegeeks.com\/author\/vinod-kashyap\/"}]}},"_links":{"self":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/44481","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/users\/112"}],"replies":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=44481"}],"version-history":[{"count":0,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/44481\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media\/6678"}],"wp:attachment":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=44481"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=44481"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=44481"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}