Menu

Showing posts with label JUNIT. Show all posts
Showing posts with label JUNIT. Show all posts

junit-jupiter failed to discover tests

JUnit Jupiter failed to discover tests classes.

Internal Error occurred.
org.junit.platform.commons.JUnitException: TestEngine with ID 'junit-jupiter' failed to discover tests
	at org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discoverEngineRoot(EngineDiscoveryOrchestrator.java:160)
	at org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discoverSafely(EngineDiscoveryOrchestrator.java:132)
	at org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discover(EngineDiscoveryOrchestrator.java:107)
	at org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discover(EngineDiscoveryOrchestrator.java:78)
	at org.junit.platform.launcher.core.DefaultLauncher.discover(DefaultLauncher.java:110)
	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:86)
	at org.junit.platform.launcher.core.DefaultLauncherSession$DelegatingLauncher.execute(DefaultLauncherSession.java:86)
	at org.junit.platform.launcher.core.SessionPerRequestLauncher.execute(SessionPerRequestLauncher.java:53)
	at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:71)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
	at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Caused by: java.lang.NoClassDefFoundError: org/junit/jupiter/api/io/TempDirFactory
	at org.junit.jupiter.engine.config.DefaultJupiterConfiguration.<clinit>(DefaultJupiterConfiguration.java:62)
	at org.junit.jupiter.engine.JupiterTestEngine.discover(JupiterTestEngine.java:66)
	at org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discoverEngineRoot(EngineDiscoveryOrchestrator.java:152)
	... 13 more
Caused by: java.lang.ClassNotFoundException: org.junit.jupiter.api.io.TempDirFactory
	at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
	... 16 more
Disconnected from the target VM, address: '127.0.0.1:57935', transport: 'socket'

Process finished with exit code -2

TestEngine with ID junit-jupiter failed to discover tests

 

Internal Error occurred.
org.junit.platform.commons.JUnitException: TestEngine with ID 'junit-jupiter' failed to discover tests
	at org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discoverEngineRoot(EngineDiscoveryOrchestrator.java:160)
	at org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discoverSafely(EngineDiscoveryOrchestrator.java:132)
	at org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discover(EngineDiscoveryOrchestrator.java:107)
	at org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discover(EngineDiscoveryOrchestrator.java:78)
	at org.junit.platform.launcher.core.DefaultLauncher.discover(DefaultLauncher.java:110)
	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:86)
	at org.junit.platform.launcher.core.DefaultLauncherSession$DelegatingLauncher.execute(DefaultLauncherSession.java:86)
	at org.junit.platform.launcher.core.SessionPerRequestLauncher.execute(SessionPerRequestLauncher.java:53)
	at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:71)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
	at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Caused by: java.lang.NoClassDefFoundError: org/junit/jupiter/api/io/TempDirFactory
	at org.junit.jupiter.engine.config.DefaultJupiterConfiguration.<clinit>(DefaultJupiterConfiguration.java:62)
	at org.junit.jupiter.engine.JupiterTestEngine.discover(JupiterTestEngine.java:66)
	at org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discoverEngineRoot(EngineDiscoveryOrchestrator.java:152)
	... 13 more
Caused by: java.lang.ClassNotFoundException: org.junit.jupiter.api.io.TempDirFactory
	at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
	... 16 more
Disconnected from the target VM, address: '127.0.0.1:57935', transport: 'socket'

Process finished with exit code -2

JUnit for AEM sling model class with adaptables SlingHttpServletRequest

Sling Model class

We have this sling model class for that we are going to write unit test using JUnit5.


@Model(adaptables = SlingHttpServletRequest.class, 
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class LinkCheckerModel {
@Inject
@Named("link")
@Source("request-attributes")
private String link;

@Inject
@Source("sling-object")
private ResourceResolver resolver;

private boolean isExternal;

private String updatedLink;
    private final static String DOT_HTML = ".html";
private final Logger logger = LoggerFactory.getLogger(getClass());

@PostConstruct
public void init() {
try {
isExternal = false;
if(StringUtils.isNotBlank(link)) {
if (!isInternal(link)) {
isExternal = true;
updatedLink = link;
} else {
link = link.contains(DOT_HTML) ? link : (link + DOT_HTML);
updatedLink = resolver.map(link);
}
}
} catch (Exception e) {
logger.error("Exception occurs while checking link:{}", e.getMessage());
}
logger.info("Exit init method of Linkchecker Model");
}

private boolean isInternal(String linkStr) {
return linkStr.startsWith("/content") && !linkStr.startsWith("/content/dam");
}
public boolean isExternal() {
return isExternal;
}
public String getUpdatedLink() {
return updatedLink;
}
}

Test class

Generate or create the test class of our sling model class. In this case we have class LinkCheckerModel and created Test class LinkCheckerModelTest.java.

Note: Right click on the class name and choose the option to generate the test. Then select the methods for that you want to create test methods.


import io.wcm.testing.mock.aem.junit5.AemContext;
import io.wcm.testing.mock.aem.junit5.AemContextExtension;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;

import static org.junit.jupiter.api.Assertions.*;

@ExtendWith(AemContextExtension.class)
class LinkCheckerModelTest {
private AemContext context = new AemContext();
@Mock
private SlingHttpServletRequest request;
LinkCheckerModel linkCheckerModel = new LinkCheckerModel();
@BeforeEach
void setUp() {
context.load().json("/linkChecker.json", "/content/jorvee/en");
context.addModelsForClasses(LinkCheckerModel.class);
Resource resource = context.resourceResolver().
getResource("/content/jorvee/en/jcr:content/parsys/linkCheckerNode");
        linkCheckerModel = context.request().adaptTo(LinkCheckerModel.class);

linkCheckerModel.init();
}

@Test
void initTest() {

}

@Test
void isExternalTest() {
assertNotNull(linkCheckerModel.isExternal());
}

@Test
void getUpdatedLinkTest() {
assertNull(linkCheckerModel.getUpdatedLink());
}
}


JSON data [linkChecker.json]

The JSON data on which we will mock the request. Create a JSON file with name linkChecker.json and add this json object into that.

{
"jcr:primaryType": "cq:Page",
"jcr:content": {
"cq:contextHubPath": "/etc/cloudsettings/default/contexthub",
"cq:contextHubSegmentsPath": "/etc/segmentation/contexthub",
"cq:designPath": "/etc/designs/jorvee",
"cq:lastModified": "{Date}2020-04-14T19:23:41.457+05:30",
"cq:lastModifiedBy": "admin",
"cq:lastReplicated": "{Date}2020-02-10T19:55:14.981Z",
"cq:lastReplicatedBy": "admin",
"cq:lastReplicationAction": "Activate",
"cq:template": "/apps/jorvee/templates/basic",
"jcr:primaryType": "cq:PageContent",
"jcr:title": "Jorvee | Homepage",
"releaseDate": "Wed Apr 01 2020 00:00:00 GMT+0530",
"sling:resourceType": "jorvee/components/pages/basic/v1/basic",
"section": "sect",
"parsys": {
"jcr:primaryType": "nt:unstructured",
"sling:resourceType": "wcm/foundation/components/parsys",
"linkCheckerNode": {
"jcr:primaryType": "nt:unstructured",
"sling:resourceType": "jorvee/components/buttons/linkChecker",
"link": "/content/jorvee"
}
}
}
}

Run the test

You are all set and now it's time to test the class and methods. You can run the testclass from run option of the IDE. also you can run the test calss using maven command for just a single class.

mvn test -Dtest=<className>

e.g. mvn test -Dtest=LinkCheckerModelTest


Junit Tutorials