Junits- Common Issues To execute and solutions

Junits can be run from console or through eclipse. However, running from console can be tiresome since it would require a build and server start for every small code change. That is why, it is preferred to get junits running through eclipse. That is a tough thing to do sometimes. Following steps will show how some of the common issues can be resolved :

  1. If a testsrc folder is already created in your package/extension, great ! . If not, we will create a testsrc folder, in which by hierarchy, we will create our test file. Put minimal values such as these in this file :

package com.test.service;

import de.hybris.bootstrap.annotations.UnitTest;

import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.MockitoAnnotations;

import com.test.StorageServiceImpl;
@UnitTest
public class StorageServiceImplTest
{

@InjectMocks
private StorageServiceImpl storageServiceImpl;

@Before
public void SetUp()
{

MockitoAnnotations.initMocks(this);
}

@Test
public void updateSubscription()
{
System.out.println(“hello”);

}
}

NOTE : remember to give package.

2) Clean eclipse from Project -> Clean -> Clean all projects (no need to build all)

3) Right click on extension to go to properties -> Java build path  and ensure the following things :

  • a) Ensure Junit 4 is present as a library.
  • b) Ensure JRE system library[jdk1.8.0_101] is present as a library.
  • c) Ensure testsrc is present in build path.

Click on apply.

4) Right click on the file, Run as -> run configurations -> click on new icon for junit and enter name of file,

a) Ensure test runner is Junit 4

b) Ensure to add testsrc in source.

c) Ensure testsrc is present in classpath.

d) Make sure there is no red sign above test tab . Unless it is there, it means that system is not able to find file. Click on search in front of test class to make sure that this class is found. Click on apply once that red sign goes and click run.

e ) EDIT : An addition, which is most helpful at all times, is to right click on the test file, Run As -> Run configurations -> Click on classpath tab -> select the folder name(it will be the xtension you are working in) -> click on advanced. -> Add folders -> Add testsrc -> testclasses, classes folder from your extension. The idea is to make sure that the class file of your test and class file of your file to be tested both are present in classpath.

 

File should run now. Happy testing, developer style 😉

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s