标签: junit

Spring Test&Security:如何模拟身份验证?

如果我的控制器的URL被正确保护,我试图弄清楚如何进行单元测试.以防有人改变现状并意外删除安全设置.

我的控制器方法如下所示:

@RequestMapping("/api/v1/resource/test") 
@Secured("ROLE_USER")
public @ResonseBody String test() {
    return "test";
}
Run Code Online (Sandbox Code Playgroud)

我设置了一个WebTestEnvironment,如下所示:

import javax.annotation.Resource;
import javax.naming.NamingException;
import javax.sql.DataSource;

import org.junit.Before;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.FilterChainProxy;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration({ 
        "file:src/main/webapp/WEB-INF/spring/security.xml",
        "file:src/main/webapp/WEB-INF/spring/applicationContext.xml",
        "file:src/main/webapp/WEB-INF/spring/servlet-context.xml" })
public class WebappTestEnvironment2 {

    @Resource
    private FilterChainProxy springSecurityFilterChain;

    @Autowired
    @Qualifier("databaseUserService")
    protected UserDetailsService userDetailsService;

    @Autowired
    private WebApplicationContext wac;

    @Autowired …
Run Code Online (Sandbox Code Playgroud)

security testing model-view-controller junit spring

102
推荐指数
5
解决办法
15万
查看次数

测试运行'JUnit 4'没有测试结果

我的Java测试在Eclipse中运行良好.但现在,当我从运行菜单重新启动测试时,我收到以下消息:

No tests found with test runner 'JUnit 4'
Run Code Online (Sandbox Code Playgroud)

.classpath文件中我有所有jar文件,最后有:

<classpathentry exported="true" kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
    <classpathentry kind="output" path="bin"/>
</classpath>
Run Code Online (Sandbox Code Playgroud)

如何解决此错误并让测试再次运行?

java eclipse junit junit4

101
推荐指数
9
解决办法
20万
查看次数

junit&java:测试非公共方法

JUnit只会在我的类中测试那些公开的方法.如何对非(即私有,受保护)的进行junit测试?

我可以通过不使用junit来测试它们,但我想知道junit标准方法是什么.

java testing junit unit-testing

100
推荐指数
7
解决办法
7万
查看次数

在Android junit测试用例中获取测试项目的上下文

有谁知道如何在Android junit测试用例中获得Test项目的上下文(扩展AndroidTestCase).

注意:测试不是仪器测试.

注2:我需要测试项目的上下文,而不是测试的实际应用程序的上下文.

我需要这个从测试项目的资产加载一些文件.

junit android

100
推荐指数
8
解决办法
6万
查看次数

Hamcrest比较收藏品

我想比较2个列表:

assertThat(actual.getList(), is(Matchers.containsInAnyOrder(expectedList)));
Run Code Online (Sandbox Code Playgroud)

但想法

java: no suitable method found for assertThat(java.util.List<Agent>,org.hamcrest.Matcher<java.lang.Iterable<? extends model.Agents>>)
method org.junit.Assert.<T>assertThat(T,org.hamcrest.Matcher<T>) is not applicable
  (no instance(s) of type variable(s) T exist so that argument type org.hamcrest.Matcher<java.lang.Iterable<? extends model.Agents>> conforms to formal parameter type org.hamcrest.Matcher<T>)
method org.junit.Assert.<T>assertThat(java.lang.String,T,org.hamcrest.Matcher<T>) is not applicable
  (cannot instantiate from arguments because actual and formal argument lists differ in length)
Run Code Online (Sandbox Code Playgroud)

我该怎么写呢?

java junit hamcrest

100
推荐指数
5
解决办法
11万
查看次数

JUnit 4测试套件

如何使用JUnit 4创建测试套件?

我见过的所有文档似乎都不适合我.如果我使用Eclipse向导,它不会让我选择我创建的任何测试类.

java junit test-suite

97
推荐指数
3
解决办法
7万
查看次数

JUnit 4比较集

你会如何简洁地断言Collection元素的相等性,特别是Set在JUnit 4中?

java collections junit unit-testing

96
推荐指数
4
解决办法
9万
查看次数

如何测试Android库项目

我正在编写一个基于Android Bitmap类(称为AndroindLib)的Android库项目,该类仅包含实用程序类(无活动).我尝试使用Android JUnit测试它,但它一直在抱怨无法找到AnroidLib.apk

单元测试Android库项目的正确方法是什么?

junit android apk

96
推荐指数
4
解决办法
3万
查看次数

Eclipse - 调试器不会在断点处停止

我试图麻烦拍摄JUnit.在源代码中,我在两个地方设置了断点:1)在一行中初始化静态成员2)其中一个测试用例的第一行.

调试器在静态字段初始化行中停止.但它并没有停留在测试用例中.无论我在测试用例中设置断点,调试器都不会停在那里.我确信测试用例已经执行,因为我可以看到我添加的日志消息出现在日志中.

任何帮助将不胜感激.

我正在使用Eclipse Galileo和JUnit4启动器.

java eclipse debugging junit

94
推荐指数
5
解决办法
17万
查看次数

使用Android Test Framework进行Android AsyncTask测试

我有一个非常简单的AsyncTask实现示例,并且在使用Android JUnit框架测试它时遇到问题.

当我在普通应用程序中实例化并执行它时,它工作得很好.但是当它从任何Android测试框架类(即AndroidTestCase,ActivityUnitTestCase,ActivityInstrumentationTestCase2等)执行时,它表现得很奇怪:

  • doInBackground()正确执行方法
  • 然而,它没有任何调用它的通知方法(onPostExecute(),onProgressUpdate()等) -只是默默忽略它们没有表现出任何错误.

这是非常简单的AsyncTask示例:

package kroz.andcookbook.threads.asynctask;

import android.os.AsyncTask;
import android.util.Log;
import android.widget.ProgressBar;
import android.widget.Toast;

public class AsyncTaskDemo extends AsyncTask<Integer, Integer, String> {

AsyncTaskDemoActivity _parentActivity;
int _counter;
int _maxCount;

public AsyncTaskDemo(AsyncTaskDemoActivity asyncTaskDemoActivity) {
    _parentActivity = asyncTaskDemoActivity;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
    _parentActivity._progressBar.setVisibility(ProgressBar.VISIBLE);
    _parentActivity._progressBar.invalidate();
}

@Override
protected String doInBackground(Integer... params) {
    _maxCount = params[0];
    for (_counter = 0; _counter <= _maxCount; _counter++) {
        try { …
Run Code Online (Sandbox Code Playgroud)

junit android

94
推荐指数
5
解决办法
6万
查看次数