#RunWith(PowerMockRunner.class) is disallowed for this location - junit

I am getting following error
The annotation #RunWith is disallowed for this location
I have following imports.
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
#RunWith(PowerMockRunner.class)
#PrepareForTest(QueryValidator.class)
#Test
public void testValidateIds2() { ... }
What is causing this and how can I get rid of this.

figured #RunWith(PowerMockRunner.class) is a class level annotation and needs to go on top of the class, not the method.

Related

Field personRepositary in () required a bean of type () that could not be found

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.

Initialization Error for Junit in Eclipse

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.

Apache Tika how to extract html body with out header and footer content

I am looking to extract entire body content of html except header and footer, however I am getting exception
org.xml.sax.SAXException: Namespace http://www.w3.org/1999/xhtml not declared
Below is my code that i have created as mentioned at
import org.apache.tika.exception.TikaException;
import org.apache.tika.io.TikaInputStream;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.sax.BodyContentHandler;
import org.apache.tika.sax.ToHTMLContentHandler;
import org.apache.tika.sax.ToXMLContentHandler;
import org.apache.tika.sax.XHTMLContentHandler;
import org.apache.tika.sax.xpath.Matcher;
import org.apache.tika.sax.xpath.MatchingContentHandler;
import org.apache.tika.sax.xpath.XPathParser;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.apache.tika.metadata.Metadata;
import java.io.File;
//import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class NewtikaXpath {
public static void main(String args[]) throws IOException, SAXException, TikaException {
XPathParser xhtmlParser = new XPathParser("xhtml", XHTMLContentHandler.XHTML);
Matcher divContentMatcher = xhtmlParser.parse("/xhtml:html/xhtml:body/xhtml:table/descendant::node()");
ContentHandler xhandler = new MatchingContentHandler(new ToXMLContentHandler(), divContentMatcher);
AutoDetectParser parser = new AutoDetectParser();
//ToHTMLContentHandler htmlhandler = new ToHTMLContentHandler();
//ContentHandler textHandler = new BodyContentHandler();
Metadata xmetadata = new Metadata();
try (InputStream stream = TikaInputStream.get(new File("///httpmoneycnncomnewsworldiidHBNQL1.html"))){
parser.parse(stream, xhandler, xmetadata);
System.out.println("text:\n" + xhandler.toString());
}
}
}
the exception I am getting is
Exception in thread "main" org.xml.sax.SAXException: Namespace http://www.w3.org/1999/xhtml not declared
at org.apache.tika.sax.ToXMLContentHandler$ElementInfo.getPrefix(ToXMLContentHandler.java:62)
at org.apache.tika.sax.ToXMLContentHandler$ElementInfo.getQName(ToXMLContentHandler.java:68)
at org.apache.tika.sax.ToXMLContentHandler.startElement(ToXMLContentHandler.java:148)
at org.apache.tika.sax.ContentHandlerDecorator.startElement(ContentHandlerDecorator.java:126)
at org.apache.tika.sax.xpath.MatchingContentHandler.startElement(MatchingContentHandler.java:60)
at org.apache.tika.sax.ContentHandlerDecorator.startElement(ContentHandlerDecorator.java:126)
at org.apache.tika.sax.SecureContentHandler.startElement(SecureContentHandler.java:250)
at org.apache.tika.sax.ContentHandlerDecorator.startElement(ContentHandlerDecorator.java:126)
at org.apache.tika.sax.ContentHandlerDecorator.startElement(ContentHandlerDecorator.java:126)
at org.apache.tika.sax.ContentHandlerDecorator.startElement(ContentHandlerDecorator.java:126)
at org.apache.tika.sax.SafeContentHandler.startElement(SafeContentHandler.java:264)
at org.apache.tika.sax.XHTMLContentHandler.startElement(XHTMLContentHandler.java:255)
at org.apache.tika.sax.XHTMLContentHandler.startElement(XHTMLContentHandler.java:285)
at org.apache.tika.parser.html.HtmlHandler.startElementWithSafeAttributes(HtmlHandler.java:171)
at org.apache.tika.parser.html.HtmlHandler.startElement(HtmlHandler.java:133)
at org.apache.tika.sax.ContentHandlerDecorator.startElement(ContentHandlerDecorator.java:126)
at org.apache.tika.parser.html.XHTMLDowngradeHandler.startElement(XHTMLDowngradeHandler.java:60)
at org.ccil.cowan.tagsoup.Parser.push(Parser.java:794)
at org.ccil.cowan.tagsoup.Parser.rectify(Parser.java:1061)
at org.ccil.cowan.tagsoup.Parser.stagc(Parser.java:1016)
at org.ccil.cowan.tagsoup.HTMLScanner.scan(HTMLScanner.java:567)
at org.ccil.cowan.tagsoup.Parser.parse(Parser.java:449)
at org.apache.tika.parser.html.HtmlParser.parse(HtmlParser.java:122)
at org.apache.tika.parser.CompositeParser.parse(CompositeParser.java:280)
at org.apache.tika.parser.CompositeParser.parse(CompositeParser.java:280)
at org.apache.tika.parser.AutoDetectParser.parse(AutoDetectParser.java:120)
at org.apache.tika.parser.AutoDetectParser.parse(AutoDetectParser.java:136)
at com.fractal.NewtikaXpath.main(NewtikaXpath.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
While I do understand that as per TIKA-1215 we should not wrap content handlers , I dont see any alternative approach to resolving this issue, as the simple bodycontenthandler is not helping, I verified a lot of stackoverflow cases similar to this but I couldn't find a solution any where. Any advice or solution or pointer is much appreciated.
Foudn a solution at based on research boilerpipe detection and this is integrated with apache tika and can be run with the below java code.
import org.apache.tika.exception.TikaException;
import org.apache.tika.io.TikaInputStream;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.html.BoilerpipeContentHandler;
import org.apache.tika.sax.BodyContentHandler;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.apache.tika.metadata.Metadata;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
public class NewtikaXpath {
public static void main(String args[]) throws IOException, SAXException, TikaException {
AutoDetectParser parser = new AutoDetectParser();
ContentHandler textHandler = new BodyContentHandler();
Metadata xmetadata = new Metadata();
try (InputStream stream = TikaInputStream.get(new URL("your favourite url"))){
parser.parse(stream, new BoilerpipeContentHandler(textHandler), xmetadata);
System.out.println("text:\n" + textHandler.toString());
}
}
}
You can have a simple demo of boilerpipe detection at.. and more information can be also available at..

JUnit NoClassDefFoundError

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

AS3 Unable to access public static constant

I've got this class where I declare two public static constants:
package com.xxx.videoplayer_v2 {
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import flash.text.TextField;
public class ControlBar extends MovieClip
{
public static const VOLUME_PRESSED:String = "volumePressed";
public static const PLAY_PRESSED:String = "playPressed";
...
The declaration looks good to me but when I call the constants from any other class in my project (below an example from the stage)
import com.xxx.videoplayer_v2.ControlBar;
trace(ControlBar.PLAY_PRESSED);
I get this error:
1119: Access of possibly undefined property PLAY_PRESSED through a
reference with static type Class.
Why does this happen? I've done this thousands of times with other classes, with the same syntax but I've never had this problem before.
I figured it out!
I have an instance of ControlBar on the stage wich in its properties I exported for ActionScript.
The problem was this: I filled in the Class textfield exactly the same name as the base class (ControlBar) and in the Base Class textfield I inserted "com.weborama.videoplayer_v2.ControlBar" which is right.
I fixed filling in the Class textfield "VPControlBar" instead of "ControlBar".
Now I know that I can't put the same name of the base class in there.
Thanks to everyone who tried to help me!