我收到以下错误:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of method setApplicant in webService.controller.RequestController required a bean of type 'com.service.applicant.Applicant' that could not be found.
Action:
Consider defining a bean of type 'com.service.applicant.Applicant' in your configuration.
Run Code Online (Sandbox Code Playgroud)
我之前从未见过这个错误,但@Autowire无法正常工作,这很奇怪.这是项目结构:
申请人界面
public interface Applicant {
TApplicant findBySSN(String ssn) throws ServletException;
void deleteByssn(String ssn) throws ServletException;
void createApplicant(TApplicant tApplicant) throws ServletException;
void updateApplicant(TApplicant tApplicant) throws ServletException;
List<TApplicant> getAllApplicants() throws ServletException;
}
Run Code Online (Sandbox Code Playgroud)
ApplicantImpl
@Service
@Transactional
public class ApplicantImpl implements Applicant {
private static Log …Run Code Online (Sandbox Code Playgroud) 我正在为类似于以下示例的方法编写JUnit测试用例:
Class SampleA{
public static void methodA(){
boolean isSuccessful = methodB();
if(isSuccessful){
SampleB.methodC();
}
}
public static boolean methodB(){
//some logic
return true;
}
}
Class SampleB{
public static void methodC(){
return;
}
}
Run Code Online (Sandbox Code Playgroud)
我在我的测试类中编写了以下测试用例:
@Test
public void testMethodA_1(){
PowerMockito.mockStatic(SampleA.class,SampleB.class);
PowerMockito.when(SampleA.methodB()).thenReturn(true);
PowerMockito.doNothing().when(SampleB.class,"methodC");
PowerMockito.doCallRealMethod().when(SampleA.class,"methodA");
SampleA.methodA();
}
Run Code Online (Sandbox Code Playgroud)
现在我想验证是否调用类Sample B的静态methodC().如何使用PowerMockito 1.6实现?我尝试了很多东西,但似乎并没有为我做好准备.任何帮助表示赞赏.
我试图让 Spring Security 的基本身份验证与 JWT 令牌身份验证并排工作,但没有成功。我已经为我的 Web 控制台和 JWT 实现了基本身份验证,以保护许多 API 端点。这是我的配置:
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class MultiHttpSecurityConfig {
@Autowired
private UserDetailsService userDetailsService;
@Autowired
public void configureAuthentication(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
authenticationManagerBuilder
.userDetailsService(this.userDetailsService)
.passwordEncoder(bCryptPasswordEncoder());
}
@Bean
public PasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}
/**
*
* API Security configuration
*
*/
@Configuration
@Order(1)
public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter{
@Bean
public JwtAuthenticationTokenFilter authenticationTokenFilterBean() throws Exception {
return new JwtAuthenticationTokenFilter();
}
@Autowired
private JwtAuthenticationEntryPoint unauthorizedHandler;
@Override
protected void configure(HttpSecurity httpSecurity) …Run Code Online (Sandbox Code Playgroud) WebSecurityConfigurerAdapter除了包含类的包之外,我还在另一个包中扩展@SpringBootApplication.然后它没有工作生成默认用户名和密码.
当它在同一个包装中时它工作正常.
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
扩展WebSecurityConfigurerAdapter的类
package com.securitymodule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
public class WebSecurity extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// TODO Auto-generated method stub
super.configure(http);
http.antMatcher("/**").authorizeRequests().anyRequest().hasRole("USER").and().formLogin();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");
}
@Override …Run Code Online (Sandbox Code Playgroud) 我正在尝试测试void方法,但要验证它在调用它时传递给静态方法的参数。静态方法负责持久化这些参数。
class ProxyHandler {
public void process(String str) {
// parse the str and populate x, y, z
PersistManager.proxy(x, y, z);
}
}
Run Code Online (Sandbox Code Playgroud)
下面的PersistManager包含我试图捕获其接收的参数的静态方法。
class PersistManager {
public static void proxy(String x, String y, String z) {
// persist the x, y, z
}
}
Run Code Online (Sandbox Code Playgroud)
最后是我的测试类,测试ProxyHandler:
@RunWith(PowerMockRunner.class)
@PrepareForTest(PersistManager.class)
public class TestProxyHandler() {
private ProxyHandler handler;
@Before
public void setUp() {
handler = new ProxyHandler();
}
@Test
public void testProxy() {
PowerMockito.mockStatic(PersistManager.class);
ArgumentCaptor<String> arg1 = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<String> arg2 = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<String> …Run Code Online (Sandbox Code Playgroud) 这里有一个小贪婪的问题,希望这个也可以帮助那些想要了解更多关于注释验证的人
我目前正在学习Spring,现在,我计划尝试自定义注释验证.
我已经搜索了很多,现在我知道主要有两种验证,一种用于控制器,另一种是使用@Valid的注释方法
所以这是我的场景:假设我有两个或更多字段,当它们是ALL NULL时可以为null.但只有当其中一个字段包含除空字符串之外的任何值时,这些字段才需要输入.我有两个想法,但不知道如何正确实现它们.
这是类示例:
public class Subscriber {
private String name;
private String email;
private Integer age;
private String phone;
private Gender gender;
private Date birthday;
private Date confirmBirthday;
private String birthdayMessage;
private Boolean receiveNewsletter;
//Getter and Setter
}
Run Code Online (Sandbox Code Playgroud)
假设我想生日和confirmBirthday字段需要为空或反对,我可能想要为每个使用一个注释注释它们,看起来像这样:
public class Subscriber {
private String name;
private String email;
private Integer age;
private String phone;
private Gender gender;
@NotNullIf(fieldName="confirmBirthday")
private Date birthday;
@NotNullIf(fieldName="birthday")
private Date confirmBirthday;
private String birthdayMessage;
private Boolean receiveNewsletter;
//Getter and Setter
}
Run Code Online (Sandbox Code Playgroud)
所以我需要像这样创建验证Annotation: …
java ×6
spring-boot ×3
mockito ×2
powermockito ×2
annotations ×1
junit4 ×1
jwt ×1
powermock ×1
spring ×1
spring-mvc ×1
unit-testing ×1
validation ×1