I'm new at JUnit and use inteliji idea.
import org.junit.Test;
import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import java.util.Date;
import static org.junit.Assert.*;
public class TestQuote {
#Test
public void testQuote() {
Date date = new Date(System.currentTimeMillis());
Quote quote=new Quote("a",date,200.0,300.0,100.0,107.0,1.0);
assertNull("Object is null",quote);
assertEquals("Symbol is ok",quote.getSymbol(),"a");
assertEquals("Date is ok",quote.getDate(),System.currentTimeMillis());
assertEquals("Open price is ok",quote.getOpenPrice(),200.0);
assertEquals("High price is ok",quote.getHighPrice(),300.0);
assertEquals("Low price is ok",quote.getLowPrice(),100.0);
assertEquals("Close price is ok",quote.getClosePrice(),107.0);
}
}
Here is code of my test class. JUnit.jar is added to classpath but when i run it it says:
java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
Any sollutions?
BTW main program work OK.
Go to the JUnit web site: http://junit.org/
Then click on "Download and install Guide" : https://github.com/junit-team/junit/wiki/Download-and-Install
Then read:
Download the following JARs and put them on your test classpath:
junit.jar
hamcrest-core.jar
Related
I am trying to run a spring developed web app and I'm getting the following error.
My folder structure is as follows.
Here is my PersonRepositary.java code which is inside the repositary folder.
package com.travelx.travelx.repositary;
import org.springframework.data.repository.CrudRepository;
import com.travelx.travelx.models.Person;
public interface PersonRepositary extends CrudRepository<Person, Integer> {
}
The RegisterController.java file which is in the controllers folder is ac follows.
package com.travelx.travelx.controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.travelx.travelx.models.Person;
import com.travelx.travelx.repositary.PersonRepositary;
#RestController
#RequestMapping("register")
public class RegisterController {
#Autowired
private PersonRepositary personRepositary;
#PostMapping("login")
public String registerPerson(#RequestBody Person person) {
personRepositary.save(person);
return "You are Registered!";
}
}
And the TravelXApplication.java file which is in the controllers is below.
package com.travelx.travelx.controllers;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
#SpringBootApplication
#ComponentScan
#EntityScan
#EnableJpaRepositories
public class TravelxApplication {
public static void main(String[] args) {
SpringApplication.run(TravelxApplication.class, args);
}
}
I'm trying to make a web page where a person can register to a site. Here, I'm using xampp as my platform to handle the back end. As shown in the image, the controllers, repositories and and models are implemented in separate folders. I'm new to Spring. So no matter how hard I to find what the problem is, I cant seem to find it. Can some one help me please?
--------------UPDATE------------------
I've moved my TravelXApplication.java to the com.travelx.travelx and now this error is gone.Spring works fine. However when I open my form, insert data and try to save it, the browser gives me the following error.
How do I solve it?
Your PersonRepositary is not registered as a bean in your Spring context. In practice, this means that Spring is not be able to inject it in your RegisterController.
I suspect that #EnableJpaRepositories, #EntityScan and #ComponentScan are unnecessary in your main application class and are actually causing Spring automatic configuration to be overridden. Try deleting these three annotations from TravelxApplication.
Here's the answer to why it should still work without annotations.
Update: just noticed that your TravelxApplication is located in the controllers package, but then it won't have visibility to your repository. Make sure to move your main class to the com.travelx.travelx package.
Below is the Test Runner File
package runner;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
#RunWith(Cucumber.class)
#CucumberOptions(features="D:/InstalledSoftwares/Eclipse_Photon/workspace/March26/src/test/resources/features/login.Feature",
glue= {"D:/InstalledSoftwares/Eclipse_Photon/workspace/March26/src/test/java/stepDefinitions/LoginSteps.java"})
public class TestRunner {
}
Below is the Feature File-
Feature: Create Account on Facebook
Scenario: Check First Name
Given: User is already on Login Page
When: Enter First Name
And: Enter Last Name
Then: Check if value of First Name is there
Below is the Step Definition-
package stepDefinitions;
import cucumber.api.java.en.Given;
public class LoginSteps {
#Given("^User is already on Login Page$")
public void User_is_already_on_Login_Page()
{
}
}
Below is the Snapshot of Eclipse.
This is how my Project structures look like
I have configured Cucumber Maven project and getting Initialization error while executing my RunTest.java file:
package annotation;
import org.junit.runner.RunWith;
import cucumber.junit.Cucumber;
#RunWith(Cucumber.class)
#Cucumber.Options(format = {"pretty", "html:target/cucumber"})
public class RunTest { }
I had included hamcrest-library files too but still I don't know what I have left out.
Use the below code instead...
package annotation;
import org.junit.runner.RunWith;
import cucumber.api.junit.Cucumber;
import cucumber.api.CucumberOptions;
#RunWith(Cucumber.class)
#CucumberOptions(plugin = { "pretty", "html:target/cucumber" })
public class RunTest {
}
Two import statements were wrong. Cucumber.Options was not correct it is CucumberOptions. format is now deprecated, use plugin.
I am running a server under WebSphere Application Server (17.0.0.1/wlp-1.0.16.cl170120170227-0220) and have added the changes recommended in this post (How to change Jackson version in JAX-RS app (WebSphere Liberty)) for upgrading the level of Jackson in WAS Liberty. I am using Postman to test my server. When I submit a GET request for an object, it completes successfully. (After adding this change, my server can return my objects in either XML or JSON.). However, I am now seeing these messages in the server console when my server builds the Response object.
[INFO ] FFDC1015I: An FFDC Incident has been created: "org.jboss.weld.exceptions.AmbiguousResolutionException: WELD-001318: Cannot resolve an ambiguous dependency between:
- Managed Bean [class com.ibm.zss.boundary.JaxbJsonProvider] with qualifiers [#Any #Default],
- Managed Bean [class com.ibm.zss.boundary.JsonProvider] with qualifiers [#Any #Default] com.ibm.ws.jaxrs20.cdi.component.JaxRsFactoryImplicitBeanCDICustomizer 425" at ffdc_17.06.13_15.59.57.0.log
com.ibm.zss.boundary.JaxbJsonProvider and com.ibm.zss.boundary.JsonProvider are the classes I added based on the instructions from the previous post.
I also updated my server.xml to include:
<feature>jsonp-1.0</feature>
<feature>jaxrs-2.0</feature>
I've been searching for solutions for handling a WELD ambiguousResolutionException, but most of them address issues with classes where the developer has control over what is being injected. So, I don't know if I have any control over the code that I need to change for this problem.
For completeness, here are the classes which I added to my application:
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.Provider;
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
#Provider
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public class JaxbJsonProvider extends JacksonJaxbJsonProvider {
}
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.Provider;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
#Provider
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public class JsonProvider extends JacksonJsonProvider {
public JsonProvider() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(objectMapper.getVisibilityChecker().withFieldVisibility(JsonAutoDetect.Visibility.ANY));
setMapper(objectMapper);
}
}
Can you either mark it an #Specializes or an #Alternative with low #Priority, depending on whether you want it to be used for injection points?
I am trying to add JUnit tests to a Tomee web-application that uses CDI and JPA. Leaving out the different hurdles of the last two weeks (including very intensive research on stackoverflow as well as other sources), my problem appears to be quite concrete:
Running the unit test gives this error message:
org.apache.openejb.Injector$NoInjectionMetaDataException:
org.demo.service.GenericServiceTest : Annotate the class with #javax.annotation.ManagedBean so it can be discovered in the application scanning process
at org.apache.openejb.Injector.inject(Injector.java:54)
at org.apache.openejb.OpenEjbContainer$GlobalContext.bind(OpenEjbContainer.java:656)
...
This I don't understand, for I have annotated the class just as required. Any idea what I can do to resolve this?
(Or does the error refer to the injected class - GenericService? I cannot annotate this #ManagedBean, for it is already #Stateless).
Here are some details of my project:
gradle dependencies:
dependencies {
compile 'org.apache.commons:commons-lang3:3.3.2'
compile "com.googlecode.json-simple:json-simple:1.1.1"
compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.8.6'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.8.6'
compile 'log4j:log4j:1.2.16'
testCompile "org.apache.openejb:tomee-embedded:1.7.3"
compile "javax:javaee-api:7.0"
compile 'mysql:mysql-connector-java:5.1.16'
}
(I have put the testCompile inbetween, because there is a ticket stating the importance of the order: EJB testing with TomEE embedded EJBContainer api: java.lang.ClassFormatError exception)
My Test class is this:
package org.demo.service;
import java.io.File;
import java.util.Properties;
import javax.annotation.ManagedBean;
import javax.ejb.embeddable.EJBContainer;
import javax.inject.Inject;
import javax.naming.NamingException;
import org.apache.openejb.OpenEjbContainer;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
#ManagedBean
public class GenericServiceTest {
private static EJBContainer ejbContainer;
#Inject
private GenericService service;
#Test
public void test() throws NamingException {
System.out.println("service: " + service);
}
#BeforeClass
public static void start() {
Properties p = new Properties();
try {
p.put(EJBContainer.MODULES, new File("bin"));
} catch (Exception e1) {
e1.printStackTrace();
}
p.put(OpenEjbContainer.Provider.OPENEJB_ADDITIONNAL_CALLERS_KEY, GenericService.class.getName());
ejbContainer = javax.ejb.embeddable.EJBContainer.createEJBContainer(p);
}
#Before
public void inject() throws NamingException {
ejbContainer.getContext().bind("inject", this);
}
#AfterClass
public static void shutdownContainer() {
if (ejbContainer != null) {
ejbContainer.close();
}
}
}
Maybe I'm running totally in the wrong direction here - Please let me know if I should choose a different approach - All I want is to add unit tests to my web application (preferably without introducing Weld/JBoss or other implementations as alternatives to the implementations I already use in the application itself).
Thank you very much in advance!
I would recommand you to read http://tomee.apache.org/developer/testing/index.html , there are some examples
Regarding your test it uses openejb embedded and not tomee embedded and deploys bin/ as an application. the hack bind("inject") only works with classpath deployment (no module).