How do I declare field variable of tested class using apache velocity? - intellij-idea

I'm trying to setup intellij idea code template for JUnit4 Test Class so that when I create the test it will also generate a field variable in the test. Example :
public class FooTest {
private Foo foo;
...
}
The problem I'm having is using the $CLASS_NAME variable to set the field name with lower camel case.

You can do a toLowerCase() of first character. Sample below for reference.
import static org.junit.Assert.*;
#parse("File Header.java")
public class ${NAME} {
${BODY}
#set($var_name = ${NAME})
#set($var_name = $var_name.substring(0,1).toLowerCase() + $var_name.substring(1))
private ${CLASS_NAME} $var_name;
}

Related

redefine static methods with ByteBuddy

Can homebody help me please to give me a hint how to redefine static methods using byte-buddy 1.6.9 ?
I have tried this :
public class Source {
public static String hello(String name) {return null;}
}
public class Target {
public static String hello(String name) {
return "Hello" + name+ "!";
}
}
String helloWorld = new ByteBuddy()
.redefine(Source.class)
.method(named("hello"))
.intercept(MethodDelegation.to(Target.class))
.make()
.load(getClass().getClassLoader())
.getLoaded()
.newInstance()
.hello("World");
I got following Exception :
Exception in thread "main" java.lang.IllegalStateException: Cannot inject already loaded type: class delegation.Source
Thanks
Classes can only be loaded once by each class loader. In order to replace a method, you would need to use a Java agent to hook into the JVM's HotSwap feature.
Byte Buddy provides a class loading strategy that uses such an agent, use:
.load(Source.class.getClassLoader(),
ClassReloadingStrategy.fromInstalledAgent());
This does however require you to install a Java agent. On a JDK, you can do so programmatically, by ByteBuddyAgent.install() (included in the byte-buddy-agent artifact). On a JVM, you have to specify the agent on the command line.

which is not annotated with #Test or not included

I created two classes under the same package one is called preparation the other is X when I use dependsOnMethods to point to the test case in Preparation I get an exception.
class X.
#Test(enabled = true, dependsOnMethods = {"com.selenium.scripts.passkey.regression.delegateprofile.Preparations.TC_01"})
public void TC_01() {
something ...
}
class preparation :
#Test(enabled = true, description = "Preparation: create a new hotel.")
public void TC_01() {........}
Here is the error:
com.selenium.scripts.passkey.regression.delegateprofile.DProfile.TC_01()
is depending on method public void
com.selenium.scripts.passkey.regression.delegateprofile.Preparations.TC_02(),
which is not annotated with #Test or not included.
The method should be included in the .xml file
The method on which your test methods depends on should also be in the same class than different classes. This will not make the code ambiguous.
As I know, dependsOnMethods only accept method name and not a class + name.
What you can try to do is using groups and dependsOnGroups attribute.

Testing an injected config with spring without spring test runner

Given the following class:
public class ClassToBeTest{
${property.utility}
private String property;
private Utility utility;
public ClassToBeTested(Utility utility){
this.utility = utility;
}
public void doSomething(){
utility.doSomething(property);
}
}
We want to Mock Utility (we are using Mockito for instance) and verify that utility.doSomething is called passing the property parameter.
We don't really care about the config value (we have other tests for that). But we don't want to open the class with a method like (or passing the property by constructor).
public void setPropertyForTest(String property){
this.property = property;
}
Is there other ways to check that this private property (with no value) is passed to Utility?
Thanks a lot.
If you just want to set the private field, Spring (not Sprint) provides support for that in the spring-test module. See the setField() variants in ReflectionTestUtils.
Regards,
Sam

Problems about java syntax [duplicate]

This question already has answers here:
Can a java file have more than one class?
(18 answers)
Closed 8 years ago.
Here's the code :
public class EmployeeTest
{
public static void main(String args[]){
//System.out.println("hello world");
Employee aEmployee = new Employee("David",1000);
System.out.println(aEmployee.getName() + aEmployee.getSalary());
}
}
class Employee // **why can't I put a "public" here**
{
// Constructor
public Employee(String name, double salary)
{
this.name = name;
this.salary = salary;
}
// Methods
public String getName()
{
return this.name;
}
public double getSalary()
{
return this.salary;
}
// instance field
private String name;
private double salary;
}
My question is : in the second class definition's first line, why can't I put a "public" to define it ?
What's the exactly meaning of "public" when using it defines a class ?
This is language feature. There must be only one top-level public class per .java file and public class name must match the source java file name.
Basically, non-public types are not accessible outside the package so if you wish to allow type to be used anywhere then make it public.
Never create a type in default package. (Always use package)
Employee.java
package com.abc.model;
public class Employee{..}
EmployeeTest.java
package com.abc.test;
public class EmployeeTest{ ... }
Because a Java source file can have at most one top-level public class or interface, and the name of the source file must be the same as the name of that class or interface.
That's a rule that the Java compiler of Oracle's JDK imposes.
In Java, there can only be a single public top level class per source file and it needs to be named the same as the file.
This is useful for the compiler when it needs to locate a class definition from outside the package, since it knows the type name, it knows which class file to find the class in. For example. since a jar file is in essence a zip file with class files, this prevents the compiler from having to unzip the entire jar to find a class definition.
The Java language specification §7.6 specifies this as an optional restriction;
If and only if packages are stored in a file system (§7.2), the host
system may choose to enforce the restriction that it is a compile-time
error if a type is not found in a file under a name composed of the
type name plus an extension (such as .java or .jav) if either of the
following is true:
The type is referred to by code in other compilation units of the
package in which the type is declared.
The type is declared public (and therefore is potentially accessible
from code in other packages).
you can define a public class inside a public class which is legal.
public class EmployeeTest
{
public class Employee {
}
}

Declare global variable in action script?

I want to create a variable(possible global variable) in one action script file and want to use the same variable across all other action script files in the project. How to create such a variable and how to use the same variable across all .as files??
One simple way is to define a static variable in a Class (either create a new Class or use one of your existing classes):
// in MyConfig.as
class MyConfig {
static var myVariable:String = "Hi";
}
// You can access / set the value from any class using MyConfig.myVariable
trace(MyConfig.myVariable); // prints Hi
MyConfig.myVariable = "Hello";
trace(MyConfig.myVariable); // prints Hello
Create one public class (lets assume GlobalVariables.as)
No need to add its instance.
Now declare all the variables that you want to use across multiple classes as STATIC.
(you can also declare and create instances of classes in that class to avoid multiple instances of classes)
Also you can add common methods to this class
So whenever you want to access that variable declared in GlobalVariables class
you need to access using reference e.g GlobalVariables.variableName
sample code:
package classes{
public class GlobalVariables{
public static var strURL:String;
public static function setExternalLinks(){
strURL = "http://demourl.asmx/";
}
}
}