JUnit coverage showing red on MockitoAnnotations - java

I have a Junit test class. When I ran test coverage in eclipse, I see all the test methods and the actual class is showing Green as covered but only the setUp method is showing in Red and also the Sonar test report is showing coverage as 0%. The same setUp() method is showing Green in other classes and also showing covered in Sonar. I am not sure what I am missing here.
#ExtendWith(MockitoExtension.class)
public class AddServiceImplTest {
#InjectMocks
private AddServiceImpl addServiceImpl;
#Mock
private OtherClass otherClass;
#Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
#Test
public void test1() {
....
}

Related

Is there a TestNG annotation that start running before the class constructor just like JUnit #BeforeAll does?

The JUnit #BeforeAll tests are executed before the constructor and the declared class variables (as they should).
The TestNG #BeforeClass calls first the class constructor and classes variables before it runs itself.
Is there a TestNG annotation that starts to run BEFORE the class constructor is called,
just like JUnit #BeforeAll does?
I run a test with both TestNG #BeforeClass and JUnit #BeforeAll and they both give different responses.
JUnit example:
package Junit;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import pages.MyClass;
public class TestJunit {
#BeforeAll
public static void setUp(){
System.out.println("1 - #BeforeAll Junit");
}
private MyClass str = new MyClass();
public TestJunit() {
System.out.println("3 - Junit Class Constructor");
}
#Test
public void test1(){
System.out.println("4 - Starting Junit Tests");
}
}
TestJunit - Junit Response:
#BeforeAll Junit
My Custom Class Constructor
Junit Class Constructor
Starting Junit Tests
TestNG example:
package TestNG;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import pages.MyClass;
public class TestTestNG {
#BeforeClass(alwaysRun = true)
public void setUp(){
System.out.println("1 - BeforeAll TestNG");
}
private MyClass str = new MyClass();
public TestTestNG() {
System.out.println("3 - TestNG Class Constructor");
}
#Test
public void test1(){
System.out.println("4 - Starting TestNG Tests");
}
}
TestTestNG - TestNG Response:
My Custom Class Constructor
TestNG Class Constructor
BeforeAll TestNG
Starting TestNG Tests
My Custom Class:
package pages;
public class MyClass {
public MyClass() {
System.out.println("2 - My Custom Class Constructor");
}
}
I want a TestNG solution (because #BeforeClass is not working) that will give the same response as the JUnit (#BeforeAll) solution.
Is there a TestNG annotation that starts to run BEFORE the class constructor is called,
just like JUnit #BeforeAll does?
Yes there is. You can use the #BeforeTest or the top level #BeforeSuite annotations to use as a "BeforeAll" alternative. Both of the will run before the test class constructors.
Following is the TestNG annotation execution order so you get the idea.

Run method before each test

Im using testng 6.11 and writing tests in the following test class:
public class MyTest{
public int i;
public void init(){
//initialize i
}
#Test
public void test1(){
//test some
}
#Test
public void test2(){
//Here I need fresh value of i as it would be
//right after invocation of init()
//...
//test something else
}
}
Is it possible to make testng run init() method before invocation of each test in a test class?
Annotate init() with #BeforeMethod annotation. See http://testng.org/doc/documentation-main.html#annotations
Sure, your can use the annotation for that
#BeforeTest: The annotated method will be run before any test method belonging to the classes inside the tag is run.
You can use #BeforeMethod annotatation to execute any method before every test.
Example

Mockito test failing to initialize

This fails with InitializationError. Other tests in the same package run so I have done something silly in my code. Stacktrace reads "No tests found matching [[Exactmatch]]".
public class TestClassToTest {
#Mock
File mockOfAFile;
#Test
public void testAMethod(File mockOfAFile) {
MockitoAnnotations.initMocks(this);
given(fileMock.getName()).willReturn("test1");
assertEquals("test1",ClassBeingTested.methodBeingTested(mockOfAFile));
}
}
Have tried everything but am very new to Mockito. What silly thing am I doing here ?
Thanks
I found two things to fix:
The #Test method should have no parameters
You need another File instance, called fileMock.
So here is the updated code:
public class TestClassToTest {
#Mock
File mockOfAFile;
#Mock
File fileMock; // the new mock
#Test
public void testAMethod() { // no parameters
MockitoAnnotations.initMocks(this);
given(fileMock.getName()).willReturn("test1"); // here is the new mock used
assertEquals("test1",ClassBeingTested.methodBeingTested(mockOfAFile));
}
}

#BeforeClass annotation is not working in dynamically created Test Suite

I have few JUnit Tests and I want to decide which one to use at runtime. I checked previous answers at SO and I ended up creating Test Suite dynamically.
This class is where my application starts. I have CustomTestSuite class and Main class adds Tests to my custom suite.
public class Main {
public static junit.framework.TestSuite suite()
{
CustomTestSuite suite = new CustomTestSuite();
suite.addTest(new JUnit4TestAdapter(BTest.class));
suite.addTest(new JUnit4TestAdapter(ATest.class));
return suite;
}
}
CustomTestSuite.java
public class CustomTestSuite extends TestSuite {
#BeforeClass
public static void setUp() throws Exception {
System.out.println("Before class test");
}
#After
public void tearDown() throws Exception {
System.out.println("After class test");
}
}
My ATest and BTest are simple Test classes, I will just show ATest as sample:
public class ATest{
#Test
public void testMethod() {
System.out.println("testMethod");
}
}
When I start running my project from Main class, it is expected to run the method with #BeforeClass first, do testing, and then run the method with #AfterClass annotation.
Tests are working fine but it skips setUp method and tearDown method. I tried #Before and #BeforeClass annotations both.
I am confused with suite structure. Any help would be appreciated.
Thanks
#Before and #BeforeClass are supposed to be used in Test class not in TestSuite. If need to have common setUp and tearDown for more than one Test class, then put those both methods in a super class and extend that super by ATest and BTest test classes. And also the Suite can be built and run simply with #RunWith and #SuiteClasses annotations and the CustomTestSuite class is not needed.
So the changes are as below.
The CustomTestSuite becomes TestSuper
public class TestSuper {
#BeforeClass
public static void setUp() throws Exception {
System.out.println("Before class test");
}
#After
public void tearDown() throws Exception {
System.out.println("After class test");
}
}
Now the ATest extends TestSuper
public class ATest extends TestSuper {
#Test
public void testMethod() {
System.out.println("testMethod");
}
}
Similarly BTest also should extend TestSuper.
Simply add #RunWith and #SuiteClasses annotations to Main class as below and run Main.
#RunWith(Suite.class)
#SuiteClasses({ATest.class, BTest.class})
public class Main {
}
Have a go with these changes.

Why isn't my #BeforeClass method running?

I have the following code:
#BeforeClass
public static void setUpOnce() throws InterruptedException {
fail("LOL");
}
And various other methods that are either #Before, #After, #Test or #AfterClass methods.
The test doesn't fail on start up as it seems it should. Can someone help me please?
I have JUnit 4.5
The method is failing in an immediate call to setUp() which is annotated as #before.
Class def is :
public class myTests extends TestCase {
do NOT extend TestCase AND use annotations at the same time!
If you need to create a test suite with annotations, use the RunWith annotation like:
#RunWith(Suite.class)
#Suite.SuiteClasses({ MyTests.class, OtherTest.class })
public class AllTests {
// empty
}
public class MyTests { // no extends here
#BeforeClass
public static void setUpOnce() throws InterruptedException {
...
#Test
...
(by convention: class names with uppercase letter)
the method must be static and not directly call fail (otherwise the other methods won't be executed).
The following class shows all the standard JUnit 4 method types:
public class Sample {
#BeforeClass
public static void beforeClass() {
System.out.println("#BeforeClass");
}
#Before
public void before() {
System.out.println("#Before");
}
#Test
public void test() {
System.out.println("#Test");
}
#After
public void after() {
System.out.println("#After");
}
#AfterClass
public static void afterClass() {
System.out.println("#AfterClass");
}
}
and the ouput is (not surprisingly):
#BeforeClass
#Before
#Test
#After
#AfterClass
Make sure you imported #Test from the correct package.
Correct package: org.junit.Test
Incorrect pacakge: org.junit.jupiter.api.Test
Please note that this is a solution for: If your #Before, #Atter, etc did not get called at all.
Make sure that :
Your test class doesn't inherits from TestCase
The #BeforeClass method is static
You don't have more than one #BeforeClass method in test class hierarchy (only the most specialized #BeforeClass method will be executed)
Check your imports.
#Before
#After
#BeforeClass (this should be static)
#AfterClass (this should be static)
and #Test annotations should import from same path.
In order that the before annotated function will run , I had to do the following:
If you use Maven , add a dependency to Junit 4.11+:
<properties>
<version.java>1.7</version.java>
<version.log4j>1.2.13</version.log4j>
<version.mockito>1.9.0</version.mockito>
<version.power-mockito>1.4.12</version.power-mockito>
<version.junit>4.11</version.junit>
<version.power-mockito>1.4.12</version.power-mockito>
</properties>
and the dependency:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${version.junit}</version>
<scope>test</scope>
</dependency>
.
.
.
</dependencies>
Make sure your Junit Test class is not extending The TestCase class, since this will cause overlapping with Older version:
public class TuxedoExceptionMapperTest{
protected TuxedoExceptionMapper subject;
#Before
public void before() throws Exception {
subject = TuxedoExceptionMapper.getInstance();
System.out.println("Start");
MockitoAnnotations.initMocks(this);
}
}

Categories