如果我的控制器的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) 我的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)
如何解决此错误并让测试再次运行?
JUnit只会在我的类中测试那些公开的方法.如何对非(即私有,受保护)的进行junit测试?
我可以通过不使用junit来测试它们,但我想知道junit标准方法是什么.
有谁知道如何在Android junit测试用例中获得Test项目的上下文(扩展AndroidTestCase).
注意:测试不是仪器测试.
注2:我需要测试项目的上下文,而不是测试的实际应用程序的上下文.
我需要这个从测试项目的资产加载一些文件.
我想比较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)
我该怎么写呢?
如何使用JUnit 4创建测试套件?
我见过的所有文档似乎都不适合我.如果我使用Eclipse向导,它不会让我选择我创建的任何测试类.
你会如何简洁地断言Collection元素的相等性,特别是Set在JUnit 4中?
我正在编写一个基于Android Bitmap类(称为AndroindLib)的Android库项目,该类仅包含实用程序类(无活动).我尝试使用Android JUnit测试它,但它一直在抱怨无法找到AnroidLib.apk
单元测试Android库项目的正确方法是什么?
我试图麻烦拍摄JUnit.在源代码中,我在两个地方设置了断点:1)在一行中初始化静态成员2)其中一个测试用例的第一行.
调试器在静态字段初始化行中停止.但它并没有停留在测试用例中.无论我在测试用例中设置断点,调试器都不会停在那里.我确信测试用例已经执行,因为我可以看到我添加的日志消息出现在日志中.
任何帮助将不胜感激.
我正在使用Eclipse Galileo和JUnit4启动器.
我有一个非常简单的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 ×10
java ×6
android ×3
eclipse ×2
testing ×2
unit-testing ×2
apk ×1
collections ×1
debugging ×1
hamcrest ×1
junit4 ×1
security ×1
spring ×1
test-suite ×1