Running tests with VS Code (without Maven) using the "Test Runner for Java" extension - visual-studio-code

I'm trying to make a minimal working example of JUnit5 using Visual Studio code without Maven. The reason is that I'm trying to make a quick demo for 1st year students, and I don't have the time to explain Maven (it will be the subject of a later course).
My VS Code workspace has this structure (I'm on Linux so forward slashes are normal) :
The settings.json file contains:
{
"java.project.referencedLibraries": [
"demojunit/junit-platform-console-standalone-1.8.1.jar"
]
}
The DemoJUnit.java contains:
package demojunit;
import java.util.Scanner;
public class DemoJUnit {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int integer = -1;
do {
System.out.print("Enter an integer between 1 and 20 : ");
if (keyboard.hasNextInt())
integer = keyboard.nextInt();
keyboard.nextLine();
} while ((integer < 1) || (integer > 20));
keyboard.close();
long result = computeFactorial(integer);
System.out.println("The factorial " + integer + "! gives " + result);
}
public static long computeFactorial(int number) {
if (number < 1)
return -1;
if (number > 20)
return -2;
long factorial = 1;
for (int i = 2; i <= number; i++) {
factorial = factorial * i;
}
return factorial;
}
}
And the DemoJUnitTest.java contains:
package demojunit;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class DemoJUnitTest {
#Test
public static void computeFactorial_CleanTest() {
long expected_value = 120;
long real_value = DemoJUnit.computeFactorial(5);
Assertions.assertEquals(expected_value, real_value);
}
#Test public static void computeFactorial_DirtyTest() {
long expected_value = -1;
long real_value = DemoJUnit.computeFactorial(0);
Assertions.assertEquals(expected_value, real_value);
}
}
The settings.json seems to work since the imports and #Test are not marked as errors in VS Code.
As you can see in the folder image, I downloaded the latest JUnit5 jar file, and it works when running from the command line:
However, running from the command line is not convenient, so I installed the "Test Runner for Java" extension.
The problem is that when running tests, something fails and I don't have any error messages, nor any indication what went wrong. I changed the extensions' settings to be verbose but I still get no error messages in the Terminal, the Debug Console, nor anywhere else.
All I get is :
Any idea why the extension fails (and fails to give any indication on why it fails)?

Related

Eclipse stops running

Whenever I try to use decrement in wrong way Eclipse stops working. Seems it started to happen after I installed Lombok. The code is shown below:
public class Patterns {
public static void main(String[] args) {
pattern31(5);
}
static void pattern31(int n) {
n = 5;
for (int row = 1 ; row <= n; row++) {
int columns = row>n ? 2*n-row:row;
int space=n-columns;
int p='A';
for(int s=0; s<=space; s++) {
System.out.print(" ");
}
for(int col=1; col<columns; col++) {
System.out.print((char) p++ +" ");
}
for(int col=2; col<=columns; col--) {
System.out.print((char) p++ +" ");
}
System.out.println();
}
}
}
I also have this message in the log:
!MESSAGE Warning: The environment variable HOME is not set. The following directory will be used to store the Git
user global configuration and to define the default location to store repositories: 'C:\Users\User'. If this is
not correct please set the HOME environment variable and restart Eclipse. Otherwise Git for Windows and
EGit might behave differently since they see different configuration options.
This warning can be switched off on the Team > Git > Confirmations and Warnings preference page.

How to import other person's package?

