0

I have downloaded a large project (Whiley) and am wanting to Trace every method that is executed during the projects JUnit Tests.

There are 6 modules and they rely on each other mostly.

I am currently trying to get a single module working using Eclipse AspectJ plugin.

My aspectJ Inpath settings

Inpath Settings

AspectJ weaves the code and produces the compiled version of wyc in the /bin folder.

However, when I try and run the JUnit test's .class file through the CLI.

With the following statement:

java -cp "C:\Users\name\git\WhileyCompiler\lib\junit-4.11.jar";"C:\Users\name\git\WhileyCompiler\lib\*.jar";"C:\Users\name\git\WhileyCompiler\lib\hamcrest-all-1.3.jar" org.junit.runner.JUnitCore "C:\Users\name\git\Test\bin\wyc\testing\AllValidTests.class"

I get the following:

Error

My aspect file

    pointcut traceMethods() : (execution(* *(..))&& !cflow(within(Trace)));

before(): traceMethods(){
    Signature sig = thisJoinPointStaticPart.getSignature();
    String line =""+ thisJoinPointStaticPart.getSourceLocation().getLine();
    String sourceName = thisJoinPointStaticPart.getSourceLocation().getWithinType().getCanonicalName();
    service.addMethodCall(sourceName);
    Logger.getLogger("Tracing").log(
            Level.INFO,
            "Call from "
                +  sourceName
                +" line " +
                line
                +" to " +sig.getDeclaringTypeName() + "." + sig.getName()
    );
}
user2469515
  • 373
  • 3
  • 12
  • first, it says that it can''t find the file. Have you verify that claim? Is there any file AllValidTests.class in that place? – Hoàng Long Mar 21 '15 at 09:44
  • second, are you sure about the argument? It sounds a little odd that JUnitCore use a path to the compiled file as its argument... – Hoàng Long Mar 21 '15 at 09:47

2 Answers2

2

Four things to note:

  • User Hoàng Long is right, you need to specify a fully qualified class name instead of a path to the JUnit runner.
  • This also means that you need to add the path to your test classes to the classpath.
  • In a classpath you cannot use *.jar, you just use * as a wildcard, see this answer.
  • Furthermore, if you really want to use precompiled AspectJ aspects, you also need to add the AspectJ runtime aspectjrt.jar to your classpath.

Here is an example of how to run JUnit in your case. I added line breaks so as to make it more readable, everything really needs to be on one line:

java
    -cp
        "C:\Users\name\git\WhileyCompiler\lib\junit-4.11.jar";
        "C:\Users\name\git\WhileyCompiler\lib\*";
        "C:\Users\name\git\WhileyCompiler\lib\hamcrest-all-1.3.jar";
        "C:\Users\name\git\WhileyCompiler\lib\aspectjrt.jar";
        "C:\Users\name\git\Test\bin"
    org.junit.runner.JUnitCore
        wyc.testing.AllValidTests

Update:

Actually I was busy this week, but I just gave it a quick try here with LTW on the binary JARs, but the *.whiley test files unpacked. It works nicely, I see the log output on the console. I had to change your pointcut though because it lead to bloated output in the LTW scenario when all the loaded framework classes (e.g. from JUnit) were also woven. I also simplified your aspect a bit in order to provide formatted output on System.out directly instead of all the logging overhead with timestamps because I found that more readable.

Modified aspect:

package de.scrum_master.aspect;

public aspect TraceAspect {
    pointcut traceMethods() :
        execution(* *(..)) && (within(whiley..*) || within(wy*..*));

    before() : traceMethods() {
        System.out.printf(
            "%40s | %s%n",
            thisJoinPoint.getSourceLocation(),
            thisJoinPoint.getSignature() 
        );
    }
}

Console output:

