Weaving production aspect into test class with Gradle AspectJ

I am using the Gradle AspectJ plugin to weave some production aspect into test Java code. I would have expected this to work out of the box with the plugin, but apparently that's not the case as demoed here: https://github.com/sedubois/gradle-aspectj-poc/tree/dc44f529831a485fcff8f4889dba8098784dddb4

The weaving of UnsupportedOperationAspect into MainSevice (both under src/main/java) works, but the weaving of this same aspect into TestService (under src/test/java) doesn't.

I am new to Groovy, Gradle and AspectJ and didn't figure out if I should add some testAspectpath configuration or similar?

EDIT1: seems unrelated, but iajc gives a warning:

... :compileTestAspect [ant:iajc] [warning] incorrect classpath: [...]\gradle-aspectj-poc\build\resources\main ...

EDIT2: I naively added this code to the Gradle dependencies:

ajInpath fileTree(dir: "src/test/java") aspectpath fileTree(dir: "src/test/java") testAjInpath fileTree(dir: "src/test/java") testAspectpath fileTree(dir: "src/test/java")

It doesn't help, the first test works and the second one fails as usual, with these new messages:

... :compileAspect [ant:iajc] [warning] build config error: skipping missing, empty or corrupt aspectpath entry: [...]\gradle-aspectj-poc\src\test\java\com\hello\aop\TestService.java [ant:iajc] [warning] build config error: skipping missing, empty or corrupt inpath entry: [...]\gradle-aspectj-poc\src\test\java\com\hello\aop\TestService.java ... :compileTestAspect [ant:iajc] [warning] build config error: skipping missing, empty or corrupt aspectpath entry: [...]\gradle-aspectj-poc\src\test\java\com\hello\aop\TestService.java [ant:iajc] [warning] build config error: skipping missing, empty or corrupt inpath entry: [...]\gradle-aspectj-poc\src\test\java\com\hello\aop\TestService.java [ant:iajc] [warning] incorrect classpath: [...]\gradle-aspectj-poc\build\resources\main ...


By default the plugin does not weave the main aspects in the test classes - we simply never made a configuration option for it. You can do this yourself using the following line:

testAspectpath sourceSets.main.output

There is no expression matching TestService#serviceMethod().

In order to make your testcase work you need to advice your service method and (very important) the aspect must be located in the src/test/ package. Otherwise the compiler would not weave it in.

// located in `src/test/java`

package com.hello.aop;

