我想在我的@BeforeTest
方法中将一些Web范围注册到spring上下文中.但事实证明,春天的背景仍然存在null
.
如果我改变,测试运行正常@BeforeMethod
.我想知道如何访问上下文@BeforeTest
,因为我不希望为每个测试方法重复范围注册代码.
以下是我的代码片段.
public class MyTest extends MyBaseTest {
@Test public void someTest() { /*...*/ }
}
@ContextConfiguration(locations="/my-context.xml")
public class MyBaseTest extends AbstractTestNGSpringContextTests {
@BeforeTest public void registerWebScopes() {
ConfigurableBeanFactory factory = (ConfigurableBeanFactory)
this.applicationContext.getAutowireCapableBeanFactory();
factory.registerScope("session", new SessionScope());
factory.registerScope("request", new RequestScope());
}
/* some protected methods here */
}
Run Code Online (Sandbox Code Playgroud)
这是运行测试时的错误消息:
FAILED CONFIGURATION: @BeforeTest registerWebScopes java.lang.NullPointerException at my.MyBaseTest.registerWebScopes(MyBaseTest.java:22)
我有一个包含70个元素的列表.
例如:
List<Long> dbList = new ArrayList<Long>();
dbList
有70条记录.如果我将所有记录发送到MySql中的查询,则需要很长时间.所以现在我想每次向数据库查询发送10个元素.所以我需要以10为间隔迭代列表.我该怎么做?这是避免长时间使用的好方法IN
.
SQL查询
select model.boothId, model.panchayat.panchayatId
from Table1 model
where model.panchayat.panchayatId in(:locationValues)
and model.publicationDate.publicationDateId in (:publicationDateIdsList)
and model.constituency.id = :id group by model.panchayat.panchayatId
Run Code Online (Sandbox Code Playgroud)
提前致谢...
我有一个项目,其中包含多个子项目的共享资源; 即,以及Java代码,它包含CSS和JavaScript.
我的子项目使用maven程序集插件打包为jar.由于某些原因,这些不包括父库中的css和js资源.
所以我的问题很简单:我如何配置项目以便发生这种情况?我不希望儿童项目成为战争; 它们是作为独立jar运行的dropwizard(/ jetty)项目.
编辑 - 我应该明确表示资源包含在父项目的jar中; 只有当汇编(在子项目中)包含他们以某种方式丢失的jar时.
父母pom:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.parentproject</groupId>
<artifactId>parentproject</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
...
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/webapp</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.java</include>
<include>**/*.gwt.xml</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArgument>-proc:none</compilerArgument>
<fork>true</fork>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>
true
</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>
...
</plugins>
</build>
</project>
Run Code Online (Sandbox Code Playgroud)
示例儿童pom:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 …
Run Code Online (Sandbox Code Playgroud) 这似乎应该很简单,但我无法让它工作.我正在尝试设置distributionType
包装器任务的属性,但它不起作用:
task wrapper(type: Wrapper) {
gradleVersion = '4.1'
distributionType = DistributionType.ALL
}
Run Code Online (Sandbox Code Playgroud)
当我尝试运行包装器任务时,我收到以下错误:
$ gradle wrapper
FAILURE: Build failed with an exception.
* Where:
Build file '.../build.gradle' line: 6
* What went wrong:
A problem occurred evaluating root project 'project'.
> Could not get unknown property 'DistributionType' for task ':wrapper' of type org.gradle.api.tasks.wrapper.Wrapper.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total …
Run Code Online (Sandbox Code Playgroud) 我有一个带有TreeMap的可序列化对象.
Map<String, Dogs> dogsByNames = Maps.newTreeMap(); // guava style
Run Code Online (Sandbox Code Playgroud)
到目前为止,一切都很好.现在忽略键的情况是必要的:
Map<String, Dogs> dogsByNames = Maps.newTreeMap(String.CASE_INSENSITIVE_ORDER);
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为String.CASE_INSENSITIVE_ORDER不是serialzable:
com.google.gwt.user.client.rpc.SerializationException:类型'java.lang.String $ CaseInsensitiveComparator'未包含在可由此SerializationPolicy序列化的类型集中,或者无法加载其Class对象.出于安全考虑,此类型不会被序列化:instance = java.lang.String$CaseInsensitiveComparator@f26f68
所以我创建了自己的一个:
private static abstract class MyComparator<T>
implements
Comparator<T>, // extends Ordering<T> do not work
IsSerializable { // Serializable do not work
}
public static final MyComparator<String> CASE_INSENSITIVE_ORDER = new MyComparator<String>() {
@Override
public int compare(final String left, final String right) {
if (null == left) {
return (null == right) ? 0 : -1;
} else if (null == …
Run Code Online (Sandbox Code Playgroud) 我正在编写测试以验证我的方法的合同,例如,参数不能null
,int
参数必须是正的等等.当违反合同时,抛出的异常类型反映了它失败的原因,例如NullPointerException
或IllegalArgumentException
.有时我不关心这两个中的哪一个被抛出,只有其中一个被抛出.(是的,我应该测试确切的异常类型,但请耐心等待...)
我可以使用JUnit @Test(expected = RuntimeException.class)
(因为RuntimeException
它是最接近的公共父IAE
和NPE
),但这是过于通用的,因为被测试的方法可能会引发其他一些RuntimeException
.
相反,我正在尝试使用JUnit的ExpectedExceptions
类.
这是一个完整的例子:
import org.hamcrest.CoreMatchers;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.hamcrest.CoreMatchers.anyOf;
public class ParameterViolationTest {
private Target target = new Target();
@Rule
public ExpectedException expected = ExpectedException.none();
@Test
public void parameterViolation() {
expected.expect(anyOf(
CoreMatchers.<Class<? extends Exception>>
equalTo(NullPointerException.class),
CoreMatchers.<Class<? extends Exception>>
equalTo(IllegalArgumentException.class)));
// Null parameter violates contract, …
Run Code Online (Sandbox Code Playgroud) 例如,假设我有一个Maven构建文件,其依赖项位于两个不同的存储库中:
<repository>
<id>id1</id>
<name>id1</name>
<url>http://localhost/mavenRepo/repository1</url>
</repository>
<repository>
<id>id2</id>
<name>id2</name>
<url>http://localhost/mavenRepo/repository2</url>
</repository>
Run Code Online (Sandbox Code Playgroud)
有一个来自第一个存储库的依赖项,另一个来自第二个存储库.我怎样才能做到这一点?
我在尝试获取SD卡ID时面临服务onCreate()的问题.使用来自src-external/android-core的android.os包的FileUtils类.
mCardId = FileUtils.getFatVolumeId(Environment.getExternalStorageDirectory().getPath());
崩溃日志:
12-31 19:55:25.804: E/AndroidRuntime(1235): java.lang.NoSuchMethodError: android.os.FileUtils.getFatVolumeId 12-31 19:55:25.804: E/AndroidRuntime(1235): at cloudtv.android.cs.service.MediaPlaybackService.onCreate(MediaPlaybackService.java:198) 12-31 19:55:25.804: E/AndroidRuntime(1235): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2572) 12-31 19:55:25.804: E/AndroidRuntime(1235): at android.app.ActivityThread.access$1800(ActivityThread.java:135) 12-31 19:55:25.804: E/AndroidRuntime(1235): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 12-31 19:55:25.804: E/AndroidRuntime(1235): at android.os.Handler.dispatchMessage(Handler.java:102) 12-31 19:55:25.804: E/AndroidRuntime(1235): at android.os.Looper.loop(Looper.java:136) 12-31 19:55:25.804: E/AndroidRuntime(1235): at android.app.ActivityThread.main(ActivityThread.java:5017) 12-31 19:55:25.804: E/AndroidRuntime(1235): at java.lang.reflect.Method.invokeNative(Native Method) 12-31 19:55:25.804: E/AndroidRuntime(1235): at java.lang.reflect.Method.invoke(Method.java:515) 12-31 19:55:25.804: E/AndroidRuntime(1235): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 12-31 19:55:25.804: E/AndroidRuntime(1235): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 12-31 19:55:25.804: E/AndroidRuntime(1235): at dalvik.system.NativeStart.main(Native Method)
提前致谢!