JUnit version 4.11
.                 AllValidTests.java:2729 | void wyc.testing.AllValidTests.TypeEquals_Valid_1()
                   AllValidTests.java:97 | void wyc.testing.AllValidTests.runTest(String)
                       TestUtils.java:28 | Pair wyc.testing.TestUtils.compile(String[])
                       Pipeline.java:292 | void wycc.lang.Pipeline.register(Class)
                       Pipeline.java:292 | void wycc.lang.Pipeline.register(Class)
                       Pipeline.java:292 | void wycc.lang.Pipeline.register(Class)
                       Pipeline.java:292 | void wycc.lang.Pipeline.register(Class)
                       Pipeline.java:292 | void wycc.lang.Pipeline.register(Class)
                       Pipeline.java:292 | void wycc.lang.Pipeline.register(Class)
                       Pipeline.java:292 | void wycc.lang.Pipeline.register(Class)
                       Pipeline.java:292 | void wycc.lang.Pipeline.register(Class)
                        Content.java:135 | Content.Filter wyfs.lang.Content.filter(String, Content.Type)
                           Trie.java:238 | Trie wyfs.util.Trie.fromString(String)
                           Trie.java:193 | Trie wyfs.util.Trie.append(String)
                           Trie.java:312 | int wyfs.util.Trie.binarySearch(Trie[], int, String)
                        Content.java:135 | Content.Filter wyfs.lang.Content.filter(String, Content.Type)
                           Trie.java:238 | Trie wyfs.util.Trie.fromString(String)
                           Trie.java:193 | Trie wyfs.util.Trie.append(String)
                           Trie.java:312 | int wyfs.util.Trie.binarySearch(Trie[], int, String)
                        Content.java:135 | Content.Filter wyfs.lang.Content.filter(String, Content.Type)
                           Trie.java:238 | Trie wyfs.util.Trie.fromString(String)
                           Trie.java:193 | Trie wyfs.util.Trie.append(String)
                           Trie.java:312 | int wyfs.util.Trie.binarySearch(Trie[], int, String)
                     VirtualRoot.java:50 | VirtualRoot.Folder wyfs.util.VirtualRoot.root()
                     VirtualRoot.java:50 | VirtualRoot.Folder wyfs.util.VirtualRoot.root()
                     VirtualRoot.java:50 | VirtualRoot.Folder wyfs.util.VirtualRoot.root()
                        WycMain.java:179 | int wyc.WycMain.run(String[])
                         OptArg.java:322 | Map wycc.util.OptArg.parseOptions(List, OptArg[])
                         OptArg.java:218 | void wycc.util.OptArg.FILEDIR.process(String, String, Map)
                         OptArg.java:231 | void wycc.util.OptArg.FILELIST.process(String, String, Map)
                        WycMain.java:258 | void wyc.WycMain.configure(Map)
                   WycBuildTask.java:331 | void wyc.util.WycBuildTask.setVerbose(boolean)
                   WycBuildTask.java:335 | void wyc.util.WycBuildTask.setVerification(boolean)
                   WycBuildTask.java:343 | void wyc.util.WycBuildTask.setSmtVerification(boolean)
                   WycBuildTask.java:339 | void wyc.util.WycBuildTask.setVerificationConditions(boolean)
                   WycBuildTask.java:359 | void wyc.util.WycBuildTask.setWhileyDir(File)
                  DirectoryRoot.java:135 | DirectoryRoot.Folder wyfs.util.DirectoryRoot.root()
                  DirectoryRoot.java:135 | DirectoryRoot.Folder wyfs.util.DirectoryRoot.root()
                   WycBuildTask.java:399 | void wyc.util.WycBuildTask.setBootPath(List)
                                     ... | ...
Community
  • 1
  • 1
kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • It is now running the tests A-OK thanks. However, the aspect doesn't seem to be being woven into it output.jar file. My project files look as such: http://prntscr.com/6jtunz – user2469515 Mar 22 '15 at 08:21
  • 2
    Well, that screenshot is insufficient information in order to find out what is going wrong. A real [SSCCE](http://sscce.org/) would be better because I could reproduce your situation. Apart from that, I wonder why you use CTW (compile-time weaving) at all instead of LTW (load-time weaving) in this case because the latter would be minimally invasive with regard to (not) modifying third-party libraries. Why do you compile them by yourself at all and not just take them as given and use the JARs? – kriegaex Mar 22 '15 at 12:20
  • @kriegaex: very detailed & good answer.Personally I think it's hard to follow with the question, because I don't understand AspectJ has anything to do with JUnit? – Hoàng Long Mar 22 '15 at 13:59
  • AspectJ has nothing to do whatsoever with JUnit, disregarding the fact that the author wants to use them together in order to trace his unit tests. ;-) – kriegaex Mar 22 '15 at 18:19
  • I have updated my answer with some source code and a console log. – kriegaex Mar 27 '15 at 20:38
1

I think that the problem lies in the argument you provide to JUnit. Let's go to the folder with the compiled class and run the command:

java -cp "(classpath here)" org.junit.runner.JUnitCore wyc.testing.AllValidTests

If I recall correctly, the argument provided must be the class name, NOT the path to the compiled file.

Hoàng Long
  • 10,746
  • 20
  • 75
  • 124