import CtCILibrary.*;
public class RotateMatrix {
public static boolean rotate (int [][]matrix) {
if ( matrix.length == 0 || matrix.length != matrix[0].length) {
return false;
}
int n = matrix.length;
for ( int layer = 0 ; layer < n / 2 ; layer++ ) {
int first = layer;
int last = n - 1 - first;
for ( int i = first ; i < last; i++ ) {
int offset = i - first;
int top = matrix[first][i];
//left-> top
matrix[first][i] = matrix [last-offset][first];
//bottom -> left
matrix[last-offset][first] = matrix [last][last-offset];
//right -> bottom
matrix[last][last-offset]= matrix[i][last];
//top->right
matrix[i][last]=top;
}
}
return true;
}
Hi there, I'm trying to import the CtCI library to solve the problem but there's an error message. And from her GitHub, that was how she imported this class. I know we can import the class from java built-in library. But I'm not sure how import works when trying to import from someone else. Could someone explain it to me?
If you are doing serious programming for a long term app, and by import you refer to libraries/dependencies, I highly recommend you using maven for it.
If you don't use maven yet, you can just add the .jar files from you IDE to your project path, but with maven is way better.
If you mean importing a .java file written by someone else, to use part of it in your code, just put the file at your project path, you probably need to adapt its libraries and imports, and once ready you can just use its methods ans stuff. Just make sure you have tipped at the top of your current file the import sentence of that file.

Katalon Studio deleting groovy code from script when switching to manual or recording

Did anybody see this as well, or am I doing something wrong?
I am working on a test case with Katalon Studio, in script mode. I have some Groovy script in it, (in particular a class definition, see start of code below). When I switch mode to work in manual or record mode, and then back to script mode, the groovy code (the class declaration in the example below) has disappeared...
Not very practical! Anything that should be done to avoid this?
Many thanks!
E.
Code example:
//Katalon Imports here
class Product {
String nozo
String price_string
Number qty = 1
Number price_ht = 0
Number price_ttc = 0
Product(String nozo, String price_string, Number qty = 1) {
this.nozo = nozo
this.price_string = price_string
def get_price = (this.price_string =~/(\d+)\s(\d+\.\d{2})/)
if(get_price) {
this.price_ttc = get_price[1] + get_price[2]
}
else this.price_ttc = 0
this.price_ht = this.price_ttc / 1.2
}
def get_price_order_line_ht() {
return this.price_ht * this.qty
}
}
// Intialisation of test data
Number qty_pdt1 = 2
'Open home page'
WebUI.openBrowser('http://localhost:8080/')
'Navigate to subrange'
WebUI.doubleClick(findTestObject('Object Repository/vb_desktop/home_page_desktop_fr/a_Lampadaire'))
// Etc...

Diagnosing netlogo extension command error

When I try to execute the first-n-integers extension example:
show noise:first-n-integers 5
I receive an error "ERROR: Expected command. " in the console or in the code tab.
I have mostly copied and pasted the class examples as is, just renaming them and putting them in a different package. I wish the error was a little more descriptive, as I suspect I am making a simple error somewhere.
I'm using 5.0.4 without the JRE and a 1.7.0_45 JRE/JDK on a x64 Windows 7 machine.
My manifest.txt file with fully-qualified class-manager and crlf at the end of the version line--
Manifest-Version: 1.0
Extension-Name: noise
Class-Manager: org.xyz.extensions.NoiseExtension
NetLogo-Extension-API-Version: 5.0 <--there is a crlf here
the jar is in a subfolder with my model file
test/
test.nlogo
noise/
noise.jar
This is my Class-Manager:
package org.xyz.extensions;
import org.nlogo.api.*;
public class NoiseExtension extends DefaultClassManager {
public void load(PrimitiveManager primitiveManager) {
primitiveManager.addPrimitive(
"first-n-integers", new org.xyz.extensions.NoiseGenerator());
}
}
This is the NoiseGenerator file:
package org.xyz.extensions;
import org.nlogo.api.*;
public class NoiseGenerator extends DefaultReporter {
public Syntax getSyntax() {
return Syntax.reporterSyntax(
new int[] {Syntax.NumberType()}, Syntax.ListType());
}
public Object report(Argument args[], Context context) throws ExtensionException {
// create a NetLogo list for the result
LogoListBuilder list = new LogoListBuilder();
int n ;
// use typesafe helper method from
// org.nlogo.api.Argument to access argument
try {
n = args[0].getIntValue();
}
catch(LogoException e) {
throw new ExtensionException( e.getMessage() ) ;
}
if (n < 0) {
// signals a NetLogo runtime error to the modeler
throw new ExtensionException
("input must be positive");
}
// populate the list. note that we use Double objects; NetLogo
// numbers are always Doubles
for (int i = 0; i < n; i++) {
list.add(Double.valueOf(i));
}
return list.toLogoList();
}
}
Thanks for any help.
AJB
This was asked and answered here:
https://groups.google.com/d/msg/netlogo-devel/eIq8drflsc8/7y_Ooh6R0sgJ
The answer was to correct the spelling of getSynax to getSyntax.

IronRuby performance issue while using Variables

Here is code of very simple expression evaluator using IronRuby
public class BasicRubyExpressionEvaluator
{
ScriptEngine engine;
ScriptScope scope;
public Exception LastException
{
get; set;
}
private static readonly Dictionary<string, ScriptSource> parserCache = new Dictionary<string, ScriptSource>();
public BasicRubyExpressionEvaluator()
{
engine = Ruby.CreateEngine();
scope = engine.CreateScope();
}
public object Evaluate(string expression, DataRow context)
{
ScriptSource source;
parserCache.TryGetValue(expression, out source);
if (source == null)
{
source = engine.CreateScriptSourceFromString(expression, SourceCodeKind.SingleStatement);
parserCache.Add(expression, source);
}
var result = source.Execute(scope);
return result;
}
public void SetVariable(string variableName, object value)
{
scope.SetVariable(variableName, value);
}
}
and here is problem.
var evaluator = new BasicRubyExpressionEvaluator();
evaluator.SetVariable("a", 10);
evaluator.SetVariable("b", 1 );
evaluator.Evaluate("a+b+2", null);
vs
var evaluator = new BasicRubyExpressionEvaluator();
evaluator.Evaluate("10+1+2", null);
First Is 25 times slower than second. Any suggestions? String.Replace is not a solution for me.
I do not think the performance you are seeing is due to variable setting; the first execution of IronRuby in a program is always going to be slower than the second, regardless of what you're doing, since most of the compiler isn't loaded in until code is actually run (for startup performance reasons). Please try that example again, maybe running each version of your code in a loop, and you'll see the performance is roughly equivalent; the variable-version does have some overhead of method-dispatch to get the variables, but that should be negligible if you run it enough.
Also, in your hosting code, how come you are holding onto ScriptScopes in a dictionary? I would hold onto CompiledCode (result of engine.CreateScriptSourceFromString(...).Compile()) instead -- as that will help a lot more in repeat runs.
you can of course first build the string something like
evaluator.Evaluate(string.format("a={0}; b={1}; a + b + 2", 10, 1))
Or you can make it a method
if instead of your script you return a method then you should be able to use it like a regular C# Func object.
var script = #"
def self.addition(a, b)
a + b + 2
end
"
engine.ExecuteScript(script);
var = func = scope.GetVariable<Func<object,object,object>>("addition");
func(10,1)
This is probably not a working snippet but it shows the general idea.