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;
}
}
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);
}
}
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
编辑:基于第一个答案(cglib需要一个),我用一个no arg构造函数更新了动物类,但是抛出了相同的异常
您看过assertj-examples吗,有一个自定义软断言的工作示例:
https://github.com/joel-costigliola/assertj-examples/blob/master/assertions-examples/src/test/generated-assertions/org/assertj/softassertions.java
https://github.com/joel-costigliola/assertj-examples/blob/master/assertions-examples/src/test/java/org/assertj/examples/generatedsoftassertionsexamples.java
我有一台带有Intel CPU的Windows机器(在引导菜单上启用VMX)。我使用VirtualBox在上面运行Ubuntu Guest。我在上面运行qemu,以运行另一个Guest OS,使用kvm(选项)。 我在尝试插入kvm-intel模块时收到以下错误: 我已经检查了来宾操作系统中的vpx标志,但我也没有看到 有没有办法在来宾Ubuntu操作系统中启用kvm支持,以便我可以在Ubuntu
问题内容: 在Android编程中,Context类的确切含义是什么? 问题答案: 简单地说: 顾名思义,它是应用程序/对象当前状态的上下文。它使新创建的对象了解正在发生的事情。通常,您调用它来获取有关程序另一部分(活动和程序包/应用程序)的信息。 您可以通过调用获取上下文,,或(当在扩展,从一类Context,如应用程序,活动,服务和IntentService类)。 上下文的典型用法: 创建新对
主机系统为ARCHLinux。我想开发需要运行Windows docker容器的.NET4.6.1应用程序。所以我在VirtualBox下运行Win10。然后我在Win10中安装Docker,以运行Windows Docker容器。Linux->VirtualBox->Win10->Docker->Windows Docker容器->要运行的应用程序。 或者更具体地说,VirtualBox是否支持
这个问题在下面的示例中重现。 } 据我所知,发生此异常是因为多个线程试图保存同一个对象。这就是我使用同步块的原因。 null null
我的应用程序一直在许多Android平台上报告这个问题(4.1、4.0.4、2.3.6.…)。但我无法在我的手机上复制这个问题。我已经在Google上搜索了这个问题,但堆栈跟踪似乎与我的不一样。
问题内容: 当Websphere调用@Startup EJB时,应在哪里设置用户上下文? 我有一个带有启动EJB的Java EE应用程序,并且我知道它在调用EJB时设置了一个用户,因为调用由于缺少角色而失败。但是,我找不到在哪里设置用户。 问题答案: EJB规范指出,会话Bean@PostConstruct方法是在未指定的安全上下文中调用的,而WebSphere并未记录特定的安全上下文或对其进