I can't get sample Junit 4.10 code with #Category annotation to compile. Code is:
import org.junit.*;
#Category({CatA.class})
public class A {
#Test
public void a() {
System.out.println("\n ***** Method a \n");
}
}
Command to compile code:
javac -cp .:junit-4.10.jar A.java
A.java:3: error: cannot find symbol
#Category({CatA.class})
^
symbol: class Category
1 error
Thanks,
Henry
You need the following import in your code as well:
import org.junit.experimental.categories.Category;
Related
# Junit not working giving error #
Tried testing basic add operation using Junit its showing this error on my mac
# AddTest.java #
import static org.junit.Assert.*;
import org.junit.Test;
public class AddTest {
#Test
public void Addtest() {
MyJUnitClass junit = new MyJUnitClass();
int result = junit.add(100, 200);
assertEquals(300, result);
}
}
# MyJunitClass #
public class MyJUnitClass {
public int add(int a, int b) {
return a+b;
}
public String conCat(String a, String b) {
return a+b;
}
}
java.lang.NoClassDefFoundError: org/junit/runner/manipulation/Filter
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:332)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadTestLoaderClass(RemoteTestRunner.java:380)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.createRawTestLoader(RemoteTestRunner.java:370)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.createLoader(RemoteTestRunner.java:365)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.defaultInit(RemoteTestRunner.java:309)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.init(RemoteTestRunner.java:224)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:208)
Caused by: java.lang.ClassNotFoundException: org.junit.runner.manipulation.Filter
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 8 more
This is the error I am getting while running I have placed the JUnit 4.jar file libraries in buildpath. My I am able to access all the functions from JUnit jar file but when I run the JUnit test I get Failure error and this message in console.
You should try "add library", and then choosing JUnit, not adding a jar directly !
Good luck :)
I'm trying to solve this same problem as in How to get ANTLR 3.2 to exit upon first error? but in antlr4 since there's no recoverFromMismatchedToken()
Antlr4 typically uses the ANTLRErrorStrategy interface. Look at the BailErrorStrategy in particular.
To bail on a lexer error immediately, override the two recover(....) methods in the Lexer class. Do this by implementing (1) lexer member methods; or (2) creating a Lexer superclass containing the override methods.
It worked, GRosenberg!
I downloaded the BailErrorStrategy.java file (see GRosenberg's note above).
I then stopped using the default Lexer, and created my own simple Sub-class Lexer as:-
package ...;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.LexerNoViableAltException;
import org.antlr.v4.runtime.RecognitionException;
public class MyYAMLANTLR4Lexer extends YAMLANTLR4Lexer {
public MyYAMLANTLR4Lexer(CharStream input) {
super(input);
}
public void recover(LexerNoViableAltException e) { }
public void recover(RecognitionException re) { }
}
HelloWorld.ceylon
import java.util { HashMap } //Error:(1, 8) ceylon: package not found in imported modules: java.util (define a module and add module import to its module descriptor)
void run() {
print("test");
}
module.properties
module CeylonHelloWorld "1.0" {
import java.base "8";
}
I get an exception in HelloWord.ceylon file
When I try that code, I get:
Incorrect syntax: mismatched token CeylonHelloWorld expecting initial-lowercase identifier
In module.ceylon.
The name of a module is supposed to be of form foo.bar.baz (initial-lowercase identifiers separated by periods).
Like mentioned by Gavin you will have to use a legal module name, when I change your code to use the module name "java8test" I get the following output when compiling:
$ ceylon compile java8test
warning: It looks like you are using class files from a Java newer than 1.7.
Everything should work well, but if not, let us know at https://github.com/ceylon/ceylon-compiler/issues.
In the near future, the Ceylon compiler will be upgraded to handle Java 1.8.
./source/java8test/run.ceylon:1: warning: import is never used: 'HashMap'
import java.util { HashMap }
^
2 warnings
Note: Created module java8test/1.0.0
Which is all as expected.
module.ceylon
module holaCeylon "1.0.0"{
import java.base "7"; // versiĆ³n 7 JDK
}
package.ceylon
shared package holaCeylon;
Now we go back to the run.ceylon file and import the java.util.HashMap Java library.
run.ceylon
import java.util { HashMap }
shared void run(){
print("Importando librerias de Java en Ceylon");
value romanos = HashMap<String,Integer>();
romanos.put("I", 1);
romanos.put("V", 5);
romanos.put("X", 10);
romanos.put("L", 50);
romanos.put("C", 100);
romanos.put("D", 500);
romanos.put("M", 1000);
print(romanos.values());
print(romanos.keySet());
}
Output:
salida
Code:
http://codemonkeyjunior.blogspot.mx/2015/03/ceylon-interoperabilidad-con-java.html
I am trying to build a ActionScript side library for my SIP library Adobe AIR native extension following this blog from Adobe, in FlashDevelop IDE. When I build the project I get the following compile time error:
C:\Users\Osama
Mohammed\Documents\AndroidSIPManager\src\in\innovative\androidsipmanager\AndroidSIPManager.as(1):
col: 9 Error: Syntax error: expecting identifier before in.
I don't know why am I getting that error, although my syntax is right. I get this error when I write package name after package keyword in any ActionScript 3 project in ActionScript file, Eg. package my.package { ..., but don't get it when no package name is written after package keyword. Following is my AndroidSIPManager.as code:
package in.innovative.androidsipmanager //getting error here
{
import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;
/**
* ...
* #author Osama Mohammed Shaikh
*/
public class AndroidSIPManager extends EventDispatcher
{
private var extContext:ExtensionContext;
public function AndroidSIPManager(target:IEventDispatcher=null)
{
super(target);
extContext = ExtensionContext.createExtensionContext("in.innovative.SIPLibExtension", "sip");
if (!extContext)
{
throw new Error("SIP Library extension is not supported on this platform");
}
}
public function initialize(enum_transport:int, agent:String, STUNServer:String, STUNServerPort:int):int
{
return int (extContext.call("initialize", enum_transport, agent, STUNServer, STUNServerPort));
}
public function setUserInfo(userName:String, displayName:String, authName:String, password:String, localSIPPort:int, userDomain:String, SIPServer:String, SIPServerPort:int, outboundServer:String, outboundServerPort:int):int
{
return int (extContext.call("setUserInfo", userName, displayName, authName, password, localSIPPort, userDomain, SIPServer, SIPServerPort, outboundServer, outboundServerPort));
}
public function portSipCall(callee:String, enum_mediaType:int):Number
{
return Number (extContext.call("portSipCall", callee, enum_mediaType));
}
}
}
Please help me solve the problem.
Problem is that in is reserved word. This is the reason, why you're able to compile project after removing the package name completely.
To tell you first, i have tried and tried it again and now i need some help
Heres my code
package staticPkg;
public class Static {
public static final String staticMethod() {
System.out.println("Static method called");
return "Static called";
}
}
package staticPkg;
public class TargetClass {
Static staticClass;
public String callHere() {
return Static.staticMethod();
}
}
package staticPkg;
import org.easymock.EasyMock;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.testng.IObjectFactory;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.ObjectFactory;
import org.testng.annotations.Test;
#PrepareForTest({Static.class})
public class TestClass {
Static staticClass = null;
#ObjectFactory
public IObjectFactory getObjectFactory() {
System.out.println("got object factory");
return new org.powermock.modules.testng.PowerMockObjectFactory();
}
#BeforeMethod
public void setup() {
System.out.println("print me");
PowerMock.mockStatic(Static.class);
staticClass = PowerMock.createMock(Static.class);
}
#Test
public void testMe() {
EasyMock.expect(Static.staticMethod()).andReturn("Mock called").anyTimes();
PowerMock.replay(Static.class,staticClass);
TargetClass tc = new TargetClass();
String output = tc.callHere();
PowerMock.verify(Static.class,staticClass);
System.out.println(output);
}
}
And heres the log
[Parser] Running:
C:\MockWorkspace\Mock\temp-testng-customsuite.xml
got object factory
print me
Static method called
FAILED: testMe
java.lang.IllegalStateException: no last call on a mock available
at org.easymock.EasyMock.getControlForLastCall(EasyMock.java:521)
at org.easymock.EasyMock.expect(EasyMock.java:499)
at staticPkg.TestClass.testMe(TestClass.java:46)
... Removed 22 stack frames
===============================================
staticPkg.TestClass
Tests run: 1, Failures: 1, Skips: 0
===============================================
===============================================
Mock
Total tests run: 1, Failures: 1, Skips: 0
===============================================
Help please, i have tried a variety of solutions, can't get it done.
Please can anyone try this code and correct it for success?
I get error in EasyMock.expect ...............
Got a work around at http://blogs.bytecode.com.au/glen/2006/10/12/doing-bytecode-kungfu-with-javassist.html
And it works
But wait..........I am stuck again
My testcase works fine when runs alone, but when run with Ant, it gives problem. Might be other test cases of different files are interfering.
I got the same error, when my individual test case was using #PrepareTest & easymock/powermock
[testng] ====================STATIC CALLED===========================
[testng] javassist.CannotCompileException: by java.lang.LinkageError: loader (instance of sun/misc/Launcher$AppClass
Loader): attempted duplicate class definition for name: "com/symantec/mobius/aggregator/submission/SubmissionFactory"
[testng] at javassist.ClassPool.toClass(ClassPool.java:1085)
[testng] at javassist.ClassPool.toClass(ClassPool.java:1028)
[testng] at javassist.ClassPool.toClass(ClassPool.java:986)
[testng] at javassist.CtClass.toClass(CtClass.java:1110)
Try extending from PowerMockTestCase. The TestNG support will also be updated in next version of PowerMock (1.4.9).
I faced this same issue, and struggled a lot. Finally, found the following solution:
Another alternative is to set the object-factory to org.powermock.modules.testng.PowerMockObjectFactory in the TestNG suite.xml. Here is a sample suite file:
<suite name="dgf" verbose="10" object-factory="org.powermock.modules.testng.PowerMockObjectFactory">
<test name="dgf">
<classes>
<class name="com.example.ClientTest"/>
</classes>
</test>
</suite>
Of course, you can also extend your test case from PowerMockTestCase as told by Johan.
Mock all the static methods in static class before proceeding to mock the static method. Try with this:
#Test
public void testMe() {
PowerMock.mockStatic(Static.class);
EasyMock.expect(Static.staticMethod()).andReturn("Mock called").anyTimes();
PowerMock.replay(Static.class,staticClass);
TargetClass tc = new TargetClass();
String output = tc.callHere();
PowerMock.verify(Static.class,staticClass);
System.out.println(output);
}