2017-06-22 24 views
0

我一直在試驗assertJ作爲當前聲明庫的替代品,hamcrest,這是我的團隊項目中使用的。因此到目前爲止,我有以下類:AssertJ:NoSuchMethodException on softAssert

Animal.java

package com.assertions.demo.assertj; 

public class Animal { 

private Species species; 

//no arg constructor 
public Animal(){} 

public Animal(Species species) { 
    this.species = species; 
} 

public Species getSpecies() { 
    return this.species; 
} 

public enum Species{ 
    DOG(4, true, Environment.LAND), 
    PIGEON(2, false, Environment.AIR), 
    OCTOPUS(8, false, Environment.SEA), 
    ALLIGATOR(4, true, Environment.AMPHIBIOUS); 

    private int numLegs; 
    private boolean hasTail; 
    private Environment environment; 

    private Species(int numLegs, boolean hasTail, Environment environment) { 
     this.numLegs = numLegs; 
     this.hasTail = hasTail; 
     this.environment = environment; 
    } 

    public int getNumLegs(){ 
     return this.numLegs; 
    } 

    public boolean hasTail() { 
     return this.hasTail; 
    } 
    public Environment getEnvironment() { 
     return this.environment; 
    } 
} 

public enum Environment { 
    LAND, SEA, AIR, AMPHIBIOUS 
} 

} 

而且我已經創建了下面的自定義斷言它:

AnimalAssert.java

package com.assertions.demo.assertj; 

import org.assertj.core.api.AbstractAssert; 
import com.assertions.demo.assertj.Animal.Environment; 

public class AnimalAssert extends AbstractAssert<AnimalAssert, Animal> { 

public AnimalAssert(Animal actual, Class<AnimalAssert> selfType) { 
    super(actual, selfType); 
} 

public static AnimalAssert assertThat(Animal actual) { 
    return new AnimalAssert(actual, AnimalAssert.class); 
} 

public AnimalAssert hasTail(boolean hasTail) { 
    isNotNull(); 

    if(actual.getSpecies().hasTail() != hasTail) { 
     String expectedHasTail = hasTail ? "have a tail" : "not have a tail"; 
     String actualHasTail = actual.getSpecies().hasTail() ? "does" : "does not"; 
     failWithMessage("Expected animal to " + expectedHasTail + ", but animal " + actualHasTail + " have a tail"); 
    } 
    return this; 
} 

public AnimalAssert livesInEnvironment(Environment env) { 
    isNotNull(); 

    if(actual.getSpecies().getEnvironment() != env) { 
     failWithMessage("Expected animal environment to be <%s>, but was <%s>", env, actual.getSpecies().getEnvironment()); 
    } 
    return this; 
} 

public AnimalAssert hasNumberOfLegs(int numLegs) { 
    isNotNull(); 

    if(actual.getSpecies().getNumLegs() != numLegs) { 
     failWithMessage("Expected animal to have <%s> legs, but animal has <%s> legs", numLegs, actual.getSpecies().getNumLegs()); 
    } 

    return this; 
} 
} 

按照AssertJ的特性,我爲上面提供了一個入口點,並且我試圖添加sof牛逼斷言功能:

CustomSoftAssertions.java

package com.demo.assertions.assertj; 

import org.assertj.core.api.SoftAssertions; 

public class CustomSoftAssertions extends SoftAssertions { 

public AnimalAssert assertThat(Animal actual) { 
    return proxy(AnimalAssert.class, Animal.class, actual); 
} 
} 

當我運行下面的測試:

AssertJDemo.java

package com.assertions.demo.assertj; 

import org.junit.Test; 
import static org.assertj.core.api.Assertions.assertThat; 
import com.assertions.demo.assertj.Animal.Environment; 
import com.assertions.demo.assertj.Animal.Species; 

public class AssertJDemo { 
    Animal[] testAnimalArray = new Animal[] { 
     new Animal(Species.ALLIGATOR), 
     new Animal(Species.DOG), 
     new Animal(Species.OCTOPUS), 
     new Animal(Species.PIGEON) 
    }; 