class UnsupportedOperationAspect {

@Before("execution(void com.hello.aop.TestService.serviceMethod(..))")
public void throwUnsupportedOperationOnMethod1() {
throw new UnsupportedOperationException();

Just a sidenote as i do not know what you are trying to achieve by advising test-classes (that are components specifically for test cases only): keep your tests as simple as possible.

Category:java Time:2017-12-03 Views:3

Related post

  • Mocking and testing classes with multiple constructor parameters 2011-02-21

    My service layer classes are setup for constructor injection. When mocking and testing these classes, how can I avoid having to pass in null for parameters I won't be using in my test? Sample code I'm trying to avoid: [TestFixture] public class SomeS

  • Manual injection into custom class with swiftSuspenders/robotlegs 2010-11-08

    in my context: var parserManager:ParserManager = injector.instantiate(ParserManager); parserManager.injector = injector; injector.mapValue(ParserManager, parserManager); in my parserManager(doesn't extend any other class) class: public var injector:I

  • Many Test classes or one Test class with many methods? 2011-08-02

    I have a PersonDao that I'm writing unit tests against. There are about 18-20 methods in PersonDao of the form - getAllPersons() getAllPersonsByCategory() getAllPersonsUnder21() etc My Approach to testing this was to create a PersonDaoTest with about

  • Multi-project test dependencies with gradle 2011-04-13

    I have a multi-project configuration and I want to use gradle. My projects are like this Project A -> src/main/java -> src/test/java Project B -> src/main/java (depends on src/main/java on Project A) -> src/test/java (depends on src/test/

  • problem compiling a junit test class with ant 2009-07-16

    I am having issues integrating junit with a working ant build.xml file. My test class is in the same directory as my source classes. As I am learning how to use ant, I simply want to compile all the source and the test classes. I am using eclipse and

  • Correct way to unit test class with inner class 2011-11-24

    Class A has inner class B. Class A has a private list of class B objects that it makes available through getBs, addB, and removeB methods. How do I unit test the removeB method? I was hoping to create two equal mocks of class B, add each, and then re

  • Can you think of a better way to only load DBbUnit once per test class with Spring? 2011-11-29

    I realise that best practise may advise on loading test data on every @Test method, however this can be painfully slow for DBUnit so I have come up with the following solution to load it only once per class: Only load a data set once per test class S

  • Load time weaving of javax.swing.* classes with AspectJ 2012-02-10

    I am using AspectJ to trace calls to graphics class. So far, I have used a pre-weaved set of javax.swing.* classes and upon loading I tell JVM to use these weaved classes and not the ones from JRE by using -Xbootclasspath/p switch. I would like to sw

  • Injecting objects into servlet classes with JAX-RS 2012-03-13

    I am pretty new to this whole servlet thing, so please correct me if I mix or use incorrect/confusing terms for things. I am however writing a blog ping server in Java using JAX-RS (Jersey), and I'm facing a problem where I have one servlet class tha

  • How to let maven run a single test class with non-default profile activated? 2010-07-30

    I'm trying to let maven run a single test class but I need to use an additional profile (which in fact is already created). Normally when I run: mvn clean install -PmyProfile "myProfile" is being activated. So I tried: mvn -Dtest=myTest -PmyProfile t

  • How can I run a custom JUnit4 Runner on JUnit3 test classes with Ant? 2011-04-20

    We have test classes which are built on Spring 2.0.8's AbstractTransactionalDataSourceSpringContextTests. There are a huge number of these, all of which are written in JUnit3 style. In order to use JUnit 4 filtering, we have concocted a replacement J

  • Unit test class with stubs. Should I configure stubs to return ALWAYS correct values? 2012-01-08

    this is one of my questions about unit testing. I'm reading The Art Of Unit Testing and at chapter 3 the author shows how to remove dependency between one or more classes. That seems clear to me. What's not absolutely clear is the following point. Wh

  • How to inject EasyMock mock into tested class private field 2012-04-04

    I'm using EasyMock to create mock that is one of private parameters (without setter) in tested class. I tried using reflection - but it does not work correctly. public class TestedClassTest{ @Test public void test(){ TestedClass instance = new Tested

  • Making each test method run in its own instance of a test class with TestNG? 2011-04-30

    So I thought the following code would run fine in TestNG, although it doesn't: public class Tests { int i = 0; @Test public void testA() { Assert.assertEquals(0, i); ++i; } @Test public void testB() { Assert.assertEquals(0, i); ++i; } } Is there a wa

  • manually testing classes with testng and jdk1.4 2012-03-08

    I work on a maven project that needs to be jdk1.4 compliant. Our testng classes use the pseudo-javadoc comments to define tests. /** * @testng.test */ In Netbeans, the right-click menu works fine to run tests. In Eclipse, my collegues tell me it does

  • Remove nested test classes with maven2 2009-04-14

    I use nested classes for accessing private members in JUnit tests. They are alaways named "TestProxy". I would like to remove them at Build time using maven2, to not include it into the jar file. Is there any configuration option? Can it be done with

  • Testing classes with threads, events, and private methods 2010-10-26

    general consensus I've done quite a lot of reading up on the subject of testing complex classes and private methods. The general consensus seems to be: "if you need to test private methods then you're class is badly designed" "if your class is comple

  • Stepping into template classes with GDB 2011-11-20

    I'm working on implementing a balanced tree in c++, but the requirement of the assignment is that I have to use template classes. I thought of doing it with ints first and then converting to templates, but the test code we are given use templates by

  • How do I deserialize an XML file into a class with a read only property? 2008-09-25

    I've got a class that I'm using as a settings class that is serialized into an XML file that administrators can then edit to change settings in the application. (The settings are a little more complex than the App.config allows for.) I'm using the Xm

  • Fluent NHibernate - mapping portions of a single table into multiple classes with a few shared fields 2011-02-16

    I have a situation where a User table has lots of information about a user (first name, last name, email, etc) in addition to credential data (username, password, prior passwords, etc). Normally I would separate Authentication from Personalization, b

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 5.754 (s). 13 q(s)