    @Test 
    public void softAssertWithCustomAssertionsTest() { 
     Animal dog = testAnimalArray[1]; 
     CustomSoftAssertions softAssert = new CustomSoftAssertions(); 

     softAssert.assertThat(dog).hasTail(false); 
     softAssert.assertThat(dog).livesInEnvironment(Environment.AIR); 
     softAssert.assertThat(dog).hasNumberOfLegs(8); 
    } 
} 

我碰到下面的堆棧跟蹤:

org.assertj.core.internal.cglib.core.CodeGenerationException: java.lang.NoSuchMethodException-->com.assertions.demo.assertj.AnimalAssert$$EnhancerByCGLIB$$b5c6715f.<init>(com.assertions.demo.assertj.Animal) 
at org.assertj.core.internal.cglib.core.ReflectUtils.getConstructor(ReflectUtils.java:313) 
at org.assertj.core.internal.cglib.proxy.Enhancer$EnhancerFactoryData.<init>(Enhancer.java:420) 
at org.assertj.core.internal.cglib.proxy.Enhancer.wrapCachedClass(Enhancer.java:709) 
at org.assertj.core.internal.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:94) 
at org.assertj.core.internal.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:91) 
at org.assertj.core.internal.cglib.core.internal.LoadingCache$2.call(LoadingCache.java:54) 
at java.util.concurrent.FutureTask.run(FutureTask.java:266) 
at org.assertj.core.internal.cglib.core.internal.LoadingCache.createEntry(LoadingCache.java:61) 
at org.assertj.core.internal.cglib.core.internal.LoadingCache.get(LoadingCache.java:34) 
at org.assertj.core.internal.cglib.core.AbstractClassGenerator$ClassLoaderData.get(AbstractClassGenerator.java:116) 
at org.assertj.core.internal.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:291) 
at org.assertj.core.internal.cglib.proxy.Enhancer.createHelper(Enhancer.java:480) 
at org.assertj.core.internal.cglib.proxy.Enhancer.create(Enhancer.java:324) 
at org.assertj.core.api.SoftProxies.create(SoftProxies.java:42) 
at org.assertj.core.api.AbstractSoftAssertions.proxy(AbstractSoftAssertions.java:31) 
at com.assertions.demo.assertj.CustomSoftAssertions.assertThat(CustomSoftAssertions.java:18) 
at com.assertions.demo.assertj.AssertJDemo.softAssertWithCustomAssertionsTest(AssertJDemo.java:202) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:497) 
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) 
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) 
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) 
Caused by: java.lang.NoSuchMethodException: com.assertions.demo.assertj.AnimalAssert$$EnhancerByCGLIB$$b5c6715f.<init>(com.assertions.demo.assertj.Animal) 
at java.lang.Class.getConstructor0(Class.java:3082) 
at java.lang.Class.getDeclaredConstructor(Class.java:2178) 
at org.assertj.core.internal.cglib.core.ReflectUtils.getConstructor(ReflectUtils.java:309) 
... 39 more 

我是在做一些與assertJ庫有關的錯誤,或者我違反了某些我沒有發現的特定於java的東西?我使用Java 8 assertJ 3.8.0

編輯:我曾基於第一個答案(CGLIB需要一個),但同樣會拋出異常

+0

繼編輯,這是因爲異常是* *要求對於需要花費構造作爲一個「動物」的參數.. @davidxxx你知道你可以編輯你的答案,所以不需要添加多個答案。 – 2017-06-22 11:27:17

+0

@RC所以動物聲明需要動物作爲arg的構造函數? AssertJ是新的,所以仍然在尋找我的方法 – jbailie1991

+0

其實,從我讀過的內容中,我忘記了調用softAssert.assertAll()。我還調整了代碼以更緊密地跟隨示例(將selfType參數和硬編碼的AnimalAssert.class移除到構造函數調用中)。當我添加這條線並改變斷言類時,測試運行時應該是這樣的 – jbailie1991

回答